Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect disconnection #33

Open
pcnate opened this issue Sep 13, 2018 · 3 comments
Open

Detect disconnection #33

pcnate opened this issue Sep 13, 2018 · 3 comments
Assignees
Labels
enhancement New feature or request new feature

Comments

@pcnate
Copy link
Contributor

pcnate commented Sep 13, 2018

Provide a disconnect event handler

Current Behavior

Disconnecting the PLC from the network does not get handled natively with the library. Attempting to read or write to it causes timeouts.

Expected Behavior

It would be nice to provide a disconnect callback to make it easier cleanup old connections and attempt to reconnect.

Possible Solution

    PLC.disconnect().then( () => {
      // do some connection cleanup, report offline, attempt reconnect etc
    });

Context

In order to detect whether the PLC is online, have to check for TIMEOUT errors thrown by _readTag, _writeTag, _readTagGroup, and _writeTagGroup but this requires actually reading or writing something. Some sort of defined watchdog type monitoring of the connecting would make code more concise and easier to understand.

@cmseaton42 cmseaton42 added enhancement New feature or request new feature labels Sep 14, 2018
@cmseaton42 cmseaton42 self-assigned this Sep 14, 2018
@kgustine
Copy link

In the mean time, can I get some advice for how to clean up a stale connection due to the PLC going offline? I've tried to just reconnect, but it fails due to timeout.

@cmseaton42
Copy link
Owner

@pcnate What method did you end up going with as a work around?

@kgustine
Copy link

kgustine commented Mar 2, 2020

I believe I solved a few problems related to this last week.

If your initial connection fails (or a reconnect attempt), you need to destory the socket via the native Socket destroy method. See the fix noted here #65 (comment)

If the connection has been established, but is then lost, you should call the Controller's destory method. Below is my actual code. I tested it with short resets of the EN2T ethernet card on the controller rack as well as long periods of being disconnected. This will reconnect when communication is reestablished. Note that this connect function is called after the controller object is created and I've subscribed to certain tags.

I have limited experience with sockets, so there may be hidden issues still. But this does recover from communication events and I believe addresses the TCP connection leak mentioned in the link above.

function connect() {
    PLC.connect(IP_ADDRESS, 0).then(() => {
        PLC.scan_rate = 1000;
        PLC.scan().catch(error => {
            PLC.destroy()
            setTimeout(connect, 5000)
        })
    }).catch(error => {
        net.Socket.prototype.destroy.call(PLC);
        setTimeout(connect, 5000)
    })
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request new feature
Projects
None yet
Development

No branches or pull requests

3 participants