Constant Variables
The constant variable (VAR_CONSTANT acc. to the IEC 61131-3) has a value that cannot be changed by the running application (read-only access). Constant variables can be declared in the variables table or data list by selecting the 'Constant' attribute of a variable (see also the following rules) or in data type worksheets using a VAR CONSTANT...END_VAR declaration block (see below).
The following rules apply for constant variables:- To be specified as literal acc. to the IEC 61131-3 standard. Expressions are not supported.
- Allowed in the controller Data List for 'Global' declarations. The global constant variable must also be defined in the POU as 'External' variable with the 'Constant' attribute set. Otherwise a compiler error is generated.
- Allowed in local variables table of POUs for 'Local', 'External', and 'Program' variables.
('Program' variables with set data direction ('I/O' property) cannot be 'Constant' variables.)When declaring an 'External' variable which relates to an already existing constant global variable, the 'Constant' attribute is set automatically in the local variables table. - Can be used in data type worksheets (see below) as:
- boundaries and initialization values in arrays.
- initialization values in structures.
- initialization values and predefined values in enumerations.
- Can be used as initialization values in variables tables and data lists. Observe the following:
- The constant, that is used as initial value of a variable in a POU, must be declared in the POU variables table as 'Local' or 'External'.
- The constant, that is used as initial value of a global variable in the resource's data list (IEC 61131-3 node), must be declared in the data list as 'Global'.
- Not allowed for global input variables (mapped to an input process datum in the Data List): While 'Constant' is selected, no process datum can be selected, and if an input process datum is already assigned, the 'CONSTANT' checkbox is inactive.
- The 'Constant' attribute is not allowed in combination with the variable's 'Retain' attribute and vice versa.
- Not allowed in the Safety PLC Data List and in safety-related variables tables.
- Not allowed in the controller Data List for exchange variables (as they are connected to a standard Safety PLC variable).
- Not allowed for ports.
- Supported by role picker and Intellisense.
- Allows partial access to ANY_BIT data types in code worksheets.Example: myBool := MY_CONST_WORD.X0;See the topic "Partial Access to ANY_BIT Variables" for details.
- In online mode:
- The initialization value is displayed.
- Constant variables cannot be edited or added to the WATCHES window or LOGIC ANALYZER window.
- Overwriting or forcing is not possible.
Declaration in data type worksheet (local constant)
Constant variables are declared in the data type worksheet using a VAR CONSTANT...END_VAR declaration block (see the following example). The initialization value of a constant variable must be a literal (expressions for the initialization value are not supported).
Note
Constants can only be used in the data type worksheet where they are declared. |