Attribute Keywords for Variable Types
You can add the following attribute keywords to the declaration of the variable type in order to specify the scope:
oRETAIN: refer to Retain Variables
oPERSISTENT: refer to Persistent Variables
oCONSTANT: refer to Constants - CONSTANT, Typed Literals
Remanent Variables - RETAIN, PERSISTENT
Remanent variables can retain their value throughout the usual program run period. Declare them as retain variables or even more stringent as persistent variables.
The declaration determines the degree of resistance of a remanent variable in the case of resets, downloads, or a reboot of the controller. In applications mainly the combination of both remanent flags is used (refer to Persistent Variables).
NOTE: A VAR PERSISTENT declaration is interpreted in the same way as a VAR PERSISTENT RETAIN or VAR RETAIN PERSISTENT.
NOTE: Use the command Add all instance paths to take variables declared as persistent into the Persistent list object.
Variables declared as retain variables are stored in a nonvolatile memory area. To declare this kind of variable, use the keyword RETAIN in the declaration part of a POU or in a global variable list.
Example
VAR RETAIN
iRem1 : INT; (* 1. Retain variable*)
END_VAR
Retain variables maintain their value even after an unanticipated shutdown of the controller as well as after a normal power cycle of the controller (or when executing the Online command Reset Warm). At restart of the program, the retained values will be processed further on. The other (non-retain) variables are newly initialized, either with their initialization values or with their default initialization values (in case no initialization value was declared).
For example, you may want to use a retained value when an operation, such as piece counting in a production machine, should continue after a power outage.
Retain variables, however, are reinitialized when executing the Online command Reset origin and, in contrast to persistent variables, when executing the Online command Reset cold or in the course of an application download.
NOTE: Only the specific variables defined as VAR RETAIN are stored in nonvolatile memory. However, local variables defined as VAR RETAIN in functions are NOT stored in nonvolatile memory. Defining VAR RETAIN locally in functions is of no effect.
Using interfaces or function blocks out of System Configuration libraries in the retain program section (VAR_RETAIN) will cause system exceptions, which may make the controller inoperable, requiring a re-start.
|
UNINTENDED EQUIPMENT OPERATION |
oDo not use interfaces out of the SystemConfigurationItf library in the retain program section (VAR_RETAIN). oDo not use function blocks out of the SystemConfiguration library in the retain program section (VAR_RETAIN). |
Failure to follow these instructions can result in death, serious injury, or equipment damage. |
NOTE: The libraries SystemConfigurationItf and SystemConfiguration are only available for PacDrive controllers (PacDrive LMC Eco, PacDrive LMC Pro/Pro2).
Persistent variables are identified by keyword PERSISTENT (VAR_GLOBAL PERSISTENT). They are only reinitialized when executing the Online command Reset origin. In contrast to retain variables, they maintain their values after a download.
NOTE: Do not use the AT declaration in combination with VAR PERSISTENT.
Application example:
A counter for operating hours, which should continue counting even after a power outage or a download. Refer to the synoptic table on the behavior of remanent variables.
You can only declare persistent variables in a special global variable list of object type persistent variables, which is assigned to an application. You can add only one such list to an application.
NOTE: A declaration with VAR_GLOBAL PERSISTENT has the same effect as a declaration with VAR_GLOBAL PERSISTENT RETAIN or VAR_GLOBAL RETAIN PERSISTENT.
Like retain variables, the persistent variables are stored in a separate memory area.
Example
VAR_GLOBAL PERSISTENT RETAIN
iVarPers1 : DINT; (* 1. Persistent+Retain Variable App1 *)
bVarPers : BOOL; (* 2. Persistent+Retain Variable App1 *)
END_VAR
NOTE: Persistent variables can only be declared inside the Persistent list object. If they are declared elsewhere, they will behave like retain variables and they will be reported as a detected Build error in the Messages view. (Retain variables can be declared in the global variable lists or in POUs.)
At each reload of the application, the persistent variable list on the controller will be checked against that of the project. The list on the controller is identified by the application. In case of inconsistencies, you will be prompted to reinitialize all persistent variables of the application. Inconsistency can result from renaming or removing or other modifications of the existing declarations in the list.
NOTE: Carefully consider any modifications in the declaration part of the persistent variable list and the effect of the results regarding reinitialization.
You can add new declarations only at the end of the list. During a download, these are detected as new and will not demand a reinitialization of the complete list. If you modify the name or data type of a variable, this is handled as a new declaration and provokes a reinitialization of the variable at the next online change or download.
Behavior of Remanent Variables
Consult the Programming Guide specific to your controller for further information on the behavior of remanent variables.
Constants are identified by the keyword CONSTANT. You can declare them locally or globally.
Syntax
VAR CONSTANT<identifier>:<type> := <initialization>;END_VAR
Example
VAR CONSTANT
c_iCon1:INT:=12; (* 1. Constant*)
END_VAR
Refer to the Operands chapter for a list of possible constants.
Basically, in using IEC constants, the smallest possible data type will be used. If another data type has to be used, this can be achieved with the help of typed literals without the necessity of explicitly declaring the constants. For this, the constant will be provided with a prefix which determines the type.
Syntax
<type>#<literal>;
<type> |
specifies the desired data type possible entries: BOOL, SINT, USINT, BYTE, INT, UINT, WORD, DINT, UDINT, DWORD, REAL, LREAL Write the type in uppercase letters. |
<literal> |
specifies the constant Enter data that fits within the data type specified under <type>. |
Example
iVar1:=DINT#34;
If the constant cannot be converted to the target type without data loss, a message is issued.
You can use typed literals wherever normal constants can be used.
As long as the default setting Replace constants (File > Project Settings > Compile options) is activated, constants in online mode have a symbol preceding the value in the Value column in the declaration or watch view. In this case, they cannot be accessed by, for example, forcing or writing.