PLC

This section describes how to configure the namespace of the TwinCAT OPC UA Server in order to gain access to the symbols of a PLC project. This requires the following steps to be carried out:

Activating the symbol file

To make the symbols of a PLC project available via OPC UA, you must first activate the download of the symbol file (TMC) in the properties of the PLC project.

PLC 1:

This means that the symbol file is automatically transferred to the corresponding target device when the project is activated and is then available there in the TwinCAT boot directory. The name of the symbol file is based, for example, on the ADS port (see below) of the PLC runtime:

%TwinCATInstallDir%\3.1\Boot\Plc\Port_851.tmc
PLC 2:

Quick start

If the TwinCAT OPC UA Server is also installed on the same device, it always automatically reads the symbol file of the first PLC runtime in the delivery state, i.e. no further settings are required on the server. Activating the project thus transfers the symbol file to the target device and a subsequent TwinCAT restart also restarts the TwinCAT OPC UA Server. This will now find the symbol file in the boot directory during the boot process and read it. The server recognizes which symbols it should provide in its address space through the set pragmas.

Enabling symbols

In TwinCAT 3 PLC, symbols are enabled for OPC UA via so-called pragmas. Such a pragma was also used in the Quick Start Tutorial, for example, to enable a variable for OPC UA. The pragma is inserted before the symbol to which it applies. The following pragma can be used to enable a variable, array, or structure for OPC UA. The TwinCAT OPC UA Server recognizes this pragma when reading the symbolism and imports the corresponding symbol into its namespace. The corresponding type information is transferred and mapped to OPC UA.

{attribute 'OPC.UA.DA' := '1'}
nMyCounter : INT;

Additional pragmas may be required for individual sub-functions of data access, for example structures, properties, AnalogItemType, StatusCode, setting a description, Alias or the read-only flag of a node. The additional pragmas are then inserted in a separate line, for example:

{attribute 'OPC.UA.DA' := '1'}
{attribute 'OPC.UA.DA.Access' := '1'}
nMyCounter : INT;

(Sets the read-only flag for this variable)

The pragma for enabling a symbol is automatically inherited to all child symbols. If you want to block inheritance from a certain point, for example from a certain member of a structure, you can work with the following pragma:

{attribute 'OPC.UA.DA' := '0'}
stChild : ST_ChildStruct;

In the case of function blocks and structures, the definition location of the pragma is decisive. If the pragma is defined on an instance, only this instance (and all child elements) is enabled for OPC UA. If, on the other hand, the pragma is defined at the function block or structure definition, all instances of the function block or structure are enabled. In the following sample, the two instances fbTest1 and fbTest2 of the function block FB_BLOCK1 are to be made available via OPC UA. When an entire instance is enabled, all its symbols are also available via OPC UA. The PLC program looks like the following:

PROGRAM MAIN
VAR
  {attribute 'OPC.UA.DA' := '1'}
  fbTest1 : FB_BLOCK1;
  fbTest2 : FB_BLOCK1;
END_VAR
FUNCTION_BLOCK FB_BLOCK1
VAR_INPUT
  {attribute 'OPC.UA.DA' := '1'}
  ni1 : INT;
  ni2 : INT;
END_VAR
VAR_OUTPUT
  {attribute 'OPC.UA.DA' := '1'}
  no1 : INT; 
  no2 : INT;
END_VAR
VAR
  {attribute 'OPC.UA.DA' := '1'}
  nx1 : INT;
  nx2 : INT;
END_VAR

The instance fbTest1 now receives the pragma, which means that all the symbols it contains are also automatically enabled for OPC UA, i.e. fbTest.ni1, fbTest.ni2, etc. The instance fbTest2, on the other hand, does not receive the pragma, but the three variables ni1, no1 and nx1 it contains are marked with the pragma. They are therefore available in all instances via OPC UA.

Configuration of the server

You can now use the TwinCAT OPC UA Configurator to configure the TwinCAT OPC UA Server so that it reads the generated symbol file and makes the PLC runtime available as a data access device in its address space.

To do this, add a new device in the Data Access tab of the TwinCAT OPC UA Configurator. Select the AMS Net ID of your device and enter the ADS port of the PLC runtime. You can find the ADS port in the properties of the PLC project:

PLC 3:

Now select the entry "TwinCAT 3 PLC (TMC) - Filtered" from the selection list in the "Type" field. In the "SymbolFile" field, select the symbol file (TMC) that was created after activating the TwinCAT PLC project and should now be located in the boot directory. Alternatively, you can also use the online symbolism.

PLC 4:

You can leave all other settings at their default values.