//use same buffer
byte[] buffer = m_ByteIn.getBuffer();
//read to byte length of message
if (m_Input.read(buffer, 0, 6) == -1) {
throw new ModbusIOException("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();
res = ModbusResponse.createModbusResponse(functionCode);
res.readFrom(m_ByteIn);
}
return res;
/*
try {
int transactionID = m_Input.readUnsignedShort();
//System.out.println("Read tid="+transactionID);
int protocolID = m_Input.readUnsignedShort();
//System.out.println("Read pid="+protocolID);
int dataLength = m_Input.readUnsignedShort();
//System.out.println("Read length="+dataLength);
int unitID = m_Input.readUnsignedByte();
//System.out.println("Read uid="+unitID);
int functionCode = m_Input.readUnsignedByte();
//System.out.println("Read fc="+functionCode);
ModbusResponse response =
ModbusResponse.createModbusResponse(functionCode, m_Input, false);
if (response instanceof ExceptionResponse) {
//skip rest of bytes
for (int i = 0; i < dataLength - 2; i++) {
m_Input.readByte();
}
}
//set read parameters
response.setTransactionID(transactionID);
response.setProtocolID(protocolID);
response.setUnitID(unitID);
return response;
*/
} catch (Exception ex) {
ex.printStackTrace();
throw new ModbusIOException("I/O exception - failed to read.");
}
}//readResponse