Sample11a: Module communication: Method call C++ module to C++ module
This article describes how TwinCAT 3 C++ modules could communicate via method calls. The method protects the data with a critical section thus the access could be initiated from different contexts / tasks.
Download
Here you can access the source code for this sample:
https://github.com/Beckhoff/TC1300_Samples/tree/main/S11a-Mod2ModCS
- 1. Using a Visual Studio with TwinCAT installed, open the project via Open Project ....
- 2. 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.
Further information on signing C++ projects can be found in the chapter TwinCAT. - 3. Select your target system.
- 4. Build the sample (e.g. Build->Build Solution).
- 5. Activate the configuration by clicking
.
- The sample is ready for operation.
Description
The project contains three modules:
- The instance of the CModuleDataProvider class hosts the data and protects against access via the Retrieve and Store methods through a Critical Section.
- The instance of the module class CModuleDataRead reads the data from the DataProvider by calling the Retrieve method.
- The instance of the module class CModuleDataWrite writes the data from the DataProvider by calling the Store method.
The read/write instances are configured for access to the DataProvider instance, which can be seen in the Interface Pointer menu on the instance configuration.
The context (task), in which the instances are to be executed, can also be configured there. In this sample two tasks are used, TaskRead and TaskWrite.
The DataWriteCounterModul parameters of CModuleDataWrite and DataReadCounterModulo (CModuleDataRead) enable the moment to be determined, at which the module instances initiate the access.
CriticalSections are described in the SDK in TcRtInterfaces.h and are therefore intended for the real-time context.