node-omron-fins icon indicating copy to clipboard operation
node-omron-fins copied to clipboard

Patch to read and write to return the sid

Open gabrieleturchi opened this issue 6 years ago • 3 comments

To be able to track multiple parallel requests, I simply added: return self.header.SID; to FinsClient.prototype.read and FinsClient.prototype.write.

It is possible to have this patch in the code?

Thank you. GT

gabrieleturchi avatar Mar 01 '18 07:03 gabrieleturchi

client.read('D00000', 5, function (err, bytes) { console.log("Bytes: ", bytes); console.log("SID: ", client.header.SID); }); Please try this.

iceoneye avatar Mar 14 '18 15:03 iceoneye

Mhhh..... This way I can see two different problems: I need to use a callback that otherwise I don't need, and because I'm using a single client to run multiple different read & write, and I need the SID to be able to identify the correct answer, so the "client" object in the meantime could have changed.

gabrieleturchi avatar Mar 22 '18 15:03 gabrieleturchi

The way I handle SIDs without the need for callbacks or for adding a return is in the client.on('reply'). Here I am expecting 17 SIDs and on getting them all we close the connection. They often do not arrive in order therefore we need an if / else or switch statement.

client.on('reply',function(msg) {
  if(msg.sid==1) {
    // code to handle SID 1
  }
  else if(msg.sid==2) {
    // code to handle SID 2
  }
  else if(msg.sid==3) { 
    // code to handle SID 3
  }
  // etc
else if(msg.sid > 17){
    console.log("ERROR invalid SID: " + msg.sid);
    client.close();
    console.log("Return 10");
    return process.exit(10);
  }

  if(++replyCounter==17){
    client.close();
  }
}

CharlieMarshall avatar Jun 22 '20 09:06 CharlieMarshall