Declaring global variables for resource or configuration and using them
Declaring/using resource-global variables
How to declare →global variables for a →resource and use them in the ST-code:
Open a PLC-object.
Search for the following line in the opened editor for the PLC-object:
SyntaxRESOURCE resource-name ON platform-name { ON_CHANNEL := channel-name }
ExampleRESOURCE local ON BuiltInPlc { ON_CHANNEL := LocalChannel }
After this line, enter the declaration of the resource-global variable:
SyntaxRESOURCE resource-name ON platform-name { ON_CHANNEL := channel-name }
( * optional_begin *) USING Namespace_1;
USING Namespace_2; (* optional_end *)
VAR_GLOBAL (* optional_begin *) CONSTANT RETAIN NON_RETAIN {DMA := 'string'} (* optional_end *)
name_1, name_2, ..., name_n (* optional_begin *) {'key'} AT %address (* optional_end *) : data-type|FB-type := initial-value;
...
END_VAR
ExampleRESOURCE local ON BuiltInPlc { ON_CHANNEL := LocalChannel }
VAR_GLOBAL
VALVE_POS AT %QW28 : INT;
END_VAR
See "Declaration of global variables in ST" on the meaning of the syntax for VAR_GLOBAL ... END_VAR and possible options. There is an alternative to declare global variables within a global-object and to insert here just a reference.
Moreover, it is here possible to declare global function block instance as well. This can be done as described under " Declaration of function block instances in ST ".
The USING namespace directive in front of VAR_GLOBAL ... END_VAR is optional. See " Namespaces in ST: usage " for details on this directive.
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.If you are using EtherCAT fieldbuses that can be addressed via →EC-Master and →EC-Engineer, logi.CAD 3 is able to enter the declaration for you. But to make this happen, you have to follow the instructions under "Accessing hardware IOs via EC-Master and EC-Engineer of Acontis".
Save the PLC-object: menu file – Save
Access the declared resource-global variables in the context of →POUs by using →external variables:
Open the object in which you want to declare the external variable
In this object, declare an external variable – in particular within the declaration of the POU where the access is necessary. Access is possible within the declaration of the program, the declaration of a function block or the declaration of a function.
See "Declaration of external variables in ST" on the ST-syntax required for the external variable. Information on the declaration of variables in a graphical editor is listed under " Declaring variables " .In the object, use the external variable within the declaration of the POU (e.g. enter it within an →assignment).
Save the object: menu file – Save
Declaring/using configuration-global variables
How to declare global variables for a →configuration and use them in the ST-code:
Analogous to the declaration of resource-global variables (see above): Open a PLC-object.
Search for the following line in the opened editor for the PLC-object:
SyntaxCONFIGURATION configuration-name
ExampleCONFIGURATION LocalConfiguration
After this line, enter the declaration of the configuration-global variable. This declaration is analogous to the declaration of resource-global variables (see above).
ExampleCONFIGURATION LocalConfiguration
VAR_GLOBAL
VALVE_POS AT %QW28 : INT;
END_VAR
There is also an alternative to declare global variables within a global-object and to insert here just a reference.
Save the PLC-object: menu file – Save
Access the declared configuration-global variables in the context of →POUs by using →external variables as well. This access is analogous to the access of resource-global variables (see above).