Code generation
With the help of the automatic code generation, PLC variables for the OPC UA nodes configured in the I/O process image can be generated quickly and easily and linked to them automatically. This function is available in the configuration dialog of the I/O client via the button Create Plc Code. This function requires an existing PLC project in the current solution.

After calling this function, a new Global Variable List (GVL) with the name of the I/O client is created in the PLC project. Subsequently, all OPC UA nodes are read and corresponding variables are created in the GVL. Each variable receives the TcLinkTo attribute for an automatic linking with the corresponding variable from the I/O process image.
Example: In the I/O part of TwinCAT XAE a TwinCAT OPC UA I/O client with the name "Client 1" was created, to which various OPC UA nodes from a server were added.

After calling the code generation, a new GVL with the name "Client_1" was now created in the (already existing) PLC project. This then contains corresponding PLC variables for the individual nodes, which were then automatically linked via the TcLinkTo attribute.


In addition, the control variables from the respective I/O client are also created and linked as variables in the GVL (not shown in the screenshot above).
Code generation for structures
An OPC UA node, which represents a so-called StructuredDataType, is also considered by the code generation and a corresponding variable is created in the GVL. Since the StructuredDataType was created as a native data type in the TwinCAT type system, it can be handled like a normal structure.
Example: Two StructuredDataTypes from a server were added to the process image of the I/O client. The data types of the StructuredDataTypes on the server are ST_Complex1 and ST_Complex2 (not visible in the screenshot below).

The code generation has now created a corresponding GVL with two variables from the respective automatically generated TwinCAT data type, which corresponds to the respective StructuredDataType.

Code generation for methods
An OPC UA method has input and output parameters which are passed to or returned from the method accordingly. Furthermore, a method is a self-contained call; it must be explicitly started by the client. This methodology is mapped accordingly in the process image of the I/O client at the method and is also taken into account during code generation. For a method, unlike normal variables or structures, a separate function block is created, which is then referenced in the GVL.
Example: Two methods from a server have been added to the process image of the I/O client.

The code generation has now created a corresponding GVL, as well as two function blocks, which represent the input/output and control variables of the respective method.


