Skip to content

Commit ea259a1

Browse files
committed
Throw ModbusCrcException on CRC mismatch
1 parent ef9d319 commit ea259a1

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

modbus/src/main/java/com/digitalpetri/modbus/client/ModbusRtuClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.digitalpetri.modbus.Crc16;
44
import com.digitalpetri.modbus.ModbusRtuFrame;
55
import com.digitalpetri.modbus.TimeoutScheduler.TimeoutHandle;
6+
import com.digitalpetri.modbus.exceptions.ModbusCrcException;
67
import com.digitalpetri.modbus.exceptions.ModbusException;
78
import com.digitalpetri.modbus.exceptions.ModbusExecutionException;
89
import com.digitalpetri.modbus.exceptions.ModbusResponseException;
@@ -169,7 +170,7 @@ private void onFrameReceived(ModbusRtuFrame frame) {
169170
if (!verifyCrc16(frame)) {
170171
transport.resetFrameParser();
171172

172-
promise.future.completeExceptionally(new ModbusException("CRC mismatch"));
173+
promise.future.completeExceptionally(new ModbusCrcException(frame));
173174
return;
174175
}
175176

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.digitalpetri.modbus.exceptions;
2+
3+
import com.digitalpetri.modbus.ModbusRtuFrame;
4+
import java.io.Serial;
5+
6+
public class ModbusCrcException extends ModbusException {
7+
8+
@Serial
9+
private static final long serialVersionUID = -5350159787088895451L;
10+
11+
private final ModbusRtuFrame frame;
12+
13+
public ModbusCrcException(ModbusRtuFrame frame) {
14+
super("CRC mismatch");
15+
16+
this.frame = frame;
17+
}
18+
19+
/**
20+
* Get the frame that caused the exception.
21+
*
22+
* @return the frame that caused the exception.
23+
*/
24+
public ModbusRtuFrame getFrame() {
25+
return frame;
26+
}
27+
28+
}

0 commit comments

Comments
 (0)