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 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).

Calling methods offered by another module via PLC 1:

Option 2:

1. Navigate to System.
2. Select the Interfaces tab.
The IStateMachine interface is in the list with its specific IID (Interface ID).
Calling methods offered by another module via PLC 2:

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.
Calling methods offered by another module via PLC 3:
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....
Calling methods offered by another module via PLC 4:
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.
Calling methods offered by another module via PLC 5:
5. Select the interface either via the Text Search tab or the Categories tab by deselecting Structured View.
Calling methods offered by another module via PLC 6:
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.
Calling methods offered by another module via PLC 7:
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:

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:
Calling methods offered by another module via PLC 8:

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…
Calling methods offered by another module via PLC 9:
2. Add the methods FB_exit and FB_init - both with Structured Text (ST) as the implementation language. They are available as predefined name.
Calling methods offered by another module via PLC 10:
3. Exit the dialog in each case by clicking on Open.
In the end, all required methods are available:
Calling methods offered by another module via PLC 11:

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.
Calling methods offered by another module via PLC 12:
2. Implement the variable declarations and the code area of the method FB_exit.
Calling methods offered by another module via PLC 13:
3. Implement the variable declarations and the code area of the method FB_init.
Calling methods offered by another module via PLC 14:
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).
Calling methods offered by another module via PLC 15:
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).
Calling methods offered by another module via PLC 16:
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).
Calling methods offered by another module via PLC 17:
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).
Calling methods offered by another module via PLC 18:
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:

Calling methods offered by another module via PLC 19:

Step 8: compile PLC code

1. Right-click on the PLC project and click on Build.
Calling methods offered by another module via PLC 20:
The compilation result shows "1 succeeded - 0 failed". Calling methods offered by another module via PLC 21:

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. Calling methods offered by another module via PLC 22:
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).
Calling methods offered by another module via PLC 23: