Attribute global_init_slot
The pragma {attribute 'global_init_slot'}
defines the sequence of initialization of POUs or global variable lists.
Variables in a list (GVL or POU) are initialized from top to bottom.
If there are several global variable lists available, then the sequence of initialization is not defined.
The sequence of initialization is irrelevant for literal values, such as 1
, 'hello'
, 3.6
, or for constants of base data types. However, if there are dependencies between the different lists, you must define the sequence of initialization by yourself. To achieve this, you can assign a defined initialization slot to a GVL or a POU using the attribute global_init_slot
.
Constants are initialized before the variables and in the same order as the variables. During initialization, the POUs are sorted according to the value for <slot>
. Then the code for initializing the constants is generated. After that, the code for initializing the variables is generated.
{attribute 'global_init_slot' := '<slot>'}
Replace <slot> by an integer value that defines the position in the initialization order. The default value for a POU (program, function block) is 50,000. The default value for a GVL is 49,990. A lower value provokes an earlier initialization. In case of several POUs or GVLs carrying the same value for the attribute global_init_slot
, the sequence of their initialization remains undefined. This will be indicated as a detected programming error in the category of the view.
The pragma {attribute 'global_init_slot'}
is valid for the entire GVL or POU and must therefore be located above the VAR_GLOBAL
or POU declaration.
The example includes 2 global variable lists GVL_1 and GVL_2 and a program PLC_PRG
that uses variables from both lists.
GVL_1 uses the variable B
for initializing a variable A
which is initialized in the GVL_2 with a value of 1000
.
:
VAR_GLOBAL //49990
A : INT := GVL_2.B*100;
END_VAR
:
VAR_GLOBAL //49990
B : INT := 1000;
C : INT := 10;
END_VAR
:
PROGRAM PLC_PRG //50000
VAR
ivar: INT := GVL_1.A;
ivar2: INT;
END_VAR
ivar:=ivar+1;
ivar2:=GVL_2.C;
When building this example, a programming error is issued in the view because GVL_2.B
is used for initializing GVL_1.A
before GVL_2
has been initialized. To avoid this, use the attribute global_init_slot
in order to position GVL_2
before GVL_1
in the sequence of initialization.
GVL_2
must have a slot value of 49989
or lower to achieve the earliest initialization within the program.
:
{attribute 'global_init_slot' := '100'}
VAR_GLOBAL
B : INT := 1000;
C : INT := 10;
END_VAR
You can even use GVL_2.C
in the implementation part of PLC_PRG
without a pragma because both GVLs are initialized before the program in either case.