Sample13: Module communication: C-module calls PLC methods

This article describes how a TwinCAT C++ module calls a methods of a PLC function block via the TcCOM interface.

Download

System requirements: TwinCAT 3.1 Build 4020

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 Sample13: Module communication: C-module calls PLC methods 1:.
The sample is ready for operation.

Description

This sample provides for communication from a C++ module to a function block of a PLC by means of method call. To this end a TcCOM interface is defined that is offered by the PLC and used by the C++ module.

The PLC page as a provider in the process corresponds to the corresponding project of the TcCOM Sample 01, where an PLC is considered after PLC communication. Here a Caller is now provided in C++, which uses the same interface.

The PLC page adopted by TcCOM Sample 01. The function block registered there as TcCOM module offers the object ID allocated to it as an output variable.
It is the C++ module’s task to make the offered interface of this function block accessible.

A C++ project with a Cycle IO module is assumed.
1. In the TMC editor, create an interface pointer of the type I_Calculation with the name Calculationn). Later access occurs via this.
Sample13: Module communication: C-module calls PLC methods 2:
2. The Data Area Inputs have already been created by the module wizard with the type Input-Destination. Here in the TMC editor you create an input of the type OTCID with the name oidProvider, via which the Object ID will be linked from the PLC later. Sample13: Module communication: C-module calls PLC methods 3:
3. All other symbols are irrelevant for the sample and can be deleted.
The TMC-Code-Generator prepares the code accordingly.
In the header of the module some variables are created in order to carry out the methods calls later.
Sample13: Module communication: C-module calls PLC methods 4:
In the actual code of the module in CycleUpdate() the interface pointer is set using the object ID transmitted from the PLC. It is important that this happens in the CycleUpdate() and thus in real-time context, since the PLC must first provide the function block.
When this has taken place once, the methods can be called.
Sample13: Module communication: C-module calls PLC methods 5:
In addition, as can be seen above, the interface pointer is cleared when the program shuts down. This happens in the SetObjStateOS method.
4. Now build the C++ project.
5. Create an instance of the module.
6. Connect the input of the C++ module to the output of the PLC.
Sample13: Module communication: C-module calls PLC methods 6:
The project can be started. When the PLC is running, the OID is made known through the mapping to the C++ instance. Once this has occurred, the method can be called.