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 |
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.
|
Tip |
|---|---|
|
Never use the |
|
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 |
|
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. |
Example 96. Example
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_VAR
Declaration in the FB_A function block
FUNCTION_BLOCK FB_A
VAR_INPUT
END_VAR
VAR_OUTPUT
END_VAR
VAR PERSISTENT
iPersistentCounter_A : INT;
END_VAR
Declaration in the PLC_PRG program
VAR
fb_A1 : FB_A;
END_VAR
|
Possible 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.
|
|||
|
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 |
|
Tip |
|---|---|
|
Whenever possible, avoid marking variables as |