AsOpcUac: clean error handling for real application

Hi all,

I could figure out how it works by doing several tests and wireshark traces. UA_Disconnect is not needed. The client takes care of automatic reconnection, creating new session Id after session expiration and transferring subscriptions of this new session. This is done without any programming needed!

1 Connection Monitoring

  • Once the client has established a connection to the server, it performs active connection monitoring.

  • Controlled by the parameter UASessionConnectInfo.MonitorConnection (interval in ms).

  • At each interval:

    • The client sends a ReadRequest for ServerStatus.State (NodeId = ns=0;i=2259).

    • The read timeout is set equal to the interval.

  • Example:

    • MonitorConnection = 2000 ms

    • A read occurs every 2000 ms.

    • If no response is received, detection of an aborted connection occurs after max. 2 × interval = 4000 ms.

2. Automatic Reconnection

  • If the connection is lost, the client runtime itself handles reconnection — the PLC application doesn’t need to intervene.

  • The client can always re-establish the connection as soon as the server is reachable again.

3. Session Expiry Case

  • If reconnection occurs before SessionTimeout has elapsed, the client reuses the existing session.

  • If reconnection occurs after SessionTimeout (server deleted the session due to inactivity):

    • The client automatically creates a new session.

    • It also re-creates all subscriptions and re-adds monitored items.

    • From the PLC application’s perspective, the session restoration is seamless.


:white_check_mark: Conclusion:
The AsOpcUa library handles connection monitoring and session recovery transparently, which simplifies PLC application code. The application only sees a temporary disconnect but does not need explicit logic for session recreation or subscription rebuild.

1 Like