Cloud-Automation / node-modbus

Modbus TCP Client/Server implementation for Node.JS
456 stars 169 forks source link

client.writeMultipleRegisters(143, [1]) -> err: 'Timeout', #328

Closed Foxylabstory closed 10 months ago

Foxylabstory commented 10 months ago

Good day, I hope you are going well! I've meet situation where I'm trying to write multiple registers, registers are change, but I can't wait any answer. I have no guess why, could you explain me please, what needs to do?

client.writeMultipleRegisters(143, [1])
      .then((resp) => {
        console.log(resp);
      })
      .catch((err) => {
        console.log(err);
      });

It command returns:

  UserRequestError {
  err: 'Timeout',
  message: 'Req timed out',
  response: undefined
}

If I'm doing manually, there is no problem image

And you should to know, client.readHoldingRegisters(4096, 100).then().catch is working perfectly!

Foxylabstory commented 10 months ago

Also I've tried this issue:

let b = Buffer.allocUnsafe(2);
 b.writeUInt16BE(1);
client.writeMultipleRegisters(143, b)

It also doesn't work.

stefanpoeter commented 10 months ago

Could be anything. Can you provide a reproducable piece of code.

Foxylabstory commented 10 months ago

Here block that you've asked

const Modbus = require('jsmodbus');
const net = require('net');
const socket = new net.Socket();
const client = new Modbus.client.TCP(socket, 206);
const options = {
  'host': '217.118.178.146', // device ip real, wait for your actions
  'port': 4001,
  'autoReconnect': true,
  'logEnabled': true
};

socket.connect(options);

socket.on('connect', function () {
  console.log('socketconnected');

  setTimeout(() => {
    console.log(client.connectionState);

    client.readHoldingRegisters(11, 3) // 11 begining of reading, 3 number of reading. Working perfectly, thanks a lot.
      .then((resp) => {
        console.log(resp.response._body._values[0]); // expected slaveID of device - 206
        console.log(resp.response._body._values[1]); // expected serial number of device - 10998
        console.log(resp.response._body._values[2].toString(16)); // expected firmware of device - "1223"
      })
      .catch((err) => {
        console.log(err);
      });
  }, 500);

  setTimeout(() => {
    console.log(client.connectionState);
    client.writeMultipleRegisters(129, [1]) // should be only one number in range between 1 and 65535, I'll see when you will take attempt
      .then((resp) => {
        console.log(resp);
      })
      .catch((err) => {
        console.log(err); // every attempt returns -> "err": "Timeout", "message": "Req timed out"
      });
  }, 1000);
})

socket.on('error', function () {
  console.log('socketerror');
})

socket.on('timeout', () => {
  console.log('timeout');
})

socket.on('close', () => {
  console.log('socketclose');
  launchIntervalConnect();
});

socket.on('end', () => {
  console.log('socketend');
});
stefanpoeter commented 10 months ago

Don't see anything wrong, can you provide a tcpdump?

Foxylabstory commented 10 months ago

Thanks for your attention, problem was in module.exports, I've set new unit id via calling a function it went wrong because in imported modules was previous unit id. Reading tcpdump helped, we've found that answer contain wrong unit id. Solved with adding one more method, it will a garantie, that I'll receive updated instance of class.

function getClient() {
  return client;
}  
dyaacov commented 3 months ago

@Foxylabstory Can you please provide a full example? I have some devices that I also get timeout while in other it works fine

My code is connected to a single device, does your solution still relevant for me?