use of net.wimpi.modbus.ModbusIOException in project openhab1-addons by openhab.
the class ModbusTCPTransport method readRequest.
// write
@Override
public ModbusRequest readRequest() throws ModbusIOException {
// System.out.println("readRequest()");
try {
ModbusRequest req = null;
synchronized (m_ByteIn) {
// use same buffer
byte[] buffer = m_ByteIn.getBuffer();
// read to byte length of message
if (m_Input.read(buffer, 0, 6) == -1) {
throw new EOFException("Premature end of stream (Header truncated).");
}
// extract length of bytes following in message
int bf = ModbusUtil.registerToShort(buffer, 4);
// read rest
if (m_Input.read(buffer, 6, bf) == -1) {
throw new ModbusIOException("Premature end of stream (Message truncated).");
}
m_ByteIn.reset(buffer, (6 + bf));
m_ByteIn.skip(7);
int functionCode = m_ByteIn.readUnsignedByte();
m_ByteIn.reset();
req = ModbusRequest.createModbusRequest(functionCode);
req.readFrom(m_ByteIn);
}
return req;
/*
* int transactionID = m_Input.readUnsignedShort();
* int protocolID = m_Input.readUnsignedShort();
* int dataLength = m_Input.readUnsignedShort();
* if (protocolID != Modbus.DEFAULT_PROTOCOL_ID || dataLength > 256) {
* throw new ModbusIOException();
* }
* int unitID = m_Input.readUnsignedByte();
* int functionCode = m_Input.readUnsignedByte();
* ModbusRequest request =
* ModbusRequest.createModbusRequest(functionCode, m_Input, false);
* if (request instanceof IllegalFunctionRequest) {
* //skip rest of bytes
* for (int i = 0; i < dataLength - 2; i++) {
* m_Input.readByte();
* }
* }
* //set read parameters
* request.setTransactionID(transactionID);
* request.setProtocolID(protocolID);
* request.setUnitID(unitID);
* return request;
*
*/
} catch (EOFException eoex) {
throw new ModbusIOException(true);
} catch (SocketException sockex) {
// connection reset by peer, also EOF
throw new ModbusIOException(true);
} catch (Exception ex) {
ex.printStackTrace();
throw new ModbusIOException("I/O exception - failed to read.");
}
}
use of net.wimpi.modbus.ModbusIOException in project openhab1-addons by openhab.
the class ModbusTCPTransport method writeMessage.
// close
@Override
public void writeMessage(ModbusMessage msg) throws ModbusIOException {
try {
msg.writeTo(m_Output);
m_Output.flush();
// write more sophisticated exception handling
} catch (Exception ex) {
throw new ModbusIOException(String.format("I/O exception - failed to write: %s", ex.getMessage()));
}
}
use of net.wimpi.modbus.ModbusIOException in project openhab1-addons by openhab.
the class ModbusUDPTransport method readRequest.
// write
@Override
public ModbusRequest readRequest() throws ModbusIOException {
try {
ModbusRequest req = null;
synchronized (m_ByteIn) {
m_ByteIn.reset(m_Terminal.receiveMessage());
m_ByteIn.skip(7);
int functionCode = m_ByteIn.readUnsignedByte();
m_ByteIn.reset();
req = ModbusRequest.createModbusRequest(functionCode);
req.readFrom(m_ByteIn);
}
return req;
} catch (Exception ex) {
throw new ModbusIOException("I/O exception - failed to read.");
}
}
use of net.wimpi.modbus.ModbusIOException in project openhab1-addons by openhab.
the class ModbusUDPTransport method readResponse.
// readRequest
@Override
public ModbusResponse readResponse() throws ModbusIOException {
try {
ModbusResponse res = null;
synchronized (m_ByteIn) {
m_ByteIn.reset(m_Terminal.receiveMessage());
m_ByteIn.skip(7);
int functionCode = m_ByteIn.readUnsignedByte();
m_ByteIn.reset();
res = ModbusResponse.createModbusResponse(functionCode);
res.readFrom(m_ByteIn);
}
return res;
} catch (InterruptedIOException ioex) {
throw new ModbusIOException("Socket timed out.");
} catch (Exception ex) {
ex.printStackTrace();
throw new ModbusIOException("I/O exception - failed to read.");
}
}
use of net.wimpi.modbus.ModbusIOException in project openhab1-addons by openhab.
the class ModbusSerialListener method listen.
/**
* Listen to incoming messages.
*/
private void listen() {
try {
m_Listening = true;
m_SerialCon.open();
logger.trace("Opened Serial connection.");
ModbusTransport transport = m_SerialCon.getModbusTransport();
do {
if (m_Listening) {
try {
// 1. read the request
ModbusRequest request = transport.readRequest();
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());
transport.writeMessage(response);
count();
} catch (ModbusIOException ex) {
ex.printStackTrace();
continue;
}
}
// ensure nice multithreading behaviour on specific platforms
} while (true);
} catch (Exception e) {
// FIXME: this is a major failure, how do we handle this
e.printStackTrace();
}
}
Aggregations