Skip to main content

Using a Dynamic Connection to an OPC UA Server

The settings for the communication between a data source (OPC UA Client) and 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 you use this kind of "dynamic connection", the server URL can be changed again at runtime. This may be necessary, for example, if the device was on a different network at the time of configuration. Furthermore, detailed settings are possible for the OPC UA connection.

Procedure. Extending the IEC code

Requirement: You have created an OPC UA Client data source in the CODESYS project. For more information, see the following: Establishing the Connection of a Data Source OPC UA Client to an OPC UA Server

  1. In your project, declare a variable of type DatasourceOpcUaServer.UserConfigurationV1 in static memory. This is possible, for example, in a GVL or in a program.

    Variable config in POU PLC_PRG

    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
  2. Open the editor of the data source and select the Communication tab.

  3. In the input field, reference the variable PLC_PRG.config of the Read Connection Settings from IEC Variable option.

    Screenshot of the tab

    _comm_use_lib_DatasourceOpcUAServer.png
  4. Now populate the structure with setting values. Note that you can populate the fields of the UserConfigurationV1 structure with values at any time. For example, this can be done when starting the controller, from a configuration file, or from a visualization dialog.

    However, the EndpointUrl setting always has to be defined.

    All other settings can be freely assigned. In this way, for example, it is possible to use a different security operating mode than in the usual editor. Furthermore, some settings, which are not available in the editor, can be configured on the OPC-UA connection.

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

    In the implementation part of PLC_PRG:

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

    In the next cycle, the data source establishes the connection. As long as a connection exists, changes to the settings will have no effect. They will only be transferred 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.