Creating and handling variable mappings

A very common scenario in which the TwinCAT Automation Interface is being used, is to automatically create variable mappings, e.g. between PLC input/output variables and their corresponding I/O counterparts. The Automation Interface provides several methods that simplify the task to create, delete or save variable mappings. The following documentation article briefly describes these methods and gives examples on how to use them. The following topics are covered:

General information

Variable mappings may occur between different input/output tree items in a TwinCAT project, for example:

The information in this article describes Automation Interface mechanisms which may be used for all of these use cases.

Link variables

From an Automation Interface point-of-view, variable mappings are always being performed between two tree items, e.g. between a PLC input/output variable and its corresponding I/O counterpart. To link two tree items, the Automation Interface provides the method ITcSysManager::LinkVariables() which links a given source tree item with a given destination tree item.

Code snippet (C#):

string source = "TIPC^PlcProj^PlcProj Instance^PlcTask Inputs^bIn";
string destination = "TIID^EtherCAT^EK1100^EL1004^Channel 1^Input";
systemManager.LinkVariables(source, destination);

Code snippet (Powershell):

$source = "TIPC^PlcProj^PlcProj Instance^PlcTask Inputs^bIn"
$destination = "TIID^EtherCAT^EK1100^EL1004^Channel 1^Input"
$systemManager.LinkVariables($source, $destination)

Unlink variables

Similar to linking variables, the Automation Interface provides a method ITcSysManager::UnlinkVariables() which releases the link between a given source tree item and a given destination tree item.

Code snippet (C#):

string source = "TIPC^PlcProj^PlcProj Instance^PlcTask Inputs^bIn";
string destination = "TIID^EtherCAT^EK1100^EL1004^Channel 1^Input";
systemManager.UnlinkVariables(source, destination);

Code snippet (Powershell):

$source = "TIPC^PlcProj^PlcProj Instance^PlcTask Inputs^bIn"
$destination = "TIID^EtherCAT^EK1100^EL1004^Channel 1^Input"
$systemManager.UnlinkVariables($source, $destination)

Save/Restore all variable mappings

To save or restore all variable mappings in a TwinCAT project, the methods ITcSysManager2::ProduceMappingInfo() and ITcSysManager2::ConsumeMappingInfo() can be used. The former reads all variable mappings in a TwinCAT project and returns them in an XML structure that can be re-imported later by using the latter method.

Code snippet (C#):

ITcSysManager2 systemManager2 = (ITcSysManager2)systemManager;
string mappingInfo = systemManager2.ProduceMappingInfo();
systemManager2.ConsumeMappingInfo(mappingInfo);

Code snippet (Powershell):

$mappingInfo = $systemManager.ProduceMappingInfo()
$systemManager.ConsumeMappingInfo($mappingInfo)

Delete all variable mappings

To delete all variable mappings in a TwinCAT project, the method ITcSysManager3.ClearMappingInfo() may be used.

Code snippet (C#):

ITcSysManager3 systemManager = (ITcSysManager3)systemManager;
systemManager.ClearMappingInfo();

Code snippet (Powershell):

$systemManager.ClearMappingInfo()