node.js - No connection to a PLC using modbus-serial - Stack Overflow

admin2025-04-18  4

I am trying to connect to a PLC using modbus TCP/IP (modbus-serial node on node.js)

This is crucial part of the code

const modbusClient = new ModbusRTU();

modbusClient.connectTCP("192.168.0.10", { port: 502 });
modbusClient.setTimeout(5000);
modbusClient.setID(1);

modbusClient.readHoldingRegisters(2, 1)
    .then(function(data) {
        console.log(data);
    });

All I get is this error

PortNotOpenError {
  name: 'PortNotOpenError',
  message: 'Port Not Open',
  errno: 'ECONNREFUSED'
}

The port is actually open. I can poll the data using modbus-tcp node in node-red on the same computer and it works. Am I missing anything?

转载请注明原文地址:http://www.anycun.com/QandA/1744968721a90179.html