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

Allow CRC mismatch to be ignored #88

Merged
merged 2 commits into from
Nov 15, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.digitalpetri.modbus.Crc16;
import com.digitalpetri.modbus.ModbusRtuFrame;
import com.digitalpetri.modbus.TimeoutScheduler.TimeoutHandle;
import com.digitalpetri.modbus.exceptions.ModbusCrcException;
import com.digitalpetri.modbus.exceptions.ModbusException;
import com.digitalpetri.modbus.exceptions.ModbusExecutionException;
import com.digitalpetri.modbus.exceptions.ModbusResponseException;
Expand Down Expand Up @@ -80,7 +81,7 @@ public CompletionStage<ModbusResponsePdu> sendAsync(int unitId, ModbusRequestPdu
// The frame parser needs to be reset!
// It could be "stuck" in Accumulating or ParseError states if the timeout was caused by
// an incomplete or invalid response rather than no response.
transport.resetFrameParser();
resetFrameParser();

promise.future.completeExceptionally(
new TimeoutException("request timed out after %sms".formatted(timeoutMillis))
Expand Down Expand Up @@ -167,9 +168,9 @@ private void onFrameReceived(ModbusRtuFrame frame) {
}

if (!verifyCrc16(frame)) {
transport.resetFrameParser();
resetFrameParser();

promise.future.completeExceptionally(new ModbusException("CRC mismatch"));
promise.future.completeExceptionally(new ModbusCrcException(frame));
return;
}

Expand Down Expand Up @@ -229,13 +230,40 @@ private void onFrameReceived(ModbusRtuFrame frame) {
}
}

/**
* Reset the transport's frame parser.
*/
protected void resetFrameParser() {
transport.resetFrameParser();
}

/**
* Calculate the CRC-16 for the given frame (unit ID and PDU).
*
* @param unitId the unit ID.
* @param pdu the PDU.
* @return a {@link ByteBuffer} containing the calculated CRC-16.
*/
protected ByteBuffer calculateCrc16(int unitId, ByteBuffer pdu) {
var crc16 = new Crc16();
crc16.update(unitId);
crc16.update(pdu);

ByteBuffer crc = ByteBuffer.allocate(2);
// write crc in little-endian order
crc.put((byte) (crc16.getValue() & 0xFF));
crc.put((byte) ((crc16.getValue() >> 8) & 0xFF));

return crc.flip();
}

/**
* Verify the reported CRC-16 matches the calculated CRC-16.
*
* @param frame the frame to verify.
* @return {@code true} if the CRC-16 matches, {@code false} otherwise.
*/
private static boolean verifyCrc16(ModbusRtuFrame frame) {
protected boolean verifyCrc16(ModbusRtuFrame frame) {
var crc16 = new Crc16();
crc16.update(frame.unitId());
crc16.update(frame.pdu());
Expand All @@ -249,19 +277,6 @@ private static boolean verifyCrc16(ModbusRtuFrame frame) {
return expected == reported;
}

private ByteBuffer calculateCrc16(int unitId, ByteBuffer pdu) {
var crc16 = new Crc16();
crc16.update(unitId);
crc16.update(pdu);

ByteBuffer crc = ByteBuffer.allocate(2);
// write crc in little-endian order
crc.put((byte) (crc16.getValue() & 0xFF));
crc.put((byte) ((crc16.getValue() >> 8) & 0xFF));

return crc.flip();
}

/**
* Create a new {@link ModbusRtuClient} using the given {@link ModbusRtuClientTransport} and a
* {@link ModbusClientConfig} with the default values.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.digitalpetri.modbus.exceptions;

import com.digitalpetri.modbus.ModbusRtuFrame;
import java.io.Serial;

public class ModbusCrcException extends ModbusException {

@Serial
private static final long serialVersionUID = -5350159787088895451L;

private final ModbusRtuFrame frame;

public ModbusCrcException(ModbusRtuFrame frame) {
super("CRC mismatch");

this.frame = frame;
}

/**
* Get the frame that caused the exception.
*
* @return the frame that caused the exception.
*/
public ModbusRtuFrame getFrame() {
return frame;
}

}