Object ‘Function Block’

A function block is a POU that yields one or more values when executed.

The object is added to the application or the project by clicking Project ‣ Add Object ‣ POU . In the device tree or in the POUs view, function block POUs have the (FB) suffix.

It always calls a function block by means of 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.

The values of the output variables and the internal variables remain unchanged after execution until the next execution. This means that the function block does not necessarily return the same output values for multiple calls with the same input variables.

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 top 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 is always made by means of an instance of the function block. When a function block is called, 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

Note the following:

  • You can access only input and output variables of a function block from outside the function block instance, not the internal variables.
  • Access to a function block instance is restricted to the POU in which the instance is declared, unless you have declared the instance globally.
  • You can assign the desired values to the function block variables when you call the instance.

Example

Access to function block variables:

The function block FB1 has the input variable iVar1 of 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 when calling:

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

A value is assigned to an input variable with := .

A value is assigned to an output variable with => .

Example

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

PROGRAM PLC_PRG
VAR
        CMD_TMR : TOF;
END_VAR

CMD_TMR(IN := %IX5.1, PT := T#100MS);
A := CMD_TMR.Q;

Note

When you insert a function block instance by means of the Input Assistant and select the Insert with arguments option in the Input Assistant dialog, CODESYS inserts the call with all input and output variables. Then you only have to insert the desired value assignment. In the example above, CODESYS inserts the call as follows: CMD_TMR (IN:= ,PT:= , Q=> ).

Note

You can use the attribute 'is_connected' and a local variable to determine at the time of the call in the function block instance whether or not a specific input receives an external assignment.

See also