Cloud-Automation / node-modbus

Modbus TCP Client/Server implementation for Node.JS
462 stars 173 forks source link

Negative number #102

Closed stachyc closed 7 years ago

stachyc commented 7 years ago

Hello, great project. Unfortunately isn't possible write negative number as follows:

client.writeMultipleRegisters(4, [-1, 20, 3, 4])....

buffer.js:1026 throw new TypeError('"value" argument is out of bounds'); ^

TypeError: "value" argument is out of bounds at checkInt (buffer.js:1026:11) at Buffer.writeUInt16BE (buffer.js:1095:5) at Object.writeMultipleRegisters (/work/iROBOT/node_modules/jsmodbus/src/handler/client/WriteMultipleRegisters.js:64:22) at Object. (/work/iROBOT/modbus_client.js:19:14) at Object.emit (/work/iROBOT/node_modules/jsmodbus/node_modules/stampit-event-bus/src/stampit-event-bus.js:20:38) at Object. (/work/iROBOT/node_modules/jsmodbus/src/modbus-tcp-client.js:53:12) at emitNone (events.js:91:20) at Socket.emit (events.js:185:7) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1072:10)

psorowka commented 7 years ago

the Array Notation assumes unsigned 16bit numbers as the input argument. If that is not what you need, you should rather compose the register content on your own and pass a Buffer as the argument. In your case that would probably mean to do something like:

let buf = Buffer.allocUnsafe(8) // Modbus registers are always words!
buf.writeInt16BE(-1, 0)
buf.writeInt16BE(20, 2) 
buf.writeInt16BE(3, 4)
buf.writeInt16BE(4, 6)
client.writeMultipleRegisters(4, buf)