Sample02: Cyclic C++ logic, which uses IO from the IO Task
This article describes the implementation of a TC3 C++ module that uses an image of an IO Task.
Download
Here you can access the source code for this sample.
- 1. Unpack the downloaded ZIP file.
- 2. Using a Visual Studio with TwinCAT installed, open the project via Open Project ....
- 3. Configure signing for this project by switching on TwinCAT signing with a right-click on Project->Properties->Tc Sign and configure your certificate and password if necessary.
For more information on signing C++ projects, click here. - 4. Select your target system.
- 5. Build the sample (e.g. Build->Build Solution).
- 6. Activate the configuration by clicking on .
- The sample is ready for operation.
Source code, which is not automatically generated by the wizard, is identified with a start flag "//sample code" and end flag "//sample code end".
In this way you can search for these strings in the files, in order to get an idea of the details.
Description
This sample describes the flexibility of C++ code when working with IOs configured at the task. This approach enables a compiled C++ module to respond more flexibly, if a different number of IOs are linked to the IO task. One application option would be cyclic testing of analog input channels with a different number of channels, depending on the project.
The sample contains
- the C++ module TcIoTaskImageAccessDrv with a module instance TcIoTaskIMageAccessDrv_Obj1
- A "Task1" with an image, 10 input variables (Var1..Var10) and 10 output variables (Var11..Var20).
- They are linked: The instance is called by the task and uses the image of Task1.
The C++ code accesses the values via a data image, which is initialized during the transition from SAFEOP to OP (SO).
In the cyclically executed method CycleUpdate the value of each input variable is checked by calling the helper method CheckValue. If it is less than 0, the corresponding output variable is set to 1, if it is greater than 0, it is set to 2, if it is 0, the output is set to 3.
After activation of the configuration you can access the variables via the Solution Explorer and set them.
Double-click on the Task1 image of system for an overview.
The input variables can be opened and then set with the Online tab.