Cloud-Automation / node-modbus

Modbus TCP Client/Server implementation for Node.JS
471 stars 175 forks source link

Modbus Serial errors #138

Closed DzmitryT closed 7 years ago

DzmitryT commented 7 years ago

Hello, I'm trying to use your library for testing of a couple of devices connected to RPi via FTDI-based USB-serial adapter and it seems there are some errors in examples provided. Also, there is critical bug in the modbus-serial-client.js. ` var onSend = function (pdu) { var crc = 0

  var base = Buffer.allocUnsafe(1)
  base.writeUInt8(1)
  var buf = Buffer.concat([base, pdu])

  for (var i = 0; i < buf.length; i += 1) {
    crc = (buf.readUInt8(i) + crc) % 0xFFFF
  }

  var crc16 = crc.crc16modbus(buf)`

Seems, this code should be replaced with something like that: ` var onSend = function (pdu) { var tmp = 0

  var base = Buffer.allocUnsafe(1)
  base.writeUInt8(1)
  var buf = Buffer.concat([base, pdu])

  for (var i = 0; i < buf.length; i += 1) {
    tmp = (buf.readUInt8(i) + tmp) % 0xFFFF
  }

  var crc16 = crc.crc16modbus(buf)`

Because an error occurred when function crc16modbus called on the number, not as method of the crc object:

TypeError: crc.crc16modbus is not a function

Regards, Dzmitry.

stefanpoeter commented 7 years ago

Hey @DzmitryT what version are u using?

DzmitryT commented 7 years ago

According to package.json, there is a latest version

"_from": "jsmodbus@latest", "_id": "jsmodbus@2.1.0",

stefanpoeter commented 7 years ago

I'll make an update. Thanks for the tip.

stefanpoeter commented 7 years ago

Tried to fix it with issue#138. I made a pull request for it. @DzmitryT can you check if it is gone?

DzmitryT commented 7 years ago

Thx, I'll try to check your update some hours later or tomorrow.

DzmitryT commented 7 years ago

@stefanpoeter, apologize for the late reply. Updated code seems to be working.