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:
- Extending a function block (Extending a function block)
- Implementing interfaces (Implementing an interface)
- Methods (Object Method)
- Properties (Object Property)
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>
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.
- 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.
- 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:
- TC3 User Interface documentation: Command Implement interfaces
- Implementation of an interface
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>
- From outside the function block instance, you can only access 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 globally.
- You can assign the desired values to the function block variables when you call the instance.
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:
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;
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=> ). |
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. |