Object ‘Function Block’

A function block is a POU that supplies one or more values during execution.

The object is added to the application or the project using the command Project ‣ Add object ‣ POU . In the Device tree or in the POUs view the function block POUs have the suffix (FB).

You always call a function block via an instance that is a copy of the function block.

The editor of a function block consists of the declaration part and the implementation part.

After execution the values of the output variables and the internal variables are retained until the next execution. This means that the function block does not necessarily supply the same output values with the same input variables when called repeatedly.

In addition to the functionality described in IEC 61131-3, you can also use function blocks in CODESYS for the following functionalities of object-oriented programming:

The uppermost line of the declaration part contains the following declaration:

FUNCTION_BLOCK <access specifier> <function block> \| EXTENDS <function block> \| IMPLEMENTS <comma-separated list of interfaces>

Calling a function block

The call always takes place via an instance of the function block. When calling a function block only the values of the respective instance change.

Declaration of the instance:

<instance> : <function block>;

You access a variable of the function block in the implementation part as follows:

<instance>.<variable>

Hint

Please note the following:

  • From outside of the function block instance you can access only the input and output variables of a function block, not the internal variables.
  • Access to a function block instance is limited to the POU in which the instance is declared, unless you have declared the instance as global.
  • You can assign the desired values to the function block variables when calling the instance.

Example

Access to function block variables:

The function block FB1 has the input variable iVar1 of the type INT and the output variable out1. In the following the variable iVar1 is called from the program Prog.

PROGRAM Prog
VAR
inst1:FB1;
END_VAR

inst1.iVar1 := 33;  (* FB1 is called and the value 33 is assigned to the variable iVar1 *)

inst1();            (* FB1 is called, that's necessary for the following access to the output variable *)

ires := inst1.out1  (* the output variable out1 of the FB1 is read *)

In FBD:

Assigning variable values during the call:

In the text languages IL and ST you can directly assign values to the input and/or output variables when calling the function block.

The assignment of a value to an input variable takes place with :=

The assignment of a value to an output variable takes place with =>

Example

The instance CMD_TMR of the timer function block is called with assignments for the input variables IN and PT. Subsequently, the output variable Q of the timer is assigned to the variable A

CMD_TMR (IN := %IX5, PT := 100=);

A:= CMD_TMR.Q;

Note

If you insert a function block instance via the input assistant and the option Insert with arguments is activated in the Input Assistant dialog box, CODESYS inserts the call with all input and output variables. You then only need to insert the desired value assignment. In the above example CODESYS inserts the call as follows: CMD_TMR (IN:= ,PT:= , Q=> ).

See also