Declaration of a function block in ST
FUNCTION_BLOCK (* optional_begin *) FINAL|ABSTRACT (* optional_end *) FB_name_2
(* optional_begin *) USING Namespace_1;
USING Namespace_2; (* optional_end *)
(* optional_begin *) EXTENDS FB_name_1 (* optional_end *)
(* optional_begin *) IMPLEMENTS interface_1, interface_2, ... interface_n (* optional_end *)
(* optional: declaration of variables/instances *)
(* optional: declaration of methods *)
(* optional: body of function block *)
END_FUNCTION_BLOCK
Meaning |
declaration of a →function block, FB_name_2 must be an →IEC-identifier. logi.CAD 3 supports the following function block variants:
The keywords FINAL and ABSTRACT (features of the →object-oriented programming ) are optional. See "Declaration of a method" for an example with an abstract or a final function block.
The USING namespace directive after the name of the function block is optional as well. See " Namespaces in ST: usage " for details on this directive.
The keyword EXTENDS
(a feature of the →object-oriented programming) is optional as well. Specify EXTENDS
, if the function block is to be derived from a different function block (this is the base function block).
Specify the name of the base function block after
EXTENDS.
The keyword
IMPLEMENTS
(a feature of the →object-oriented programming)
is optional as well. Specify
IMPLEMENTS
, if the function block is to implement one or more
→interfaces. Specify the name of the interfaces after IMPLEMENTS (separated by a comma).
Subsequently, observe that the function block must contain all methods of the interfaces. If not, a message will indicate the missing methods.
Best practice is to use the
quick fix
of
logi.CAD 3 to add the unimplemented methods within the function block. The concrete methods that are declared within the
function block are matched with the abstract methods that are declared within the interfaces. Matching is done by using the name of the method only. The methods with the same name must be identical concerning the following aspects:
|
FUNCTION_BLOCK Control
END_FUNCTION_BLOCK
INTERFACE ISwitch (* declaration of the 1st
interface
*)
METHOD SwitchState
VAR_INPUT
STATE : BOOL;
END_VAR
END_METHOD
END_INTERFACE
INTERFACE IValve (* declaration of the 2nd
interface
*)
METHOD Open
END_METHOD
END_INTERFACE
FUNCTION_BLOCK MySwitch IMPLEMENTS ISwitch, IValve
VAR
switchState : BOOL;
valveOpen : BOOL;
END_VAR
METHOD PUBLIC SwitchState
VAR_INPUT
STATE : BOOL;
END_VAR
switchState := STATE;
END_METHOD
METHOD PUBLIC Open
valveOpen := TRUE;
END_METHOD
END_FUNCTION_BLOCK
FUNCTION_BLOCK LightRoom (* the base function block
"LightRoom"
*)
VAR
Light : BOOL;
END_VAR
VAR_INPUT
Daytime : BOOL;
END_VAR
METHOD PUBLIC M1 : INT
m1:=
2
;
END_METHOD
Light := NOT(Daytime);
END_FUNCTION_BLOCK
FUNCTION_BLOCK FB_A
METHOD PUBLIC m1 : INT
m1 :=
1
;
END_METHOD
END_FUNCTION_BLOCK
FUNCTION_BLOCK Light2Room
EXTENDS LightRoom (* the derived function block
"Light2Room"
*)
VAR
Light2 : BOOL;
// 2. Licht
Inst1 : FB_A;
v1, v2, v3 : INT;
END_VAR
METHOD PUBLIC OVERRIDE M1 : INT
m1 :=
3
;
END_METHOD
METHOD PUBLIC M2
END_METHOD
SUPER();
// The body of the base function block "LightRoom" is called here.
Light2:= NOT(Daytime);
v1 := SUPER.M1();
// The method "M1" of the base function block "LightRoom" is called here.
v2 := THIS.M1();
// The method "M1" of the current function block "Light2Room" is called here.
v3 := Inst1.M1();
// The method "M1" of the function block instance 'Inst1' (= function block 'FB_A') is called here.
END_FUNCTION_BLOCK
FUNCTION_BLOCK FB1 (* the base function block
"FB1"
*)
VAR
Var1 : INT;
END_VAR
METHOD PUBLIC InitFB1
Var1 :=
5
;
END_METHOD
InitFB1(); (* call of the method
"InitFB1"
*)
END_FUNCTION_BLOCK
FUNCTION_BLOCK FB2 EXTENDS FB1 (* the derived function block
"FB2"
*)
VAR
Var2 : INT;
END_VAR
METHOD PUBLIC InitFB2
Var2 :=
5
;
InitFB1(); (* call of the method
"InitFB1"
of the base function block
"FB1"
- identical to the call of the method within the base function block itself *)
END_METHOD
InitFB2(); (* call of the method
"InitFB2"
of the derived function block
"FB2"
*)
END_FUNCTION_BLOCK
FUNCTION_BLOCK FB3
VAR
iFB2 : FB2;
END_VAR
iFB2(); (* call of the instance
for
the derived function block type
"FB2"
*)
END_FUNCTION_BLOCK
You are able to use the following language elements for ST within a function block:
Page:SFC elements in ST
Page:Call of method in ST
Page:CASE statement in ST
Page:EXIT statement in ST
Page:FOR statement in ST
Page:IF statement in ST
Page:Comments in ST
Page:Assignments in ST
The usage of these language elements makes it possible to use other elements (e.g. declaration of STRING variables within the section VAR ... END_VAR ) as well. Such elements are not listed here.