Cloud-Automation / node-modbus

Modbus TCP Client/Server implementation for Node.JS
466 stars 174 forks source link

modbus rtu Request timed out #195

Closed LogicNode closed 6 years ago

LogicNode commented 6 years ago

Hello, I am having an issue trying to connect to salve device through Modbus RTU with ReadHoldingRegisters.js using the code below, I am getting the error: { '0': { err: 'Timeout', message: 'Request timed out' } } { '0': null }

Could you help please? thanks in advance!

let SerialPort = require('serialport')
let socket = new SerialPort('COM6', {
  baudRate: 19200,
  Parity: 'none',
  stopBits: 1,
  dataBits: 8
})
//set Slave PLC ID
let client = new modbus.client.RTU(socket , 1)

socket.on('close', function () {
  console.log(arguments)
})

socket.on('open', function () {
  console.log('opened');
  client.readDiscreteInputs(1,1)
    .then(function (resp) {
      console.log(resp)
      socket.close()
    }).catch(function () {
      console.error(arguments)
      socket.close()
    })
})

socket.on('data', function () {
  console.log(arguments)
})

socket.on('error', console.error)
LogicNode commented 6 years ago

Yes this is what i am looking for,thanks a lot .. I've learned so much about Modbus RTU because of this issue.. thanks again for your efforts :)

stefanpoeter commented 6 years ago

Thank you for your effort! RTU wasn't really used much. You helped a lot in making this library better.

stefanpoeter commented 6 years ago

@Mohammed-OceanPLX I updated the v3.0.1_dev branch with a working set of tests and some fixes. Would you test it with your device? Thanks.

LogicNode commented 6 years ago

Sorry for the delay,, here is the output: // note: register value in response was okay ( hex:1388 Dec: 5000) ! serialport:binding:auto-detect loading WindowsBinding +0ms serialport:main opening path: COM6 +0ms serialport:bindings open +0ms serialport:main _read queueing _read for after open +2ms serialport:main opened path: COM6 +84ms opened modbus-client issuing new read holding registers request +0ms rtu-client-request-handler registrating new request +0ms user-request creating new user request with timeout 5000 +0ms client-request-handler flushing +0ms client-request-handler flushing new request <Buffer 01 03 00 00 00 01 84 0a> +1ms serialport:main _write 8 bytes of data +5ms serialport:bindings write 8 bytes +92ms serialport:main _read reading +1ms serialport:bindings read +0ms serialport:main binding.write write finished +1ms client-request-handler request fully flushed, ( error: undefined ) undefined +4ms serialport:main binding.read finished +35ms modbus-client received data +42ms rtu-response-handler receiving new data +0ms rtu-response-handler buffer <Buffer 01> +1ms rtu-response address 1 buffer <Buffer 01> +0ms response-factory when NoSuchIndex Exception, the buffer does not contain a complete message +0ms response-factory RangeError: Index out of range response-factory at checkOffset (buffer.js:977:11) response-factory at Buffer.readUInt8 (buffer.js:1015:5) response-factory at Function.fromBuffer (F:\ocnplcx\nodeapps0\node_modules\jsmodbus\src\response\response-factory.js:19:23) response-factory at Function.fromBuffer (F:\ocnplcx\nodeapps0\node_modules\jsmodbus\src\rtu-response.js:14:32) response-factory at ModbusRTUClientResponseHandler.handleData (F:\ocnplcx\nodeapps0\node_modules\jsmodbus\src\rtu-client-response-handler.js:17:40) response-factory at ModbusRTUClient._onData (F:\ocnplcx\nodeapps0\node_modules\jsmodbus\src\modbus-client.js:37:27) response-factory at emitOne (events.js:121:20) response-factory at SerialPort.emit (events.js:211:7) response-factory at addChunk (_stream_readable.js:263:12) response-factory at readableAddChunk (_stream_readable.js:250:11) +0ms rtu-response-handler not enough data available to parse +1ms { '0': <Buffer 01> } serialport:main _read reading +5ms serialport:bindings read +41ms serialport:main binding.read finished +1ms modbus-client received data +6ms rtu-response-handler receiving new data +4ms rtu-response-handler buffer <Buffer 01 03 02 13 88 b5 12> +1ms rtu-response address 1 buffer <Buffer 01 03 02 13 88 b5 12> +6ms response-factory fc 3 payload <Buffer 03 02 13 88 b5 12> +6ms rtu-response-handler crc 769 +1ms rtu-response-handler reset buffer from 7 to 0 +4ms rtu-response-handler not enough data available to parse +1ms rtu-client-request-handler new response coming in +54ms (node:7832) UnhandledPromiseRejectionWarning: TypeError: "value" argument is out of bounds at checkInt (buffer.js:1185:11) at Buffer.writeUInt8 (buffer.js:1233:5) at F:\ocnplcx\nodeapps0\node_modules\jsmodbus\src\response\read-holding-registers.js:87:15 at Array.forEach () at ReadHoldingRegistersResponseBody.createPayload (F:\ocnplcx\nodeapps0\node_modules\jsmodbus\src\response\read-holding-registers.js:86:18) at ModbusRTUClientRequestHandler.handle (F:\ocnplcx\nodeapps0\node_modules\jsmodbus\src\rtu-client-request-handler.js:46:77) at ModbusRTUClient._onData (F:\ocnplcx\nodeapps0\node_modules\jsmodbus\src\modbus-client.js:51:30) at emitOne (events.js:121:20) at SerialPort.emit (events.js:211:7) at addChunk (_stream_readable.js:263:12) (node:7832) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:7832) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. client-request-handler flushing +5s client-request-handler no request to be executed +3ms { '0': { err: 'Timeout', message: 'Request timed out' } } serialport:main #close +5s serialport:bindings close +5s serialport:main binding.close finished +37ms { '0': null }

stefanpoeter commented 6 years ago

Since this special issue is solved, i am gonna close it. I'll need to test v3.0.1 a bit more and restart the munin server for long time observations. Can take a while, i hope you are fine with checking out the v3.0.1_dev branch in the meantime. Thanks for you commitment.