How to Use Attributes for Automatic Variable Linking in TwinCAT 3 PLC

Abstract
This article provides a detailed explanation of how to use Attributes in TwinCAT 3 to automatically link variables.

Software and Hardware Versions
Control Software
TwinCAT 3.1.4024.22
Sample Program Download


Usage of Attribute'TcLinkTo'

Syntax Format: {attribute 'TcLinkTo' := ''}
Used to link variables to the I/O configuration under I/O Devices using TIID node.
As shown in the figure, declare and add variables in the Main program. After compilation, the program variables will automatically link to the I/O, displayed with a green icon (as shown). Manually linked variables display a white icon.



For linking I/O using function blocks, the method is the same. First, create variables in the function block, then declare them in Main as shown below:



Specifying Modules with TIIB(%d)
Link by specifying the module number. For example, for Term 3, use TIIB(3) and specify the channel, as shown in the figure.



Specifying Modules with TIIB[%s]
Link by specifying the module name, e.g., TIIB[Term 3 (EL2008)], as shown in the figure.


Additional shortcuts are provided in the help documentation.


Usage of Attribute 'TcNcAxis'

Single Axis Linking
If no NC axis exists in the current project, one will be automatically created and linked.


Multi-Axis Linking

Multi-axis linking is shown in the figure below.