Declaration of global variables in ST

Syntax
VAR_GLOBAL (* optional_begin *) {DMA := 'string'} CONSTANT RETAIN NON_RETAIN (* optional_end *)
name_1, name_2, ..., name_n (* optional_begin *) {'key'} AT %address (* optional_end *) : data-type := initial-value;
name_3, name_4, ..., name_n (* optional_begin *) {'key'} AT %address (* optional_end *) : STRING[length] := 'initial-value';
name_5, name_6, ..., name_n (* optional_begin *) {'key'} AT %address (* optional_end *) : ARRAY [x..y] OF type := [initial-value_1, initial-value_2, .., initial-value_n];
name_7, name_8, ..., name_n (* optional_begin *) {'key'} AT %address (* optional_end *) : ARRAY [x1..y1, x2..y2, x3..y3] OF data-type; (* Here, initial values are possible as well. *)
(* Additional pieces of data, such as partial addresses, are also possible for the variables. *)
END_VAR

Meaning

declaration of one or more →global variables, name_1, name_2 etc. must be →IEC-identifiers.
The declaration of more sections of this kind is allowed.

Enhancement to IEC-standard

This is an enhancement to the →IEC-standard which allows only one such section for resource-global or configuration-global variables. Moreover, it is possible to declare global variables within a global-object and to insert here just a reference (observe that the syntax elements might differ).

The declaration of program-global variables is possible within an ST-object, in particular within the declaration of program. Access to such global variables is done in the context of other →POUs (FUNCTION, FUNCTION_BLOCK) – by using →external variables.

The declaration of resource-global variables or configuration-global variables is possible within a PLC-object, in particular within the declaration of a →resource or of a →configuration. Access to such global variables is done in the context of POUs (PROGRAM, FUNCTION, FUNCTION_BLOCK) – also by using external variables. See "Declaring global variables for resource or configuration and using them".

The optional attributes DMA (= Direct Memory Access) and key are intended for system integrators.

Providing the optional keyword CONSTANT prevents all global variables of this section from being overwritten during program execution (makes them →constants).
Providing the optional keyword RETAIN or NON_RETAIN makes all variables of this section →retentive or non-retentive. If variables are declared based on a structured data type within the section with RETAIN or NON_RETAIN, its structure elements (incl. nestings) are treated as retentive or non-retentive alike.

See "Supported data types" to learn which data types are supported for the declaration of variables. Use the optional →initial value [:= initial-value] to assign a value to the variable (see "Initialization of variables in ST" for details).
Moreover, it is possible to declare:

Use the optional keyword AT to assign a physical address in the →PLC to the symbolic variable.

Examples for physical addresses:

Example

Explanation

%QB7

output, byte, 7th element (in the 1st level)

%IX1

input, bool, 1st element (in the 1st level)

%I1

input, bool, 1st element (in the 1st level)

%IW2.5.7.3

input, word, 3rd element in the 7th "module" in the 5th "rack" of the 2nd "bus"

If you are using EtherCAT fieldbuses that can be addressed via →EC-Master and →EC-Engineer, see "Accessing hardware IOs via EC-Master and EC-Engineer of Acontis" for information on access of their hardware IOs.

Necessary structure for physical address

Enter a physical address according to this structure:


Character

Meaning

1.

%

initiates the address

2.

prefix for location

defines the location


I

input


Q

output


M

memory

3.

prefix for size

defines the size


X or none

bool (single bit)


B

byte (8 bits)


W

word (16 bits)


D

double word (32 bits)


L

long word (64 bits)

4.

one or more →unsigned integers
You must separate the integers by . in order to enter hierarchical levels.
Enter the highest level as leftmost number, the lower levels as numbers to the right.

Restriction

logi.CAD 3 supports max. 5 levels. The highest possible number per level is: 4_294_967_295 (that corresponds to UINT32_MAX)
The underscore characters _ are inserted as separator to make the value more readable, hence _ is not significant.

defines the address

Good to know

images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/lightbulb.svg Moreover, logi.CAD 3 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.

images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/lightbulb.svg Moreover, logi.CAD 3 allows you to specify additional data for the declaration . See " Defining description, comment, JSON string or type for variables or data types " for details.

Example
VAR_GLOBAL
portSetting : INT;
upTime : TIME;
gVar1: ARRAY [1..2] of BOOL;
gVar2 : STRING[10];
END_VAR

Example for assigning a physical address
VAR_GLOBAL
VALVE_POS AT %QW28 : INT;
END_VAR