You can declare variables:
oin the Variables view of the Software Catalog
oin the Declaration Editor of a POU
ovia the Auto Declare dialog box
oin a GVL editor
The kind (in the tabular declaration editor it is named Scope) of the variables to be declared is specified by the keywords embracing the declaration of one or several variables. In the textual declaration editor, the common variable declaration is embraced by VAR and END_VAR.
For further variable declaration scopes, refer to:
oVAR_INPUT
oVAR_OUTPUT
oVAR_IN_OUT
oVAR_GLOBAL
oVAR_TEMP
oVAR_STAT
oVAR_EXTERNAL
oVAR_CONFIG
The variable type keywords may be supplemented by attribute keywords.
Example: RETAIN (VAR_INPUT RETAIN)
Syntax for variable declaration:
<Identifier> {AT <address>}:<data type> {:=<initialization>};
The parts in braces {} are optional.
The identifier is the name of a variable.
Consider the following facts when defining an identifier.
ono spaces or special characters allowed
ono case-sensitivity: VAR1, Var1 and var1 are all the same variable
orecognizing the underscore character: A_BCD and AB_CD are considered 2 different identifiers. Do not use more than 1 underscore character in a row.
ounlimited length
orecommendations concerning multiple use (see next paragraph)
Also, consider the recommendations given in chapter Recommendations on the Naming of Identifiers.
Multiple Use of Identifiers (Namespaces)
The following outlines the regulations concerning the multiple use of identifiers:
oDo not create an identifier that is identical to a keyword.
oDuplicate use of identifiers is not allowed locally.
oMultiple use of an identifier is allowed globally: a local variable can have the same name as a global one. In this case, the local variable within the POU will have priority.
oA variable defined in a global variable list (GVL) can have the same name as a variable defined in another global variable list (GVL). In this context, consider the following IEC 61131-3 extending features:
oGlobal scope operator: an instance path starting with a dot (.) opens a global scope. So, if there is a local variable, for example ivar, with the same name as a global variable, .ivar refers to the global variable.
oYou can use the name of a global variable list (GVL) as a namespace for the included variables. You can declare variables with the same name in different global variable lists (GVL). They can be accessed specifically by preceding the variable name with the list name.
Example
globlist1.ivar := globlist2.ivar;
(* ivar from globlist2 is copied to ivar in GVL globlist1 *)
oVariables defined in a global variable list of an included library can be accessed according to syntax <library namespace>.<name of GVL>.<variable>.
Example:
globlist1.ivar := lib1.globlist1.ivar
(* ivar from globlist1 in library lib1 is copied to ivar in GVL globlist1 *)
oFor a library also, a namespace is defined when it gets included via the Library Manager. So you can access a library module or variable by <library namespace>.<modulename|variablename>. Consider that, in case of nested libraries, the namespaces of all libraries concerned have to be stated successively.
Example: If Lib1 is referenced by Lib0, the module fun being part of Lib1 is accessed by Lib0.Lib1.fun:
ivar := Lib0.Lib1.fun(4, 5); (* return value of fun is copied to variable ivar in the project *)
NOTE: Once the checkbox Publish all IEC symbols to that project as if this reference would have been included there directly. has been activated within the Properties dialog box of the referenced library Lib, the module fun may also be accessed directly via Lib0.fun.
oVariables declared in GVLs or POUs of the Global node of the Applications tree can be accessed by prefixing them with the operator "__POOL.".
You can link the variable directly to a definite address using the keyword AT.
In function blocks, you can also specify variables with incomplete address statements. In order that such a variable can be used in a local instance, an entry has to exist for it in the variable configuration.
Valid data type, optionally extended by an :=< initialization>.
Optionally, you can add pragma instructions in the declaration part of an object in order to affect the code generation for various purposes.
Automatic declaration of variables is also possible.
For faster input of the declarations, use the shortcut mode.