Persistent variables are declared in the declaration section VAR_GLOBAL RETAIN PERSISTENT in the persistent global variable list. For variables that are marked with the PERSISTENT keyword outside of the persistence editor, instance paths are added there.
As of CODESYS version 3.3.0.1, a variable declaration with PERSISTENT RETAIN has the same effect as with RETAIN PERSISTENT or PERSISTENT.
Syntax of the declaration in the global persistent variable list PersistentVars:
VAR_GLOBAL PERSISTENT RETAIN <identifier> : <data type> (:= <initialization>)?; <instance path to POU variable> END_VAR
Syntax of the declaration in POUs
<scope> PERSISTENT RETAIN <identifier> : <data type> ( := <initialization> )?; // ( ... )? : Optional END_VAR <scope> : VAR | VAR_INPUT | VAR_OUTPUT | VAR_IN_OUT | VAR_STAT | VAR_GLOBAL
An assignment of inputs, outputs, or memory addresses with the AT keyword is not permitted.
Never use the POINTER TO data type in persistent variable lists. If the application is downloaded again, their
addresses could change. The corresponding compiler warnings are shown in the message
window.
If you frequently change the names or data types of remanent variables, then it is
better to declare them as retain variables with the RETAIN keyword only.




NOTICE

Avoid inserting instance paths because in this case twice as much memory is used and a higher cycle time can occur. Instead, declare variables in the list of persistent variables.
Example
Declaration in the persistent variable list PersistentVars:
{attribute 'qualified_only'}
VAR_GLOBAL PERSISTENT RETAIN
g_iCounter : INT;
// Generated instance path of persistent variable
PLC_PRG.fb_A.iPersistentCounter_A: INT;
END_VAR
Declaration in the function block FB_A:
FUNCTION_BLOCK FB_A VAR_INPUT END_VAR VAR_OUTPUT END_VAR VAR PERSISTENT iPersistentCounter_A : INT; END_VAR
Declaration in the program PLC_PRG:
VAR fb_A1 : FB_A; END_VAR
Possible declaration locations
|
Directly in the persistent global variable list |
The variable is persistent and lies in the protected memory area. |
|
Locally in a program with an instance path in the persistent variable list Locally in a function block with an instance path in the persistent variable list |
The variable is persistent and located in the protected memory area and in the memory (double allocation). |
|
Only locally in a program Only locally in a function block |
This variable is not persistent. A warning is shown in the message window. Hint: Click “Declarations Add All Instance Paths” to import the variables into the persistent variable list. |
|
Locally in a function |
This declaration does not have any effect. The variable is not persistent. |
In the persistence editor, click “Declarations Add All Instance Paths” if local variables are marked with PERSISTENT.
Whenever possible, avoid marking variables, which are declared in a function block,
with PERSISTENT. This is because the function block instance is stored entirely in remanent memory
and not just the marked variable.