Declaration of function block instances or class 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;  (* Initial values are also possible here. *)
(* Additional pieces of data, such as partial addresses, are also possible for the function block instances. *)
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
 
VAR_EXTERNAL (* optional_begin *) CONSTANT (* optional_end *)
   name_1, name_2, ..., name_n : FB-type;
   name_5, name_6, ..., name_n : ARRAY [x..y] OF FB-type;
   name_7, name_8, ..., name_n : ARRAY [x1..y1, x2..y2, x3..y3] OF FB-type;
END_VAR

 

(* Note: If a class is used as type for the variable, it is actually a class instance. *)

Meaning

declaration of one or more →function block instances, FB_instance_1 etc. must be →IEC-identifiers
Analogously, it is possible to declare instances of a →class. In this case, the following description applies to the class (instead of the function block) when the context can be applied to a class.

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-retentiveRETAIN 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 Neuron Power Engineer applies different visibility to the elements without any keyword, define the Neuron Power Engineer configuration variable lc3.var.access.default and the value PUBLICPRIVATE or INTERNAL. Subsequently, this value will be used as the new default. In the case of the value PROTECTED for the configuration variable, the default PROTECTED is valid as this is specified by the →IEC-standard.
For PUBLIC as default, you are also able to use the Neuron Power Engineer configuration variable lc3.var.access.default.public and value TRUE as an alternative. The configuration variable lc3.var.access.default.public takes precedence over lc3.var.access.default. 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_VARVAR_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 attribute DMA (as it is provided for →global variables).
When declaring a function block instance within the section VAR_IN_OUT...END_VAR or VAR_EXTERNAL...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 Neuron 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 Neuron Power Engineer.

  • 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 declaring a function block instance as →external variable in the section VAR_EXTERNAL...END_VAR.

  • 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. (info) Access to such global function block instances in the context of functions is not allowed according to the →IEC-standard.

 

Good to know

(grey lightbulb) Moreover, Neuron Power Engineer allows you to define a not yet fully specified location for the declaration. See "Declaration of a language element with partial address in ST" for details.

(grey lightbulb) Moreover, Neuron Power Engineer allows you to specify additional data for the declaration. See "Defining description, comment, JSON string or type for variables or data types" for details.

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