Sample08: provision of ADS-RPC
This article describes the implementation of methods that can be called by ADS via the 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.
Description
The download contains 2 projects:
- The TwinCAT project, which contains a C++ module. This offers some methods that can be called by ADS.
- Also included is a Visual C++ project that calls the methods from the User mode as a client.
Four methods with different signatures are provided and called. These are organized in two interfaces, so that the composition of the ADS symbol names of the methods becomes clear.
Understanding the sample
The sample consists of the TwinCAT C++ module, which offers the RPC methods and a C++ sample program that calls them.
TwinCAT C++ module
The TwinCAT C++ project contains a module and an instance of the module with the name "foobar".
RPC methods are normal methods that are described by interfaces in the TMC editor and are additionally enabled by an RPC enable checkbox. The options are described in greater detail in the Description of the TMC Editor.
In this module two interfaces are described and implemented, as can be seen in the TMC Editor:
The methods, four in all, have different signatures of call and return values.
Their ADS symbol name is formed according to the pattern: ModuleInstance.Interface#MethodName
Particularly important in the implementing module is the ContextId, which defines the context for the execution.
As can be seen in the C++ code itself, the methods are generated by the code generator and implemented like normal methods of a TcCOM module.
If the type information of the methods is to be available on the target system, the TMI file of the module can be transferred to the target system.
The TwinCAT OPC-UA server offers the option to call these methods also by OPC-UA – the TMI files are required on the target system for this.
C++ example client
Directly after starting, the C++ client will fetch the handles and then call the methods any number of times; however a [RETURN] is expected between the procedures. Every other key leads to the enabling of the handle and the termination of the program.
The outputs illustrate the calls: