@@ -81,7 +81,7 @@ public CompletionStage<ModbusResponsePdu> sendAsync(int unitId, ModbusRequestPdu
81
81
// The frame parser needs to be reset!
82
82
// It could be "stuck" in Accumulating or ParseError states if the timeout was caused by
83
83
// an incomplete or invalid response rather than no response.
84
- transport . resetFrameParser ();
84
+ resetFrameParser ();
85
85
86
86
promise .future .completeExceptionally (
87
87
new TimeoutException ("request timed out after %sms" .formatted (timeoutMillis ))
@@ -168,7 +168,7 @@ private void onFrameReceived(ModbusRtuFrame frame) {
168
168
}
169
169
170
170
if (!verifyCrc16 (frame )) {
171
- transport . resetFrameParser ();
171
+ resetFrameParser ();
172
172
173
173
promise .future .completeExceptionally (new ModbusCrcException (frame ));
174
174
return ;
@@ -230,13 +230,40 @@ private void onFrameReceived(ModbusRtuFrame frame) {
230
230
}
231
231
}
232
232
233
+ /**
234
+ * Reset the transport's frame parser.
235
+ */
236
+ protected void resetFrameParser () {
237
+ transport .resetFrameParser ();
238
+ }
239
+
240
+ /**
241
+ * Calculate the CRC-16 for the given frame (unit ID and PDU).
242
+ *
243
+ * @param unitId the unit ID.
244
+ * @param pdu the PDU.
245
+ * @return a {@link ByteBuffer} containing the calculated CRC-16.
246
+ */
247
+ protected ByteBuffer calculateCrc16 (int unitId , ByteBuffer pdu ) {
248
+ var crc16 = new Crc16 ();
249
+ crc16 .update (unitId );
250
+ crc16 .update (pdu );
251
+
252
+ ByteBuffer crc = ByteBuffer .allocate (2 );
253
+ // write crc in little-endian order
254
+ crc .put ((byte ) (crc16 .getValue () & 0xFF ));
255
+ crc .put ((byte ) ((crc16 .getValue () >> 8 ) & 0xFF ));
256
+
257
+ return crc .flip ();
258
+ }
259
+
233
260
/**
234
261
* Verify the reported CRC-16 matches the calculated CRC-16.
235
262
*
236
263
* @param frame the frame to verify.
237
264
* @return {@code true} if the CRC-16 matches, {@code false} otherwise.
238
265
*/
239
- private static boolean verifyCrc16 (ModbusRtuFrame frame ) {
266
+ protected boolean verifyCrc16 (ModbusRtuFrame frame ) {
240
267
var crc16 = new Crc16 ();
241
268
crc16 .update (frame .unitId ());
242
269
crc16 .update (frame .pdu ());
@@ -250,19 +277,6 @@ private static boolean verifyCrc16(ModbusRtuFrame frame) {
250
277
return expected == reported ;
251
278
}
252
279
253
- private ByteBuffer calculateCrc16 (int unitId , ByteBuffer pdu ) {
254
- var crc16 = new Crc16 ();
255
- crc16 .update (unitId );
256
- crc16 .update (pdu );
257
-
258
- ByteBuffer crc = ByteBuffer .allocate (2 );
259
- // write crc in little-endian order
260
- crc .put ((byte ) (crc16 .getValue () & 0xFF ));
261
- crc .put ((byte ) ((crc16 .getValue () >> 8 ) & 0xFF ));
262
-
263
- return crc .flip ();
264
- }
265
-
266
280
/**
267
281
* Create a new {@link ModbusRtuClient} using the given {@link ModbusRtuClientTransport} and a
268
282
* {@link ModbusClientConfig} with the default values.
0 commit comments