Skip to main content

CODESYS OPC UA Client API

Product: CODESYS OPC UA Client API SL

Description

The example shows how to use the OPC Client API library. The sample project contains an OPC UA Server configuration and an OPC UA Client configuration. Server/Client communication takes place via localhost.

Additional information

Preparation

  1. Connect to the controller. Use the following credentials:

    • Name: user1

    • Password: user1

    • The user1 must be a member of the Administrator group.

    Important

    You need to use these credentials for the example because the OPC UA Client signs in to the OPC UA Server with this data.

  2. Add the certificates for OPC UA Server and OPC UA Client to the trusted certificates.

    _example_img_certificates.png

    The certificate for UaExpert is required only if you want to connect to the OPC UA Server from an external UaExpert instance (for example, to get more server information).

Structure of the project

Server

  • Server: (GVL) Defines variables which are used for the symbol configuration of the OPC UA Server.

  • PLC_PRG_Server: Writes variables to GVL Server with a cyclical counter.

  • Symbol configuration: Defines the symbol configuration of the OPC UA Server. It contains all variables and symbols of the GVL server.

    OPC UA Server in the UaExpert program.

    _example_img_uacapi_uaexpert.PNG

Client

  • Client: (GVL) Defines all variables which are published by the subscription. Updated when the subscription is active and the publication is released.

  • UAVars: (GVL) Defines constants and variables which are used by the client to establish a connection to the OPC UA Server.

  • NamespaceArray (FB): Translation table for node IDs which are used on the application side. The function block is prepared only for optional use and is not used in the standard setup of the sample project.

  • PLC_PRG: Cyclical call of all function blocks used in the OPC UA Client API: Client, Subscription, Read, and Write.

  • ValueSet: Setup of values which are used by the Read and/or Write function blocks.

  • Subscription: Defines a subscription which publishes all variables of the GVL client.

Testing the project in online mode

Tip

Use a watch list to run the sample project.

  1. Set PLC_PRG.xEnable to TRUE.

    This allows the Client function block to establish a connection to the server.

    _example_img_uacapi_xEnable.PNG

    The status of CONNECTION_STATE changes to CONNECTED and the UAClient.xActive output becomes TRUE. Now the activation of a subscription, read actions, and write actions is possible.

  2. Set PLC_PRG.xSubscribe to TRUE.

    This allows the Subscription function block to create a subscription on the server and publish all values in GVL client variables.

    _example_img_uacapi_xSubscribe.PNG

    Make sure that xPublishingEnabled=TRUE so that SUBSCRIPTION_STATE becomes active.

  3. Set PLC_PRG.xRead to TRUE.

    This enables a read action which is done by the Read function block.

    _example_img_uacapi_xRead.PNG

    Switch PLC_PRG.xRead so that a new read action is triggered.

  4. Set PLC_PRG.xWrite to TRUE.

    This enables a write action by the Write function block.

    _example_img_uacapi_xWrite.PNG

    Switch PLC_PRG.xRead so that a new read action is triggered.

System requirements and restrictions

Programming system

CODESYS Development System (version 3.5.18.40 or higher)

Runtime system

CODESYS Control Win (version 3.5.18.40)

Add-on components

  • CODESYS OPC UA Client

  • CODESYS Communication

  • CODESYS Security Agent

Note

_example_icon.png DOWNLOAD Project