Quick Start (TwinCAT 2)

The following chapter provides a quick start to the TwinCAT OPC UA Client. In these instructions, the PLC sample is used to establish a connection to an OPC UA server and read out a variable. Any server can be used as an OPC UA server, e.g. the TwinCAT OPC UA Sample Server, which can also be started as an executable file on a TwinCAT 2 system. Alternatively, you can also use the OPC Foundation's .NET Standard Sample Server, which can be obtained from the OPC Foundation's GitHub repository.

The following sample is based on the TwinCAT OPC UA Sample Server. The server does not necessarily have to be started in order to follow the individual steps. The aim of this quick start is to familiarize you with the PLC sample and learn which parts of the sample you need to adapt to your operating environment.

The action steps are described in more detail below in the order in which they are performed:

Quick Start (TwinCAT 2) 1:

TwinCAT OPC UA I/O Client

The TwinCAT OPC UA I/O Client is only available under TwinCAT 3. Under TwinCAT 2, we enable communication exclusively via the PLC library Tc2_PLCopen_OpcUa. This article has been written accordingly for the use of this library.

Address space of the Sample OPC UA server

After starting the server, its address space contains a number of nodes that can be read by a client. An OPC UA client requires the following information to establish a connection:

In this sample, the server can be reached under the following server URL and the endpoint "None/None". Anonymous" is used as the IdentityToken. The following screenshots were created based on the "UA Expert" software.

Quick Start (TwinCAT 2) 2:

After the connection has been established, the address space of the server used here is as follows:

Quick Start (TwinCAT 2) 3:

Identify the OPC UA node to be read

To read out a node, you need the specific address information, which you will find in the form of the so-called "attributes". The following attributes are required for reading a node:

In this sample, we want to read the "MyInt16" node. The attributes mentioned above are as follows:

Quick Start (TwinCAT 2) 4:

The PLCopen mapping specifies that an OPC UA node must be mapped from the data type "Int16" to the PLC data type "INT". We must take this into account in the following step.

You can use the NamespaceIndex for communication. However, best practice is to use the associated NamespaceName, as the NamespaceIndex can change dynamically. The NamespaceName, on the other hand, is static and enables a resolution in the NamespaceIndex at runtime. You will find the NamespaceName below the "Server" object and there in the so-called "NamespaceArray". The NamespaceName is then the xth entry in the array, i.e. the second entry here.

Quick Start (TwinCAT 2) 5:

Adjusting the PLC sample for the TwinCAT OPC UA Client

Open the PLC sample in TwinCAT PLC Control and open the PRG_Read_INT program. This program already contains a ready-made State Machine that uses the function blocks from the Tc2_PLCopen_OpcUa library to connect to an OPC UA server and read a node.

Quick Start (TwinCAT 2) 6:

Establishing the connection

The following variable values may now need to be adjusted to establish the connection:

This part of the PLC program therefore looks as follows:

Quick Start (TwinCAT 2) 7:

Resolve the NamespaceName

In the next step, we have to resolve the NamespaceName into the corresponding NamespaceIndex. We use the function block UA_GetNamespaceIndex for this. This function block receives the NamespaceName determined by us as input. In the PLC sample, set the value of the sNamespaceName variable to 'http://yourorganisation.org/TcOpcUaSampleServer/'.

Adjusting the address information

Now we can adjust the other address information (IdentifierType and Identifier) of the node in the PLC sample. To do this, we set the variables sNodeName and NodeID.eIdentifierType to the previously determined values. The declaration part of the program therefore looks as follows:

Quick Start (TwinCAT 2) 8:

And the implementation part:

Quick Start (TwinCAT 2) 9:

After these adjustments, you can activate the program. The variable value of the OPC UA node is then read by the server and stored in the PLC variable iReadData.