Extending Function Blocks

The extension of a function block is based on the concept of inheritance in object-oriented programming. An inherited function block thereby extends a basic function block and in doing so is given the properties of the basic 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 inherited function block is permitted to overwrite the methods that you have defined in the basic 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.

    Tip: 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 box 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 inherited function block is not permitted to have any function block variables with the same name as used for the base function block. The compiler reports this as an error.

    Sole 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 directly address the variables and methods of the basic function block within the scope of the inherited function block by using the SUPER pointer.

Attention

Multiple inheritance is not permitted.

See also

Extending a basic function block by a new function block

Requirement: The open project possesses a base function block (example: POU_1(FB)) that will be extended by a new function block.

  1. Right-click the Application object in the device tree and select Project ‣ Add Object ‣ POU.

    The Add POU dialog box opens.

  2. Type the name for the new POU in the Name input field, for example POU_Ex.

  3. Select Function block.

  4. Click Advanced and then the more button ( 125bc846ea1a9450c0a8640e0123a187_21d51bd0cd4519c1c0a8640e00abb4b7 ).

  5. In the category Function blocks under Application in the input assistant, select the POU(FB) that is to serve as the basic function block, for example POU_1, and click on OK.

  6. As an option, you can select an Access modifier for the new function block from the selection list.

  7. Select from the Implementation language combo box (example: Structured text (ST).

  8. 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 block POU_1.

Extending a basic 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, which means: POU_Ex(FB)is to extend POU_1(FB).

  1. Double-click on the POU POU_Ex(FB) in the device tree

    The POU editor opens.

  2. Extend the existing entry in the uppermost line FUNCTION_BLOCK POU_1 with EXTENDS POU_1

    The function block POU_Ex extends the basic function block POU_1