use of net.wimpi.modbus.ModbusIOException in project openhab1-addons by openhab.
the class TCPConnectionHandler method run.
// setConnection
@Override
public void run() {
try {
do {
// 1. read the request
ModbusRequest request = m_Transport.readRequest();
logger.trace("Request:{}", request.getHexMessage());
ModbusResponse response = null;
// test if Process image exists
if (ModbusCoupler.getReference().getProcessImage() == null) {
response = request.createExceptionResponse(Modbus.ILLEGAL_FUNCTION_EXCEPTION);
} else {
response = request.createResponse();
}
logger.debug("Request:{}", request.getHexMessage());
logger.debug("Response:{}", response.getHexMessage());
m_Transport.writeMessage(response);
} while (true);
} catch (ModbusIOException ex) {
if (!ex.isEOF()) {
// other troubles, output for debug
ex.printStackTrace();
}
} finally {
try {
m_Connection.close();
} catch (Exception ex) {
// ignore
}
}
}
use of net.wimpi.modbus.ModbusIOException in project openhab1-addons by openhab.
the class ModbusASCIITransport method readResponse.
// readRequest
@Override
public ModbusResponse readResponse() throws ModbusIOException {
boolean done = false;
ModbusResponse response = null;
int in = -1;
setReceiveThreshold(1);
try {
do {
// 1. Skip to FRAME_START
while ((in = m_InputStream.read()) != FRAME_START) {
if (in == -1) {
throw new IOException("readResponse: I/O exception - Serial port timeout.");
}
}
logger.trace("Managed to read at least one byte");
// 2. Read to FRAME_END
synchronized (m_InBuffer) {
m_ByteInOut.reset();
while ((in = m_InputStream.read()) != FRAME_END) {
if (in == -1) {
throw new IOException("I/O exception - Serial port timeout.");
}
m_ByteInOut.writeByte(in);
}
int len = m_ByteInOut.size();
logger.debug("Received: {}", ModbusUtil.toHex(m_InBuffer, 0, len));
// check LRC
if ((m_InBuffer[len - 1] & 0xff) != ModbusUtil.calculateLRC(m_InBuffer, 0, len - 1)) {
logger.debug("LRC is wrong: received={} calculated={}", (m_InBuffer[len - 1] & 0xff), ModbusUtil.calculateLRC(m_InBuffer, 0, len - 1));
continue;
}
m_ByteIn.reset(m_InBuffer, m_ByteInOut.size());
in = m_ByteIn.readUnsignedByte();
// JDC: To check slave unit identifier in a response we need to know
// the slave id in the request. This is not tracked since slaves
// only respond when a master request is made and there is only one
// master. We are the only master, so we can assume that this
// response message is from the slave responding to the last request.
// if (in != ModbusCoupler.getReference().getUnitID()) {
// continue;
// }
in = m_ByteIn.readUnsignedByte();
// create request
response = ModbusResponse.createModbusResponse(in);
response.setHeadless();
// read message
m_ByteIn.reset(m_InBuffer, m_ByteInOut.size());
response.readFrom(m_ByteIn);
}
done = true;
} while (!done);
return response;
} catch (Exception ex) {
final String errMsg = String.format("I/O exception - failed to read: %s", ex.getMessage());
logger.debug("{}", errMsg);
throw new ModbusIOException("readResponse " + errMsg);
} finally {
m_CommPort.disableReceiveThreshold();
}
}
use of net.wimpi.modbus.ModbusIOException in project openhab1-addons by openhab.
the class ModbusASCIITransport method readRequest.
// writeMessage
@Override
public ModbusRequest readRequest() throws ModbusIOException {
boolean done = false;
ModbusRequest request = null;
int in = -1;
try {
do {
// 1. Skip to FRAME_START
while ((in = m_InputStream.read()) != FRAME_START) {
;
}
// 2. Read to FRAME_END
synchronized (m_InBuffer) {
m_ByteInOut.reset();
while ((in = m_InputStream.read()) != FRAME_END) {
if (in == -1) {
throw new IOException("readRequest: I/O exception - Serial port timeout.");
}
m_ByteInOut.writeByte(in);
}
// check LRC
if ((m_InBuffer[m_ByteInOut.size() - 1] & 0xff) != ModbusUtil.calculateLRC(m_InBuffer, 0, m_ByteInOut.size() - 1)) {
continue;
}
;
m_ByteIn.reset(m_InBuffer, m_ByteInOut.size());
in = m_ByteIn.readUnsignedByte();
// check message with this slave unit identifier
if (in != ModbusCoupler.getReference().getUnitID()) {
continue;
}
in = m_ByteIn.readUnsignedByte();
// create request
request = ModbusRequest.createModbusRequest(in);
request.setHeadless();
// read message
m_ByteIn.reset(m_InBuffer, m_ByteInOut.size());
request.readFrom(m_ByteIn);
}
done = true;
} while (!done);
return request;
} catch (Exception ex) {
final String errMsg = "I/O exception - failed to read";
logger.debug("{}: {}", errMsg, ex.getMessage());
throw new ModbusIOException("readRequest: " + errMsg);
}
}
use of net.wimpi.modbus.ModbusIOException in project openhab1-addons by openhab.
the class ModbusBINTransport method readResponse.
// readRequest
@Override
public ModbusResponse readResponse() throws ModbusIOException {
boolean done = false;
ModbusResponse response = null;
int in = -1;
setReceiveThreshold(1);
try {
do {
// 1. Skip to FRAME_START
while ((in = m_InputStream.read()) != FRAME_START) {
// FIXME: handle EOF, similar to ASCIITransport
}
// 2. Read to FRAME_END
synchronized (m_InBuffer) {
m_ByteInOut.reset();
while ((in = m_InputStream.read()) != FRAME_END) {
m_ByteInOut.writeByte(in);
}
// check CRC
int[] crc = ModbusUtil.calculateCRC(m_InBuffer, 0, m_ByteInOut.size() - 2);
if (!(// low byte first
m_InBuffer[m_ByteInOut.size() - 2] == crc[0] && // hibyte
m_InBuffer[m_ByteInOut.size() - 1] == crc[1])) {
continue;
}
m_ByteIn.reset(m_InBuffer, m_ByteInOut.size());
in = m_ByteIn.readUnsignedByte();
// check unit identifier
if (in != ModbusCoupler.getReference().getUnitID()) {
continue;
}
m_ByteIn.reset(m_InBuffer, m_ByteInOut.size());
in = m_ByteIn.readUnsignedByte();
// check unit identifier
if (in != ModbusCoupler.getReference().getUnitID()) {
continue;
}
in = m_ByteIn.readUnsignedByte();
// create request
response = ModbusResponse.createModbusResponse(in);
response.setHeadless();
// read message
m_ByteIn.reset(m_InBuffer, m_ByteInOut.size());
response.readFrom(m_ByteIn);
}
done = true;
} while (!done);
return response;
} catch (Exception ex) {
final String errMsg = "failed to read";
logger.debug("{}: {}", errMsg, ex.getMessage());
throw new ModbusIOException("I/O exception - " + errMsg);
} finally {
m_CommPort.disableReceiveThreshold();
}
}
Aggregations