Skip to main content

Using a Dynamic Connection to an OPC UA Server

The settings for the communication of a OPC UA Client data source to an OPC UA Server can be dynamically configured from the IEC code and can also be changed at runtime. For this purpose, a structure is available in the DatasourceOpcUAServer library.

When using this kind of "dynamic connection", at runtime, for example, the server URL can be changed again if the device is located in a different network than at the time of configuration. It can also be used to configure detailed settings on the OPC UA connection.

. Requirement
  1. On the Communication tab of the data source, now select the Read Connection Settings from IEC Variable option.

  2. In static memory (for example, in a GVL or in a program in your project), create a variable of type DatasourceOpcUaServer.UserConfigurationV1.

    Example: config variable in the PLC_PRG program

    PROGRAM PLC_PRG
    VAR	
        config : DatasourceOpcUaServer.UserConfigurationV1 := (
    	        SessionName := 'MyCustomSession',		
                    EndpointUrl := 'opc.tcp://DevXY:4840',		
                    SecurityPolicyUri := DatasourceOpcUaServer.CmpOpcUaStack_Interfaces.OpcUa_SecurityPolicy_None);	
        value : INT;
    END_VAR
  3. In the input field below the Read Connection Settings from IEC Variable option, reference the variable created in Step 2:

    Example: PLC_PRG.config

    Now at any time you can fill the fields of this UserConfigurationV1 structure with values. For example, this can be done when starting the controller, from a configuration file, or from a visualization dialog.

    When filling the structure with setting values, the following applies:

    • The EndpointUrl setting always has to be defined.

    • All other settings can be assigned as desired. They allow, for example, the use of a different security mode than in the usual editor. Some settings, which are not available in the editor, can also be configured on the OPC-UA connection.

When all connection settings are configured, you can set the xValid parameter of the UserkonfigurationV1 to TRUE.

Example: In the implementation part of PLC_PRG:

config.SubscriptionPublishingInterval := config.SamplingInterval := TIME#2S;
config.xValid := TRUE;

Then the data source starts to establish the connection in the next cycle.

Changes to the settings while a connection is established have no effect and are only applied the next time a new connection is established.

When the xValid setting is set to FALSE, the data source automatically disconnects from the server and the settings can be changed again.

In the documentation view of the DatasourceOpcUAServer library, you can see an overview of the currently available setting parameters of the structure and their default values in the library manager. New versions of the data sources might provide new options.