node-modbus icon indicating copy to clipboard operation
node-modbus copied to clipboard

CRC calculation error for WriteSingleCoil response

Open yangit opened this issue 2 months ago • 1 comments

What is the problem

  • client.writeSingleCoil(6, true) response is CRC-ed incorrectly if the values is set to true
  • client.writeSingleCoil(6, false) response is CRC-ed correctly

The problem above is true for any register but I show it for register No6 so that it is easier to see where which bytes are.

Expected outcome

Both commands should CRC response correctly

Setup

I'm on Ubuntu 22.04.4 LTS Node v20.12.1 using FTDI UART TTL USB adapter

If I use serial port and send this command to my device:

01 05 00 06 ff 00 6c 3b

01 SlaveId 
05 Write Sinlge Coil
00 06 Address
ff 00 set to true
6c 3b check sum

and I get back this reply

01 05 00 06 ff 00 6c 3b
which is the same thing just confirming that bytes are set

The above is confirmed using SerialPort package REPL, and also by using 1 extra FTDI adapter evaesdropping on tx line from my device. So I'm 100% sure the response bytes what they are.

If I use JS Modbus library like so client.writeSingleCoil(6, true)

I get an error below (output is with DEBUG=rtu*)

Error logs

client.writeSingleCoil(6, true)
  rtu-client-request-handler registrating new request +0ms
Promise {
  <pending>,
  [Symbol(async_id_symbol)]: 61,
  [Symbol(trigger_async_id_symbol)]: 4
}
>   rtu-response-handler receiving new data +0ms
  rtu-response-handler buffer <Buffer 01 05 00 06 ff 00 6c 3b> +0ms
  rtu-response address 1 buffer <Buffer 01 05 00 06 ff 00 6c 3b> +0ms
  rtu-response-handler crc 15212 +3ms
  rtu-response-handler reset buffer from 8 to 0 +0ms
  rtu-response-handler not enough data available to parse +0ms
  rtu-client-request-handler new response coming in +22ms
  rtu-client-request-handler create crc from response <Buffer 01 05 00 06 00 00> +0ms
  rtu-client-request-handler CRC does not match 15212 !== 52013 +1ms
Uncaught UserRequestError {
  err: 'crcMismatch',
  message: 'the response payload does not match the crc',
  request: ModbusRTURequest {
    _address: 1,
    _body: WriteSingleCoilRequestBody { _fc: 5, _address: 6, _value: true },
    _corrupted: false,
    _crc: 15212
  },
  response: ModbusRTUResponse {
    _address: 1,
    _crc: 15212,
    _body: WriteSingleCoilResponseBody { _fc: 5, _address: 6, _value: 0 }
  }
}

One line stands out to me

rtu-client-request-handler create crc from response <Buffer 01 05 00 06 00 00>

Why it says 01 05 00 06 00 00? There should be ff 00 At the same time you can see that line below does indeed show correct data received.

rtu-response address 1 buffer <Buffer 01 05 00 06 ff 00 6c 3b>

The error is persistent and happens 100% of the time. The error says CRC error. Feels like data got mangled somewhere deep in the library.

yangit avatar Apr 20 '24 05:04 yangit