Implementation of an Interface
The implementation of interfaces is based on the concept of object-oriented programming. Using shared interfaces, you can implement different but similar function blocks in a similar way.
A function block which implements an interface must implement its own code for all methods and properties defined with this interface.
When you define a new function block which implements an interface, the methods and properties of this interface are automatically inserted below the new function block in the object tree. However, not all methods and properties are inserted, but only those which are required for an error-free compile.
If you add more methods to an interface at a later time, then these methods are not automatically included in the declaration of the function blocks which implement the interface. In order to update the implementation there, you need to call the Implement Interface command. To override individual methods, select the Add Object → Method command. The dialog opens with a list of all overridable methods. For a selection, click in the Name list box.
In the case of derived function blocks, you need to make sure that methods or properties, which are inherited from an interface via EXTENDS, are correctly implemented. When no custom implementation is provided, the function block automatically uses the basic implementation of the superordinate or a related function block.
For more information, see the following: Updating an interface, Method
Using interface variables
Important
A function block instance with a type-compliant interface must be assigned to a variable defined as type Interface. You can call methods via this variable only after this.
A variable of the interface type is always a reference to the assigned function block instance.
An interface variable serves as a reference to a function block instance. It can reference any instance which implements this interface. The specific instance assigned can vary at runtime. Until the first assignment, the variable contains the value 0.
The I1 interface contains the GetName method.
The A and B function blocks implement the interface I1:
METHOD GetName : STRING
FUNCTION_BLOCK A IMPLEMENTS I1 FUNCTION_BLOCK B IMPLEMENTS I1
Both function blocks can include a method named GetName and the return type STRING.
A function includes the declaration of a variable of interface I1 type.
FUNCTION DeliverName : STRING
VAR_INPUT
l_i : I1;
END_VARFunction 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 project which is currently open has at least one interface object.
In the device tree, select the Application object and click the Project → Add Object → POU menu command.
The Add POU dialog opens.
Enter a name for the new function block in the input field Name, e.g. POU_Im.
Select the type Function block.
Click the Implements attribute and click the
button.In the Input Assistant, select the Interfaces category and then, for example, the interface
ITF1. After that, click OK.If you want to insert another interface, click
again. Next, select the desired interface.Optionally, you could also select an Access specifier from the list box for the new function block.
Make a selection from the Implementation language list box (example: Structured text (ST).
Click the Add button.
CODESYS adds the POU_Ex function block with the interfaces 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 in the device tree below the function block. You can now enter program code in 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 POU_Ex(FB) in the device tree.
The editor of the POU opens.
Extend the existing entry in the top line
FUNCTION_BLOCK POU_ImwithIMPLEMENTS ITF1.The POU_Im function block implements the ITF1 interface.
Updating an interface
If you add more methods and properties to an already defined interface at a later time, then the function blocks which implement this interface are not automatically extended to include the new methods. You need to manually update the function blocks which should get an interface implementation.
To do this, select the Implement interfaces command to insert the new objects.
For more information, see the following: Implement interfaces
Overriding methods
In scenarios with IMPLEMENTS (interfaces) and EXTENDS (inheritance), methods which have already been declared in the interface may need to be reimplemented in derived function blocks in order to adapt or override them.
To override individual methods, select the Add Object → Method command. The dialog opens with a list of all overridable methods so that you can select the desired method which should get its own implementation. This will be added and you can supplement the implementation.
For more information, see the following: Dialog: Add Method