Linking a PLC task to a program

In TwinCAT, a PLC program of type Program must be explicitly assigned to a PlcTask. This is done via CreateChild with the SubType 650. The name of the child object created must be identical to the program name.

Requirements

C# sample

// Create PLC project
ITcSmTreeItem plcNode = sysManager.LookupTreeItem("TIPC");
ITcSmTreeItem plcProject = plcNode.CreateChild("PLC1", 0, "", "Standard PLC Template");

// Create program POU (SubType 602 = Program)
ITcSmTreeItem pous = sysManager.LookupTreeItem(@"TIPC^PLC1^PLC1 Project^POUs");
pous.CreateChild("MyProg", 602);

// Assign program to PlcTask (SubType 650, name must match exactly)
ITcSmTreeItem task = sysManager.LookupTreeItem(@"TIPC^PLC1^PLC1 Project^PlcTask");
task.CreateChild("MyProg", 650);
Linking a PLC task to a program 1:

The SubType 650 for task assignment requires that the child name match the program name exactly. The program itself is created as a program POU using SubType 602. The path to the PlcTask node is TIPC^<ProjectName>^<ProjectName> Project^PlcTask.