Implementation of an interface

The implementation of interfaces is based on the concept of object-oriented programming. Through common interfaces, you can use different but similar function blocks in a similar way.

A function block, which implements an interface, must contain all methods and properties that are defined in this interface (interface methods and interface properties). This means: The name, return type, inputs and outputs of the respective method or property must be exactly the same. If the element declarations in the interface and in the function block differ, or if the interface contains further elements, which are not included in the function block, the compiler reports an error.

For more information on the using an interface and an example, see section "Object Interface

When you create a new function block that implements an interface, TwinCAT automatically inserts all methods and properties of this interface under the new function block in the tree.

Implementation of an interface 1:

If you then add further methods or properties to the interface, TwinCAT does not automatically add these elements to the relevant function blocks. For the update, you must explicitly use the command Implement interfaces.

On executing this command the automatically created methods or properties will be provided with a pragma attribute that provokes compilation errors or warnings. This will support you in ensuring that automatically created elements do not inadvertently remain empty. For further information, please refer to the help page for the Command Implement interfaces.

Implementing an interface in a new function block

The currently open project has at least one interface object.
1. Select the PLC project object or a subfolder in the PLC project tree, and in the context select the command menu Add > POU...
The Add POU dialog opens.
2. Enter a name for the new function block in the Name input field, for example "FB_SampleImp“.
3. Select Function block.
4. Select Implements and click the Implementation of an interface 2: button.
5. In the input assistant, select the interface, for example "I_Itf1", from the Interfaces category and click OK.
6. To add another interface, click Implementation of an interface 3: again and select another interface.
7. Optionally you can select an Access specifier for the new function block from the selection list.
8. From the Implementation language selection list Select “Structured Text (ST)”, for example.
9. Click on Open.
TwinCAT adds the function block FB_SampleImp in the PLC project tree, and the editor opens. The first line says:
FUNCTION_BLOCK FB_SampleImp IMPLEMENTS I_Itf1
The methods and properties of the interface have now been inserted in the PLC project tree under the function block and you can now enter program code in the implementation section of the methods and properties.

Implementing an interface in an existing function block

The currently open project has a function block, for example "FB_SampleImp", and at least one interface object, for example "I_Itf1“.
1. In the PLC project tree double-click on the POU FB_SampleImp.
The POU editor opens.
2. Extend the existing entry in the top line, FUNCTION_BLOCK FB_SampleImp, with IMPLEMENTS I_Itf1.
The function block FB_SampleImp implements the interface I_Itf1.
3. Elements (methods or properties) that are defined in the interface, but not yet present in the function block, can be generated automatically in the function block by executing the command Implement interfaces, which is available in the context menu of the function block in the project tree.
The methods and properties of the interface have now been inserted in the PLC project tree under the function block and you can now enter program code in the implementation section of the methods and properties.

See also: