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.

images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/information.svg logi.CAD 3 offers the global-variables-editor as an alternative to display and edit global variables within a global-object. Moreover, the application navigator provides a possibility to automatically insert the reference (details: see "Creating and using a global-object"). In both cases, you do not need to know the following syntax.

Creating global-object

See " Creating and using a global-object ".

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
( * optional_begin *) { Readonly ('your comment')} (* optional_end *)
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|FB-type := initial-value;
...
(* Additional pieces of data, such as partial addresses, are also possible for the variables. *)
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|FB-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 or declaration of function block instances in ST (see there for the possibilities when declaring global variables or function block instances) . Differences:

  • The read-only statement { Readonly ('your comment') is supported in front of the GLOBALS section.

  • 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, textual editor. It is used/evaluated by the →graphical editor. 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 global-object.

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.

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
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 ...
...