Search code examples
node.jsraspberry-pi4modbusmodbus-tcp

PortNotOpenError while connecting to Modbus


I am trying to connect to Modbus TCP using the modbus-serial library for JavaScript. I have confirmed that I am using the correct IP address and port. My network doesn't have a firewall and if it does, using 2 other alternatives, I was able to connect my Modbus to the devices and make other devices do many things. However, I am still getting this error:

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

Here's my code:

const ModbusRTU = require("modbus-serial");
console.log("Imported Library");

const client = new ModbusRTU();
console.log("Defined Client");

client.connectTCP("", { port: });
console.log("Connected to Modbus TCP server");

console.log("");

client.writeCoil(2000, 0x01, function (err) {
  if (err) {
    console.error(err);
  } else {
    console.log("Coil successfully set!");
  }
});

I have removed the IP and port in this question.

I expected the code to turn on the A.C. in my hall, but it showed the error.

Also, the first app was too expensive so I couldn't use it and the second app was free, but it's graphics couldn't be customised and I had to resort to my current option, NodeJS (v20.10.0) and some addons to customise the view.

Also, I tried using both 2000 and 2001. I don't know the offset of this library (How to check it) ?

https://github.com/yaacov/node-modbus-serial/issues/115 - In this, they're using RS485. I'm using TCP IP.

Could this be due to me using Replit temporarily? Am I supposed to use another library of JS (Which library, how to use it?) ? Is my code incorrect (I found the code from the library and editted it) ?


Solution

  • When running code in Replit it will run on their servers not your PC. This means that it will be unable to directly access things on your LAN.