Creating and handling Tasks
This article explains how to create and handle Tasks via TwinCAT Automation Interface. It consists of the following topics:
- General information
- Inserting Tasks
- Inserting input/output variables
General information
There are two types of Tasks which can be configured in TwinCAT XAE and therefore also via Automation Interface: Tasks with Process Image and without. When you insert a new Task in TwinCAT XAE, you can decide whether you want to include a Process Image or not by selecting the corresponding checkbox in the "Insert Task" dialog.
As a result, the inserted Task either includes three more child nodes (Image, Inputs, Outputs) or not - as the following example shows (Task 1 = With Image, Task 2 = Without Image).
Inserting Tasks
To insert a Task via Automation Interface, you can make use of the ITcSmTreeItem::CreateChild() method and the corresponding SubTypes for "With Image" (SubType = 0) and "Without Image" (SubType = 1).
Code Snippet (C#)
ITcSmTreeItem tasks = systemManager.LookupTreeItem("TIRT");
tasks.CreateChild("Task 1 (With Image)", 0, null, null);
Code Snippet (Powershell):
$tasks = $systemManager.LookupTreeItem("TIRT")
$tasks.CreateChild("Task 1 (With Image)", 0, $null, $null)
Code Snippet (C#)
ITcSmTreeItem tasks = systemManager.LookupTreeItem("TIRT");
tasks.CreateChild("Task 2 (Without Image)", 1, null, null);
Code Snippet (Powershell):
$tasks = $systemManager.LookupTreeItem("TIRT")
$tasks.CreateChild("Task 1 (Without Image)", 1, $null, $null)
Inserting input/output variables
You can add Input/Output variables to process images (Task "With Image"), which can then be linked with I/O devices or variables from other Tasks. The corresponding dialog from TwinCAT XAE lets you choose e.g. the data type and address of the input/output variable in the process image. By clicking on "Ok", the variable will be added to the process image.
This procedure can also be triggered via Automation Interface by using the ITcSmTreeItem::CreateChild() method with the corresponding variable data type as vInfo. In this case the SubType specifies the "Start Address", as shown in the dialog above.
Code Snippet (C#):
ITcSmTreeItem task1 = systemManager.LookupTreeItem("TIRT^Task 1 (With Image)^Inputs");
task1.CreateChild("bInput", -1, null, "BOOL");
Code Snippet (Powershell):
$task1 = $systemManager.LookupTreeItem("TIRT^Task 1 (With Image)^Inputs")
$task1.CreateChild("bInput", -1, $null, "BOOL")
By using SubType = -1, TwinCAT automatically attaches the new variable at the end of the variable list.