Calling methods offered by another module via PLC
This article describes how a PLC can call a method that is provided by another module; in this case: the previously defined C++ module.
- Step 1: check the available interfaces.
- Step 2: create a new PLC project.
- Step 3: add a new FB state machine (which acts as the proxy that calls the C++ module methods).
- Step 4: clean up the function block interface.
- Step 5: add the FB methods "FB_init" and "exit".
- Step 6: implement the FB methods.
- Step 7: call the FB state machine in the PLC.
- Step 8: compile the PLC code.
- Step 9: link the PLC FB with the C++ instance.
- Step 10: observe the execution of both modules, PLC and C++.
Step 1: check available interfaces
Option 1:
- 1. Navigate to the C++ module instance.
- 2. Select the Interfaces tab.
- The IStateMachine interface is in the list with its specific IID (Interface ID).
Option 2:
- 1. Navigate to System.
- 2. Select the Interfaces tab.
- The IStateMachine interface is in the list with its specific IID (Interface ID).
The lower section shows the stored code in different programming languages.
Step 2: creating a new PLC project
A standard PLC project called "PLC-calling state machine" is created.
- 1. Right-click on the PLC node.
- 2. Select Standard PLC Project.
- 3. Adapt the name.
- The project has been successfully created.
Step 3: add a function block (FB) (which serves as the proxy for calling the C++ module methods)
- 1. Right-click on POUs.
- 2. Select Add->POU....
- 3. Define a new FB to be created, which will later act as a proxy for calling C++ classes: Enter the name of the new FB: "FB_StateMachine".
- 4. Select Function Block, then Implements and then click on the ... button.
- 5. Select the interface either via the Text Search tab or the Categories tab by deselecting Structured View.
- 6. Select IStateMachine and click on OK.
- The IStateMachine interface is then listed as the interface to be implemented.
- 7. Select Structured Text (ST) as Method implementation language.
- 8. Select Structured Text (ST) as implementation language.
- 9. End this dialog with Open.
- You have successfully added the FB.
Step 4: Customizing the function block interface
As a result of creating an FB that implements the IStateMachine interface, the wizard will create an FB with corresponding methods.
The FB_StateMachine makes 4 methods available:
- GetState
- SetState
- Start
- Stop
- 1. Delete Implements IStateMachine. Since the function block should act as proxy, it does not implement the interface itself. Therefore, it can be deleted.
- 2. Delete the methods TcAddRef, TcQueryInterface and TcRelease. They are not required for a proxy function block.
- The result is:
Step 5: add FB methods FB_init (Constructor) and FB_exit (Destructor)
- 1. Right-click on FB_StateMachine in the tree and select Add / Method…
- 2. Add the methods FB_exit and FB_init - both with Structured Text (ST) as the implementation language. They are available as predefined name.
- 3. Exit the dialog in each case by clicking on Open.
- In the end, all required methods are available:
Step 6: implement FB methods
Now all methods have to be filled with code.
Note | |
Missing attributes lead to unexpected behavior Attribute statements in brackets represent code to be added. |
More precise information on the attributes is given in the PLC documentation.
- 1. Implement the variable declarations of the FB_Statemachine. The FB itself does not require cyclically executable code.
- 2. Implement the variable declarations and the code area of the method FB_exit.
- 3. Implement the variable declarations and the code area of the method FB_init.
- 4. Implement the variable declaration and the code area of the method GetState (the generated pragmas can be deleted as they are not required for a proxy FB).
- 5. Implement the variable declaration and the code area of the method SetState (the generated pragmas can be deleted as they are not required for a proxy FB).
- 6. Implement the variable declaration and the code area of the method Start
(the generated pragmas can be deleted as they are not required for a proxy FB). - 7. Implement the variable declaration and the code area of the method Stop
(the generated pragmas can be deleted as they are not required for a proxy FB).
- The implementation of the FB_StateMachine, which acts as the proxy for calling the C++ module instance, is completed.
Step 7: call FB in the PLC
The FB_StateMachine is now called in the POU MAIN.
This simple sample acts as follows:
- Cyclic incrementation of a PLC counter nCounter
- If nCounter = 500, the C++ StateMachine is started with the state "1" in order to increment its internal C++ counter. Then read the state of C++ using GetState().
- If nCounter = 1000, the C++ state machine is set to the state "2" in order to decrement its internal C++ counter. Then read the state of C++ using GetState().
- If nCounter = 1500, the C++ StateMachine is stopped. The PLC nCounter is also set to "0", so that everything starts again from the beginning.
Step 8: compile PLC code
- 1. Right-click on the PLC project and click on Build.
- The compilation result shows "1 succeeded - 0 failed".
Step 9: link PLC FB with C++ instance
The benefits of all previous steps now become apparent:
The PLC FB FB_StateMachine can be configured with regard to linking with every instance of the C++ module StateMachine. This is a very flexible and powerful method of connecting PLC and C++ modules on the machine with each other.
- 1. Navigate to the instance of the PLC module in the left-hand tree and select the Symbol Initialization tab on the right-hand side.
- All instances of FB_StateMachine are listed; in this sample we have only defined one FB instance in POU MAIN.
- 2. Select the drop-down field Value and then the C++ module instance that is to be linked to the FB instance.
- PLC and C++ module are connected to each other.
Step 10: observe the execution of the two modules, PLC and C++
Following the activation of the TwinCAT configuration and the downloading and starting of the PLC code, the execution of the two codes, PLC and C++, is simple to observe:
- 1. After the Login and Start of the PLC project, the editor is already in online mode (left-hand side – see following illustration).
- 2. In order to be able to access online variables of the C++ module, activate the C++ debugging and follow the steps in the quick start in order to start the debugging (right-hand side of the following illustration).