Declaration of function block instances in ST

Syntax
VAR (* optional_begin *) CONSTANT RETAIN NON_RETAIN PUBLIC|PROTECTED|PRIVATE|INTERNAL (* optional_end *)
| VAR_INPUT (* optional_begin *) RETAIN NON_RETAIN (* optional_end *)
| VAR_OUTPUT (* optional_begin *) RETAIN NON_RETAIN (* optional_end *)
| VAR_GLOBAL (* optional_begin *) CONSTANT RETAIN NON_RETAIN (* optional_end *)
FB_instance_1, FB_instance_2, ..., FB_instance_n : FB-type := ( input_1 := initial-value, input_2 := initial-value, ..., output_n := initial-value);
FB_instance_A, FB_instance_B, ..., FB_instance_n: ARRAY [x..y] OF FB-type := [(input_1 := initial-value, input_2 := initial-value, ...), (input_1 := initial-value, input_2 := initial-value, ...), ...];
FB_instance_C, FB_instance_D, ..., FB_instance_n: ARRAY [x1..y1, x2..y2, x3..y3] OF FB-type; (* Initialisierungswerte sind hier ebenfalls möglich. *)
END_VAR
 
VAR_IN_OUT
FB_instance_1, FB_instance_2, ..., FB_instance_n : FB-type;
FB_instance_A, FB_instance_B, ..., FB_instance_n: ARRAY [x..y] OF FB-type;
FB_instance_C, FB_instance_D, ..., FB_instance_n: ARRAY [x1..y1, x2..y2, x3..y3] OF FB-type;
END_VAR

Meaning

declaration of one or more →function block instances, FB_instance_1 etc. must be →IEC-identifiers.
The declaration of more sections of this kind is allowed. The declaration is possible within the declaration of a →program or of a →function block.

Providing the optional keyword RETAIN or NON_RETAIN makes all elements of this section →retentive or non-retentive. RETAIN or NON_RETAIN is possible within the declaration of a program and the declaration of a function block . If function block instances are declared in the current section with RETAIN or NON_RETAIN , see the FAQ-article "How is the keyword "RETAIN" considered for function block instances?" for the behavior of its variables concerning RETAIN and NON_RETAIN.

Use the optional keywords for visibility to define the visibility of all elements of this section. These keywords are possible within the declaration of a function block only.

Keyword

Meaning

PUBLIC

Public usage: The variable may be used anywhere where the function block can be used as well.

PROTECTED (or none)

Protected usage (default): The variable may only be used inside the defining function block and its derived function blocks.

If there are no derived function blocks , PROTECTED has the same meaning as PRIVATE .

PRIVATE

Private usage: The variable may only be used from inside the defining function block.

INTERNAL

Internal usage: The variable may only be used from inside the same →namespace.

Deviations from IEC-standard

If you prefer that PUBLIC is the default, define the logi.CAD 3 configuration variable lc3.var.access.default.public and the value TRUE. See the documentation "Administrator's Manual" for details about the configuration variables.
Consequence: →Local variables (= VAR ) of →function blocks without a keyword for the visibility can be used inside the defining function block and its derived function blocks according to the standard. But it is possible to change the configuration so that these variables can be used anywhere where the function block can be used as well.

An existing →function block (created in →ST, →FBD or →LD) is possible as type for a function block instance.

When declaring a function block instance within the section VAR...END_VAR, VAR_INPUT...END_VAR or VAR_OUTPUT...END_VAR , it is possible to initialize its →input variables and →output variables. The initial value must be of the same data type as the declared variable is of or it must be a data type that can be converted into this data type due to the implicit conversion.
When declaring a global function block instance (i.e., within the section VAR_GLOBAL...END_VAR) , it is not possible to use the a ttribute DMA (as it is provided for →global variables).
When declaring a function block instance within the section VAR_IN_OUT...END_VAR , it is not possible to specify an initial value.

Moreover, it is possible to declare function block instances with one- or multi-dimensional arrays (this is similar to "Declaration of an ARRAY data type in ST" as well) or by means of structure elements (see "Declaration of a structured data type in ST").

Restriction and note on usage

  • At present, it is possible to declare function block instances within the section VAR_INPUT...END_VAR. However, the call of such a function block instance is illegal.

  • At present, it is possible to declare function block instances with ARRAYs within a →function. But logi.cals advises you to not declare such function block instances within functions because they are not compliant with the IEC standard and they might become an illegal construct in the future of logi.CAD 3.

  • Access to a global function block instance that is declared in the section VAR_GLOBAL...END_VAR of an ST-object is done in the context of other of function block – by using →external variables.

  • The declaration of function block instances is also possible within a PLC-object and/or in a global-object. Access to such global function block instances is done in the context of POUs (PROGRAM, FUNCTION_BLOCK) – also by using external variables. images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/information.svg Access to such global function block instances in the context of functions is not allowed according to the →IEC-standard.

Examples
VAR
Inst1, Inst2 : TOF;
(* declaring two instances of function block 'TOF' *)
Inst3 : Control := (IN1 := 10, IN2 := 20, T1 := T#3ms);
(* declaring one instance of function block 'Control' and initializing its inputs 'IN1', 'IN2' and 'T1' *)
TONArr2 : ARRAY [0..2] OF TON := [(PT:=T#100ms), (PT:=T#50ms)];
(* declaring 3 instances of TON block and initializing the input 'PT' of the first 2 instances *)
END_VAR
 
VAR_INPUT
I_TON : TON;
END_VAR
 
VAR_OUTPUT
O_TON : TON;
END_VAR
 
VAR_IN_OUT
IO_TMR : TOF;
END_VAR

Example for the declaration of a function block instance incl. its initialization -- one of its inputs is declared by using a structure data type
PROGRAM Test
VAR
myFB : FB := (in1 := 1, c := (re := 1, im := 2));
END_VAR
END_PROGRAM
 
FUNCTION_BLOCK FB
VAR_INPUT
in1 : int;
c : complex;
END_VAR
END_FUNCTION_BLOCK
 
TYPE
complex : STRUCT
re : REAL;
im : REAL;
END_STRUCT;
END_TYPE