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.
logi.CAD 3 offers the global-variables-editor as an alternative to display and edit global variables within a global-object. In this case, 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.
( * 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;
...
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.
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:
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.
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).
{INCLUDE_GLOBALS name_1}
{INCLUDE_GLOBALS name_2}
...
Always insert the INCLUDE_GLOBALS directives in front of possibly existing sections VAR_GLOBAL ... END_VAR.
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:
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);
...
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 ...
...