diff --git a/src/main/java/com/ghgande/j2mod/modbus/io/BytesOutputStream.java b/src/main/java/com/ghgande/j2mod/modbus/io/BytesOutputStream.java index 65af0fc..068b9b5 100644 --- a/src/main/java/com/ghgande/j2mod/modbus/io/BytesOutputStream.java +++ b/src/main/java/com/ghgande/j2mod/modbus/io/BytesOutputStream.java @@ -70,6 +70,17 @@ public void reset() { count = 0; } + /** + * Resets this BytesInputStream using the given + * byte[] as new input buffer. + * + * @param data a byte array with data to be read. + */ + public void reset(byte[] data) { + buf = data; + count = 0; + } + @Override public void writeBoolean(boolean v) throws IOException { dataOutputStream.writeBoolean(v); diff --git a/src/main/java/com/ghgande/j2mod/modbus/io/ModbusRTUTransport.java b/src/main/java/com/ghgande/j2mod/modbus/io/ModbusRTUTransport.java index 3d51321..a36a3d2 100644 --- a/src/main/java/com/ghgande/j2mod/modbus/io/ModbusRTUTransport.java +++ b/src/main/java/com/ghgande/j2mod/modbus/io/ModbusRTUTransport.java @@ -321,7 +321,8 @@ protected ModbusRequest readRequestIn(AbstractModbusListener listener) throws Mo int[] crc = ModbusUtil.calculateCRC(inBuffer, 0, dlength); // does not include CRC if (ModbusUtil.unsignedByteToInt(inBuffer[dlength]) != crc[0] || ModbusUtil.unsignedByteToInt(inBuffer[dlength + 1]) != crc[1]) { if (logger.isDebugEnabled()) { - logger.debug("CRC should be {}, {}", Integer.toHexString(crc[0]), Integer.toHexString(crc[1])); + logger.debug("CRC should be {}, {} inBuffer={}", Integer.toHexString(crc[0]), Integer.toHexString(crc[1]), + ModbusUtil.toHex(inBuffer, 0, dlength + 2)); } // Drain the input in case the frame was misread and more @@ -380,6 +381,8 @@ protected ModbusRequest readRequestIn(AbstractModbusListener listener) throws Mo logger.debug("Read message not meant for us: {}", ModbusUtil.toHex(byteInputOutputStream.getBuffer(), 0, byteInputOutputStream.size())); } } + // as the buffer might have been bumped due to more than 256 bytes input, we reset it to the original inBuffer + byteInputOutputStream.reset(inBuffer); } } }