plcpeople / nodeS7

Node.JS library for communication to Siemens S7 PLCs
MIT License
356 stars 120 forks source link

Question: How to check the connection status? #117

Open DheemanthBhat opened 3 years ago

DheemanthBhat commented 3 years ago

Hi, How do we know that connection is still active before attempting to write data to PLC? I am currently using

if(s7Connection.isoConnectionState !== 4) {
  throw new Error("Not connected to PLC");
} else {
  s7Connection.writeItems(address, data, cb);
}

Should we call initiateConnection for every plc operation (read or write to data-blocks)? something like:

s7Connection.initiateConnection(s7ConnectionDetails, err => {
  if (err) {
    reject('Error connecting to PLC.');
  } else {    
    s7Connection.writeItems(addresses, data, writeStatus => {
      s7Connection.dropConnection(() => {
        resolve(writeStatus);
      });
    });
  }
});

Please help!