TwinCAT 3 C++ module providing methods

This article describes the creation of a TwinCAT 3 C++ module that provides an interface with several methods that can be called by a PLC and also by other C++ modules.

The idea is to create a simple state machine in C++ that can be started and stopped from the outside by other modules, but which also allows the setting or reading of the particular state of the C++ state machine.

Two further articles use the result from this C++ state machine.

This article describes:

Step 1: create a new TwinCAT 3 project

First of all, create a TwinCAT project as usual.

TwinCAT 3 C++ module providing methods 1:

Step 2: create a new TwinCAT 3 C++ driver

1. Right-click on C++ and Add New Item…
TwinCAT 3 C++ module providing methods 2:
2. Select the template TwinCAT Driver Project and enter a driver name, "StateMachineDrv" in this sample. Click on Add to continue.
TwinCAT 3 C++ module providing methods 3:
3. Select a template to be used for this new driver. In this sample "TwinCAT Module Class with Cyclic IO" is selected, since the internal counter of the state machine is available for assigning to the IO.
4. Click on Add to continue.
TwinCAT 3 C++ module providing methods 4:
5. Specify a name for the new class in the C++ driver "StateMachineDrv".
The names of the module class and the header and source files are derived from the specified "Short Name".
6. Click on OK to continue.
TwinCAT 3 C++ module providing methods 5:
The wizard then creates a C++ project, which can be compiled error-free.

Step 3: create a new TwinCAT 3 interface

Note

Name conflict

A name collision can occur if the driver is used in combination with a PLC module.
Do not use any of the keywords that are reserved for the PLC as names.

1. Start the TMC editor by double-clicking on StateMachineDrv.tmc.
TwinCAT 3 C++ module providing methods 6:
2. Select Data Types within the TMC editor.
3. Add a new interface by clicking on Add a new interface TwinCAT 3 C++ module providing methods 7:.
A new entry IInterface1 is then listed.
4. Open IInterface1 by double-clicking in order to change the properties of the interface.
5. Enter a meaningful name - in this sample "IStateMachine".
TwinCAT 3 C++ module providing methods 8:
The interface has been created.

Step 4: add methods to the interface

1. Click on Edit Methods... to get a list of the methods of this interface:
Click on the + button to create a new default method, Method1.
2. Replace the default name Method1 by a more meaningful name, in this sample "Start".
TwinCAT 3 C++ module providing methods 9:
3. Add a second method and name it "Stop".
TwinCAT 3 C++ module providing methods 10:
4. Add a third method and name it "SetState".
5. Subsequently, you can add parameters by clicking on Add a new parameter or edit parameters of the SetState method.
TwinCAT 3 C++ module providing methods 11:
The new parameter, Parameter1, is generated by default as Normal Type INT.
6. Click on the name "Parameter1" and change the name in the edit box to "State".
7. After Start, Stop and SetState have been defined, define a further method.
8. Rename it "GetState".
9. Add a parameter and name it "pState" (which is conceived to become a pointer later on).
10. Change Normal Type to Is Pointer.
TwinCAT 3 C++ module providing methods 12:
You then obtain a list of all methods. You can change the order of the methods with the TwinCAT 3 C++ module providing methods 13: buttons.
TwinCAT 3 C++ module providing methods 14:

Step 5: add a new interface to the module

1. Select the module that is to be extended by the new interface - in this case select the destination Modules->CStateMachineModule.
2. Extend the list of implemented interfaces by a new interface with Add a new interface to the module by clicking on the + button.
TwinCAT 3 C++ module providing methods 15:
3. All available interfaces are listed - select the new interface IStateMachine and end with OK.
TwinCAT 3 C++ module providing methods 16:
The new interface IStateMachine is part of the module description.
TwinCAT 3 C++ module providing methods 17:

Step 6: Start the TwinCAT TMC Code Generator

1. In order to generate the C/C++ code on the basis of this module, right-click in the C/C++ project and then select the TwinCAT TMC Code Generator.
TwinCAT 3 C++ module providing methods 18:
The module StateMachineModule.cpp now contains the new interfaces
CModule1: Start()
CModule1: Stop()
CModule1: SetState(SHORT State)
CModule1: GetState(SHORT* pState).

Step 7: implementation of the member variables and the constructor

Add the member variables to the header file StateMachineModule.h.

TwinCAT 3 C++ module providing methods 19:

Step 8: implementation of methods

Implement the code for the four methods in the StateMachineModule.cpp:

TwinCAT 3 C++ module providing methods 20:

Step 9: implementation of a cyclic update

The C++ module instance is cyclically called, even if the internal state machine is in Stop mode.

TwinCAT 3 C++ module providing methods 21:

.

Step 10: compilation of code

1. Following the implementation of all interfaces, compile the code by right-clicking on the state machine and selecting Build.
TwinCAT 3 C++ module providing methods 22:
2. Repeat the compilation and optimize your code until the result looks like this:
TwinCAT 3 C++ module providing methods 23:

Step 11: creating an instance of the C++ module

1. Right-click on the C++ project and select Add New Item... to create a new module instance.
TwinCAT 3 C++ module providing methods 24:
2. Select the module that is to be added as a new instance – in this case CStateMachineModule.
TwinCAT 3 C++ module providing methods 25:
3. Assign the instance to a task:
TwinCAT 3 C++ module providing methods 26:

Step 12: finished - check the result

1. Navigate to the module listed in the solution tree and select the Interfaces tab on the right-hand side.
The new interface IStateMachine is listed. TwinCAT 3 C++ module providing methods 27: