Cloud-Automation / node-modbus

Modbus TCP Client/Server implementation for Node.JS
456 stars 169 forks source link

rtu-client is offline if socket is open before construction #252

Closed hansmaad closed 4 years ago

hansmaad commented 4 years ago

If I pass an open SerialPort to Modbus.client.RTU all requests will fail, because the request handler missed the open event.

This happened to me the first time I tried to use jsmodbus. It's easy to workaround in normal cases but now I have the need to pass an open socket to multiple protocol handlers.

https://github.com/Cloud-Automation/node-modbus/blob/e57681a6e26990835533186d7baaeae143640f97/src/rtu-client-request-handler.js#L18-L25

Would it be possible to check socket.isOpen in the constructor to set this._state = 'online' ?

if (this._socket.isOpen) {
  this._onConnect();
} else {
  this._socket.on('open', this._onConnect.bind(this))
}
stefanpoeter commented 4 years ago

Hi @hansmaad,

i'll guess you are using jsmodbus v3? Seems like a valid request. If you prepare a pull request with the latest v3 version I'll check and merge it as soon as possible.

hansmaad commented 4 years ago

As it turned out... I'm currently on 4.0.0. But it doesn't seem to make a big difference regarding this issue...? What is the status of v4? Is it not really an official release?

stefanpoeter commented 4 years ago

v4 is an official release. You can patch it up there if you like.

alexbuczynsky commented 4 years ago

I opened a pull request that I believe addresses the issue @hansmaad is observing. If this is the case, @stefanpoeter can you look over this and then bump the version to 4.0.1?

stefanpoeter commented 4 years ago

If @hansmaad gives his ok I sure will :-)

hansmaad commented 4 years ago

@stefanpoeter Looks good!