Ad

JavaScript Net.connect() - Explicitly Set Local Endpoint IP Address

- 1 answer

I can't find a way to set the local endpoint field in the net.connect() function. I need to explicitly set the local endpoint to a specific IP address.

I am using the node-ads library to connect a node process to a PLC device.

In my example:

ads.tcpClient = net.connect(
    ads.options.port,
    ads.options.host,
    function () {
      ads.connected = true
      cb.apply(ads.adsClient)
    }
  )

The connect() function does not offer a parameter for this. The returning socket object also does not seem to have a field for the endpoint at all. What am I doing wrong?

The reason for this is that I am connecting to a Beckhoff PLC where the connection requires a differnt host IP address if another connection already exists on the same computer. The problem is mentionend here.

I know for a fact that it is possible in the Beckhoff-ADS C# implementation, as this code works:

 var socket = new AmsSocket(targetip, amsport)
 {
     LocalEndPoint = new IPEndPoint(IPAddress.Parse(sourceip), 0)
 };
Ad

Answer

UPDATE Solved the issue by doing the following:

  • Change your ads.js file to set the source IP address explicitly
  • It's better to not do this in node_modules, but to just copy the ads.js file somewhere where it does not get overwritten

Locate in ads.js:

var connect = function (cb) {
  var ads = this
  ads.tcpClient = net.connect({
    port: ads.options.port,
    host: ads.options.host,
    localAddress: ads.options.sourceIp,   // the part you have to add
  },

Pass the sourceIp in your options for the ADS connection:

 public options = {
        host: "192.168.100.1",
        amsNetIdTarget: "192.168.100.1.1.1",
        amsNetIdSource: "192.168.100.105.1.1",
        sourceIp: "192.168.100.105",
    };

Connect to your PLC:

const client = ads.connect(this.options, () => {
            console.log("Trying to connect.");
            client.readDeviceInfo((err, result) => {
                if (err) {
                    console.log("Error: " + err);
                }
                console.log("Connection result: " + JSON.stringify(result));
                client.end();
            });
        });

Simple enough, but unfortunately not supported in node-ADS.

Ad
source: stackoverflow.com
Ad