Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WA9266_fix_failing_crc_check #154

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/com/ghgande/j2mod/modbus/io/BytesOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ public void reset() {
count = 0;
}

/**
* Resets this <tt>BytesInputStream</tt> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}
Expand Down