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

 Abstract: This article provides a detailed explanation of how to use TwinCAT 3 to automatically link variables using Attributes. It covers software and hardware version control within the TwinCAT 3 environment.  

Software Version: TwinCAT 3.1.4024.22
Sample Code Download: Available for reference

Usage Syntax of the Attribute 'TcLinkTo':

{attribute 'TcLinkTo' := ''} 

Implementation Details:
Using the TIID (TwinCAT I/O Device) configuration under Node I/O Configuration > I/O Devices, link the variables as shown in the figure. Add and declare the variables in the Main section, and after compilation, the program variables will automatically link to the I/O, indicated by a green icon (as shown in the figure). In contrast, a manually linked variable is displayed with a white icon.


If using a function block to link I/O, the method remains the same as described above. You need to first create the variables within the function block and then declare them in the Main section as follows:







To link variables by specifying the module number using TIIB (%d), as shown in the figure, you can designate the module position—for example, Term 3 corresponds to TIIB(3)—and then specify the channel. Below is the detailed approach:



To link variables by specifying the module name using TIIB[%s], such as TIIB[Term 3 (EL2008)] as shown in the figure, you can directly reference the module by its designated name in the I/O configuration. Below is the detailed approach:



The TwinCAT help documentation provides additional shortcuts to streamline the process of linking variables to I/O points.


Usage of Attribute 'TcNcAxis':
For linking a single axis, if no NC axis has been created in the current project, it will automatically create and link one.

Multi-Axis Linking:
Multi-axis linking is as shown in the figure below.