Declaration of global variables in global-object and its usage in ST

The declaration of →global variables in a global-object allows you to collect the declaration of global variables in one or more central objects for the project. Subsequently, you just insert a reference where you will need the declaration – this will be a →program, →resource or →configuration.

Enhancement to IEC-standard

This possibility is an enhancement to the →IEC-standard.

Creating global-object

Create a global-object by using the command Global-Object (e.g. in the context menu for a project, under New).

See "Creating new objects and folders" for generic information on creating the objects.

GLOBALS section in global-object for central declaration of global variables

Declare the global variables within the GLOBALS section of a global-object, do this analogous to the declaration of global variables in ST. One GLOBALS section is possible per global-object.

Syntax
GLOBALS name
 
( * optional_begin *) USING Namespace_1;
USING Namespace_2; (* optional_end *)
 
VAR_GLOBAL (* optional_begin *) {SECTION_NAME:= 'string'; DMA := 'string'} CONSTANT RETAIN (* optional_end *)
name_1, name_2, ..., name_n (* optional_begin *) {'key'} AT %address (* optional_end *) : data-type := initial-value;
...
END_VAR
 
VAR_GLOBAL (* optional_begin *) {SECTION_NAME:= 'string'; DMA := 'string'} CONSTANT RETAIN (* optional_end *)
name_1, name_2, ..., name_n (* optional_begin *) {'key'} AT %address (* optional_end *) : data-type := initial-value;
...
END_VAR
...
END_GLOBALS

Meaning

declaration of the GLOBALS section, name must be an →IEC-identifier. T he declaration is done within the global →namespace or with a declared namespace.
It is possible to create several VAR_GLOBAL sections within the GLOBALS section. Each VAR_GLOBAL section can contain the declaration of several global variables.

As already indicated, the syntax for VAR_GLOBAL ... END_VAR and their possibilities are analogous to the declaration of global variables in ST. Differences:

  • The USING namespace directive is supported in the GLOBALS section.

  • The optional attribute SECTION_NAME is only supported for a section VAR_GLOBAL ... END_VAR of a global-object.
    This name of the section is additional information in the textual editor. and intended for future processing by the logi.CAD 3 graphical interface. The name of the section can be specified without the attribute DMA as well – in the form of: {SECTION_NAME:= 'string}

  • The keyword NON_RETAIN is not supported in the GLOBALS section.

Moreover, observe: If you specify a namespace within the global-object, this namespace has no impact onto the access to such global variables by using the appertaining →external variable. Because the access is done via the name of the variable. Hence, no fully qualified name and no additional USING namespace directive is required for an external variable, if a global variable in a global-object with a namespace is accessed.

Moreover, logi.CAD 3 allows you to specify additional data for the declaration . See " Defining description, comment or JSON string for variables and data types " for details.

Example
GLOBALS MyGVs
VAR_GLOBAL
Setting_1 : INT;
Setting_2 : BOOL;
END_VAR
 
VAR_GLOBAL CONSTANT
Constant_1 : INT;
Constant_2 : BOOL;
END_VAR
END_GLOBALS

Reference to the GLOBALS section in a program, resource and/or configuration

In the program, the resource or the configuration, insert INCLUDE_GLOBALS directives so that the global variables from the corresponding GLOBALS section are declared as program-global, resource-global or configuration-global variables.

If the GLOBALS section is declared within a namespace, specify the fully qualified name. A fully qualified name consists of a sequence of namespace identifiers separated by . (dots).

Syntax
{INCLUDE_GLOBALS name_1}
{INCLUDE_GLOBALS name_2}
... 

Always insert the INCLUDE_GLOBALS directives in front of possibly existing sections VAR_GLOBAL ... END_VAR.


Example for a reference in a program
PROGRAM Motor
{INCLUDE_GLOBALS MyGVs} (* These program-global variables are declared: 'Setting_1', 'Setting_2', 'Constant_1', 'Constant_2' *)
VAR_GLOBAL
portSetting : INT;
Setting_1 : INT; (* error because the program-global variable 'Setting_1' has been declared repeatedly *)
END_VAR
...
END_PROGRAM

For the reference in a resource, it is indispensable to insert the INCLUDE_GLOBALS directive before the tasks are declared:

Example for a reference in a resource
RESOURCE local ON BuiltInPlc { ON_CHANNEL := LocalChannel }
{INCLUDE_GLOBALS MyGVs} (* These resource-global variables are declared: 'Setting_1', 'Setting_2', 'Constant_1', 'Constant_2' *)
VAR_GLOBAL
VALVE_POS AT %QW28 : INT;
Constant_1 : INT; (* error because the resource-global variable 'Constant_1' has been declared repeatedly *)
END_VAR
TASK DefaultTask(INTERVAL := TIME#500ms, PRIORITY := 38229);
...

Example for a reference in a configuration
CONFIGURATION LocalConfiguration
{INCLUDE_GLOBALS MyGVs} (* These configuration-global variables are declared: 'Setting_1', 'Setting_2', 'Constant_1', 'Constant_2' *)
VAR_GLOBAL
VALVE_POS AT %QW28 : INT;
Constant_2 : INT; (* error because the configuration-global variable 'Constant_2' has been declared repeatedly *)
END_VAR
RESOURCE ...
...