Object Function block

A function block is a POU, which returns one or several values when executed. The values of the output variables and the internal variables are retained until the next execution. This means that the function block may not return the same output values, if it is called repeatedly with the same input variables.

In the PLC project tree, function block POUs have the suffix (FB). The editor of a function block consists of the declaration part and the implementation part.

A function block is always called via an instance, which is a copy of the function block.

In addition to the functionality described in IEC 61131-3, in TwinCAT function blocks can also be used for the following object-oriented programming functionalities:

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>

Object Function block 1:

8-byte alignment

An 8-byte alignment was introduced with TwinCAT 3. Make sure that the alignment is appropriate if data are exchanged as an entire memory block with other controllers or software components (see Alignment).

Automatic creation of interface elements in a function block

There are two ways of automatically generating elements of an interface that implements a function block in this function block.

  1. If you specify an interface in the field Implements in the dialog Add when creating a new function block, TwinCAT automatically also adds the methods and properties of the interface to this function block.
  2. If an existing function block implements an interface, you can use the command Implement Interface in order to generate the interface elements in the function block. The command Implement Interface can be found in the context menu of a function block in the project tree.

See also:

Calling a function block

The call always takes place via an instance of the function block. If a function block is called, only the values of the respective instance change.

Declaration of the instance:

<instance> : <function block>;

A variable of the function block is accessed as follows in the implementation part:

<instance>.<variable>

Sample:

The function block FB_SampleA has the input variable nVar1 of type INT and the output variable nOut1. In the sample below, the variable nVar1 is called from the MAIN program.

ST:

PROGRAM MAIN
VAR
    fbSampleA : FB_SampleA;
END_VAR
fbSampleA.nVar1 := 33; (* FB_SampleA is called and the value 33 is assigned to the variable nVar1 *)
fbSampleA(); (* FB_SampleA is called, that's necessary for the following access to the output variable *)
nRes := fbSampleA.nOut1 (* the output variable nOut1 of the FB1 is read *)

FBD:

Object Function block 2:

Assigning variable values during a call:

In the text-based languages IL and ST, you can assign values directly to input and/or output variables when the function block is called.

A value is assigned to an input variable with :=

A value is assigned to an output variable with =>

Sample:

The instance fbTimer of the timer function block is called with assignments for the input variables IN and PT. The output variable Q of the timer is then assigned to the variable bVarA

PROGRAM MAIN
VAR
    fbTimer : TOF;
    bIn     : BOOL;
    bVarA   : BOOL;
END_VAR
fbTimer(IN := bIn, PT := t#300ms);
bVarA := fbTimer.Q;
Object Function block 3:

If you add a function block instance via the input assistant and the option Insert with arguments is enabled in the Input Assistant dialog, TwinCAT adds the call with all input and output variables. All you have to add is the desired value assignment. In the above example, TwinCAT adds the call as follows: CMD_TMR (IN:= , PT:= , Q=> ).

Object Function block 4:

Using the attribute 'is_connected' on a local variable, you can determine at the time of the call in the function block instance whether a particular input receives an assignment from outside.