Implementing interfaces is based on the concept of object-oriented programming. With common interfaces, you can use different but similar function blocks the same way.
A function block that implements an interface has to include all methods and attributes that are defined in that interface (interface methods and interface attributes). This means that the name and the inputs and outputs of the methods or attributes must be exactly the same. When you create a new function block that implements an interface, CODESYS adds all methods and attributes of the interface automatically to the tree below the new function block.




NOTICE

If you add more interface methods afterwards, then CODESYS does not add these methods automatically to the affected function block. To perform this update, you must execute the “Implement Interfaces” command explicitly.
For inherited function blocks, you have to make sure that any methods or attributes that were derived through the inheritance of an interface also receive the appropriate implementation. Otherwise they should be deleted in case the implementation that was provided in the basis should be used. Respective compile error messages or warnings are displayed, prompted automatically by added pragma attributes. For more information, refer to the help page for the “Implementing Interfaces” command.




NOTICE

-
You must assign the interface of a function block to a variable of the interface type before a method can be called via the variable.
-
A variable of the interface type is always a reference of the assigned function block instance.
A variable of the interface type is a reference to instances of function blocks. This
kind of variable can refer to every function block that implements the interface.
If there is no assignment to a variable, then the variable in online mode contains
the value 0
.
Examples
The I1
interface contains the GetName
method.
METHOD GetName : STRING
The functions blocks A
and B
implements the interface I1
:
FUNCTION_BLOCK A IMPLEMENTS I1 FUNCTION_BLOCK B IMPLEMENTS I1
For this reason, both function blocks must include a method named GetName
and the return type STRING
. Otherwise the compiler reports an error.
A function includes the declaration of a variable of interface I1
type.
FUNCTION DeliverName : STRING VAR_INPUT l_i : I1; END_VAR
Function blocks that implement the I1
interface can be assigned to these input variables.
Examples of function calls:
DeliverName(l_i := A_instance); // call with instance of type A DeliverName(l_i := B_instance); // call with instance of type B
Calling of interface methods:
In this case, it depends on the actual type of l_i
whether the application calls A.GetName
or B.GetName
.
DeliverName := l_i.GetName();
Implementing an interface in a new function block
Requirement: The open project has at least one interface object.
-
Right-click “Application” in the device tree and select “Project Add Object POU”.
The “Add POU” dialog box opens.
-
Type the name for the new POU in the “Name” input field, for example “POU_Im”.
-
Select “Function block”.
-
Click “Implemented” and then the more button (
).
-
In the input assistant, select the interface from the category “Interfaces”, for example
ITF1
, and click on “OK”. -
To insert more interfaces, click
and select a another interface.
-
As an option, you can select an “Access modifier” for the new function block from the selection list.
-
Select from the “Implementation language” combo box (example: “Structured text (ST)”.
-
Click “Add”.
CODESYS adds the “POU_Ex” function block to the device tree and opens the editor. The first line contains the text:
FUNCTION_BLOCK POU_Im IMPLEMENTS ITF1
The interface and its methods and properties are now inserted below the function block in the device tree. Now you can type program code into the implementation part of the interface and its methods.
Implementing an interface in an existing function block
Requirement: The currently open project has a function block (example: “POU_Im”) and at least one interface object (example: “ITF1”).
-
Double-click the “POU_Ex(FB)” POU in the device tree.
The POU editor opens.
-
Extend the existing entry in the uppermost line
FUNCTION_BLOCK POU_Im
withIMPLEMENTS ITF1
The “POU_Im” function block implements the “ITF1” interface.
See also