Function Block

A function block is a POU (program organization unit), which returns one or several values when executed.

It is similar to a class in any programming language which can be instantiated and executed with a different value.

Download sample

Function block in the structured text has a similar meaning. Let’s say we have 20 similar pumps in our plant. If we want to model these pumps then we can create 1 function block and then make 20 instances of that function block. So we don’t need to model 20 times. It is similar to a class in C++ where we can make an instance and give the input and get the output.

Object Action:

An action can be used to implement further program code. You can implement this program code in a language that is different from the basic implementation. The basic implementation is the function block or the program under which you have inserted the action. An action does not have its own declarations and uses the data of the basic implementation. This means that the action uses the input/output and local variables of the basic implementation.

Object Method:

Methods are an extension of the IEC 61131-3 standard and a tool for object-oriented programming that is used for data encapsulation. A method contains a declaration and an implementation that includes a series of statements. However, unlike a function, a method is not an independent POU, and it is assigned to a function block or program. You can use interfaces for the organization of methods.

//Declare a method
METHOD TestMethod : REAL
VAR_INPUT
  A           : REAL;
  B           : REAL;  
END_VAR
//Body of the method
TestMethod := A + B;   
//Calling the method
functionMethodResult := myFnBlock.TestMethod(12.0, 23.0);  // functionMethodResult is 35.0

Function Block Example

Function block has input, output  and local variable. Input variables can be passed when calling the function block and the input can be assigned.

//Header of the function block

FUNCTION_BLOCK TESTFunctionBlock
VAR_INPUT
input1FnBool  : BOOL ;
input2FnReal  : REAL ;
END_VAR
VAR_OUTPUT
output1FnBool  : BOOL ;
output2FnReal  : REAL ;
END_VAR
VAR
value1LocalFnBool : BOOL;
END_VAR
VAR_INPUT PERSISTENT
value1RefFnBool : BOOL := TRUE;
value2RefFnReal : REAL := -9.99;
END_VAR

//Body of the function block

IF value1RefFnBool THEN	
output1FnBool := NOT input1FnBool;
output2FnReal :=  input2FnReal * 2.0 ;
ELSE
output1FnBool :=  input1FnBool;
output2FnReal :=  input2FnReal * 1.0 ;
END_IF

//Calling the function block Main program

PROGRAM MAIN
VAR //declare few variable we can assign fn block output to those
input1BoolMain  : BOOL ;
input2RealMain  : REAL ;
output1BoolMain  : BOOL ;
output2RealMain  : REAL ;
value1RefBoolMain : BOOL;
value2RefRealMain : REAL;
myFnBlock : TESTFunctionBlock;
END_VAR

//Body of the MAIN program

// value1RefFnBool  is input
myFnBlock.value1RefFnBool := TRUE;  
//Error 'value1LocalBool' is no input of 'TESTFunctionBlock'
//myFnBlock.value1LocalBool := TRUE; 
myFnBlock.input1FnBool := TRUE;
myFnBlock.input2FnReal := 100.0;
myFnBlock();
value2RefRealMain := myFnBlock.value2RefFnReal;
output2RealMain:= myFnBlock.output2FnReal;

We have also couple of persistent variables in the function block.

From the main program, we need to call the function block in the following way.

myFnBlock();

If we don’t call then the output will not be updated. Also, note that we can’t assign a local variable of the function block from the caller.

Download

Download the test software from the link at the beginning of the page.

See Also

References

Download the sample from the link given above.

Next, let’s try to understand PLC data type at https://www.hemelix.com/plc/pointers-and-function-block/

Ask questions related to Hemelix sample code and design at Google group https://groups.google.com/g/hemelix