Variable: PERSISTENT
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.
Tip
As of CODESYS V3.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_VARSyntax 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_GLOBALAn assignment of inputs, outputs, or memory addresses with the AT keyword is not permitted.
Tip
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.
Tip
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.
Important
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.
Declaration in the PersistentVars persistent variable list
{attribute 'qualified_only'}
VAR_GLOBAL PERSISTENT RETAIN
g_iCounter : INT;
// Generated instance path of persistent variable
PLC_PRG.fb_A.iPersistentCounter_A: INT;
END_VARDeclaration in the FB_A function block
FUNCTION_BLOCK FB_A
VAR_INPUT
END_VAR
VAR_OUTPUT
END_VAR
VAR PERSISTENT
iPersistentCounter_A : INT;
END_VARDeclaration in the PLC_PRG program
VAR
fb_A1 : FB_A;
END_VARPossible declaration locations | Description |
|---|---|
Directly in the persistent global variable list | The variable is persistent and located 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 | The variable is not persistent. A warning is shown in the message window. TipClick to import the variables into the persistent variable list. |
Locally in a function | This kind of declaration does not have any effect. The variable is not persistent. |
Tip
In the persistence editor, click if local variables are marked as PERSISTENT.
Tip
Whenever possible, avoid marking variables as PERSISTENT which are declared in a function block. This is because the function block instance is stored entirely in remanent memory and not just the marked variable.