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

CRC calculation error for WriteSingleCoil response #338

Open
yangit opened this issue Apr 20, 2024 · 1 comment
Open

CRC calculation error for WriteSingleCoil response #338

yangit opened this issue Apr 20, 2024 · 1 comment

Comments

@yangit
Copy link

yangit commented Apr 20, 2024

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
Copy link
Author

yangit commented Apr 20, 2024

Found the issue:

This line creates value===true

const value = buffer.readUInt16BE(3) === 0xFF00

It is then sent to the WriteSingleCoilResponseBody constructor

return new WriteSingleCoilResponseBody(address, value)

Which is unable to correctly process boolean and instead tries to compare it agains 0xFF00

this._value = value === 0xFF00 ? 0xFF00 : 0x0000

yangit added a commit to yangit/node-modbus that referenced this issue Apr 20, 2024
…omation#338

The WriteSingleCoilResponseBody constructor declares that it can handle `boolen` but it can not do that.
So I have addled  boolean handling into the function.
yangit added a commit to yangit/node-modbus that referenced this issue Apr 20, 2024
…omation#338

The WriteSingleCoilResponseBody constructor declares that it can handle `boolen` but it can not do that.
So I have addled  boolean handling into the function.
yangit added a commit to yangit/node-modbus that referenced this issue Apr 20, 2024
Fix write-single-coil.ts incorrect processing of true value Cloud-Automation#338
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant