High Performance OPC UA Server SDK  1.3.1.248
Sample Client

The HP SDK Sample Clients demonstrates different OPC UA feature like

  • Server Discovery
  • Connecting and disconnecting from a server
  • Browsing the server address space
  • Reading values from the server
  • Method Calls
  • Creating subscriptions and monitored items
  • Modifying a subscription
  • Deleting a subscription
  • Recovering from connection errors

Usage

This application is a console application which support command line arguments and offers an interactive menu for triggering certain actions.

Command Line Arguments

The following command line arguments can be used to configure the client.

Usage: ./sample_client [-d debug_level] [-f facility_mask] [-c config_file] [-u <url>] [-l] [-h]
-d: sets the debug level bit mask of the trace output (default=ERROR)
-f: sets the facility bit mask of the trace output (default=ALL)
-c: sets the configuration file to use
-l: lists all debug_levels and facility_masks
-u: Sets the URL to use (default is to use the URLs from the configuration file)
-h: prints this help

Interactive Menu

The first menu allows connecting to the configured endpoint URL or to discover a server endpoints using the SDKs discovery functionality.

-------------------------------------------------------
- Press x to close client
-------------------------------------------------------
- Press 0 to start discovery at opc.tcp://localhost
- Press 1 to connect to opc.tcp://localhost

When the client is connected the following menu is displayed.

-------------------------------------------------------
- Press x to close client -
-------------------------------------------------------
- Press 3 to disconnect from server -
- Press 4 to browse server -
- Press 5 to read values -
- Press 6 to create a subscription -
- Press 7 to modify a subscription -
- Press 8 to delete a subscription -
- Press 9 to call a method -
-------------------------------------------------------

Connection State Machine

The SDK not only simplifies the OPC UA connecting process, it can also recover from connection errors automatically. To enable/disable the automatic reconnect feature you need to call ua_client_set_automatic_reconnect. By default, the feature is enabled.

The application gets informed about the current state by receiving the ua_client_connection_state_changed_cb callback. Callbacks can be registered by using ua_client_callbacks_init.

The following state machine diagram shows the possible states and transitions.

client_fsm_auto.png
Client FSM Automatic

Note that this FSM is active when automatic_reconnect is set to true. If this feature is disabled, the FSM shown below is active. In this case the application can decide how to handle connection loss

The SESSION_CREATED event informs the application, that a new session was created. This can either be the first session, or a new session when recovering from a connection loss.

When reaching the state CONNECTED, the application can create subscriptions or call any other OPC UA service. When the SDK recovers from a connection loss, it first tries to re-activate the last session. If this works, you get the status changes to SESSION_CONNECTED. If the last session does not exist anymore, the application gets informed by receiving the SESSION_DELETED event, immediately followed by SESSION_CREATED, which indicates that a new session was created. See next section for more information on recovering subscriptions.

client_fsm.png
Client FSM Manual

When automatic reconnects are disabled, the state changes to SESSION_DISCONNECTED on connection loss. Note the next to the ua_client_connection_state_changed_cb callback you receive also the ua_client_connection_error_cb callback to detect connection errors.

Subscription State Machine

When automatic reconnect is enabled the SDK also tries to recover the subscription if possible. The following scenarios are possible:

  • The last session could be recovered by the Connection State Machine. This means also the subscriptions are still active.
  • If the last session could not be recovered, the Connection State Machine creates a new session. When this happens, the Subscription State Machine tries to transfer the old subscription into the new session. This is possible if the subscription lifetime is longer than the session lifetime and the server supports the TransferSubscription service.
  • If this fails the application gets informed about the lost subscription and the application needs to re-create it.
client_sub_fsm.png
Subscription FSM

The CREATED state is a transient state which informs the application that a new subscription was created. When receiving this, the application needs to create the monitored items.

Connection Loss Detection

There are different ways how a connection loss can be detected. If the network card gets disabled or the cable is unplugged, on most systems this creates an immediate socket error, which is handled by the SDK and causes a connection loss event.

However, if the remote system does not answer anymore, e.g. because of a damaged cable, or an unplugged cable on the remote site, this cannot be detected immediately. In this case the SDK detects a timeout. Because the TCP timeout is too long for most applications, UA service calls have their own timeout.

The SDK can use the UA subscription mechanism and the Publish service to detect interrupted connections. If no subscription exists, and no service gets called by the application itself, then the SDK itself performs periodic watchdog Read service calls to detect a connection loss. The interval can be configured using ua_client_set_watchdog_interval. If the watchdog read does not return within the configured time, then the connection is considered broken. This timeout can be configured using ua_client_set_watchdog_timeout.