Extension of a Function Block
General
The extension of a function block is based on the concept of inheritance in object-oriented programming. A derived function block "extends" a base function block and is then given the properties of the base function block in addition to its own properties.
The extension of a function block means:
The inherited function block contains all data and methods that are defined by the basic function block. You can use an instance of the basic function block in every context in which CODESYS expects a function block of the type of the basic function block.
The derived function block can overwrite the methods that you have defined in the base function block. This means that the inherited function block can define a method with the same name, the same inputs and the same output as is defined by the basic function block.
Hint: You have the following support when overwriting methods, actions, attributes, and transitions that are inherited by the base block. When you insert a method, action, etc. below an inherited block, the Add Object dialog includes a combo box with a list of methods, actions, etc. used in the base block. You can accept these and adapt them accordingly.
The derived function block must not contain function block variables with the same names as used by the base function block. The compiler reports this as an error.
The only exception: If you have declared a variable in the basic function block as
VAR_TEMP
, then the inherited function block may define a variable with the same name. In this case, the inherited function block can no longer access the variable of the basic function block.You can address the variables and methods of the base function block directly within the scope of the inherited function block by using the
SUPER
pointer.
Important
Multiple inheritance is not permitted.
Exception: A function block can implement multiple interfaces, and an interface can extend other interfaces.
Extending a base function block by a new function block
Requirement: The currently open project has a base function block (for example, POU_1(FB)), that will be extended by a new function block.
In the device tree, select the Application object and click .
The Add POU dialog opens.
Type the name for the new POU in the Name input field, for example POU_Ex.
Select Function block.
Click Extends and then click the
button.
In the Function Blocks category below Application in the Input Assistant, select the POU(FB) that will be used as the base function block (example:
POU_1
) and click OK.Optionally, you could also select an Access Modifier from the list box for the new function block.
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_Ex EXTENDS POU_1
The function block
POU_Ex
extends the basic function blockPOU_1
.
Extending a base function block by an existing function block
Requirement: The open project possesses a base function block (example: POU_1(FB)
) and another function block (example: POU_Ex(FB)
). The function block POU_Ex(FB)
is also to be given the properties of the basic function block. This means that POU_Ex(FB)
should extend POU_1(FB)
.
Double-click the function block
POU_Ex(FB)
in the device tree.The function block editor opens.
Extend the existing entry in the top line
FUNCTION_BLOCK POU_Ex
withEXTENDS POU_1
.The POU_Ex function block extends the
POU_1
base function block.
For more information, see: Implementation of an Interface and Extension of an interface
Input support when generating inheriting POUs
When you doing object-oriented programming and using the inheritance (EXTENDS
keyword) of POUs, you can get support as follows:
When you insert an action, a property, a method, or a transition below a POU derived from a base POU, the Add … dialog opens. Then the input field for the name extends to a list box. The list box contains a valid selection from the actions, properties, methods, or transitions available in the base POU. Now you can, for example, easily accept a method of the base POU and then adapt it to the derived function of the POU.
Methods and properties with the access modifier PRIVATE
are not listed here because they are also not inherited. Methods and properties with the access modifier PUBLIC
automatically get a blank access modifier field when accepting into the derived POU, which means the same thing functionally.
For more information, see: Property, Method, Transition, Action,
