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("I/O exception - Serial port timeout.");
}
m_ByteInOut.writeByte(in);
}
//check LRC
if (m_InBuffer[m_ByteInOut.size()-1] !=
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) {