Retention

Retention qualifiers control whether variables preserve their values across power cycles and program restarts.

IEC 61131-3

Section 2.4.3

Support

Not yet supported

Qualifiers

Qualifier

Description

RETAIN

Value is preserved across power cycles

NON_RETAIN

Value is reset to initial value on restart

CONSTANT

Value cannot be modified after initialization

Example

PROGRAM main
    VAR RETAIN
        run_hours : DINT := 0;
    END_VAR
    VAR CONSTANT
        MAX_TEMP : INT := 150;
    END_VAR
    VAR
        current_temp : INT;
    END_VAR

    run_hours := run_hours + 1;
END_PROGRAM

PERSISTENT

Note

This is an extension not part of the IEC 61131-3 standard. See Enabling Dialects and Features for how to enable it.

The PERSISTENT qualifier marks variables whose values survive a download and a reset as well as a power cycle. Enable it with the --allow-persistent-var flag; the codesys and twincat dialects enable it.

Qualifier

Description

PERSISTENT

Value is preserved across downloads, resets and power cycles

RETAIN PERSISTENT

Same as PERSISTENT

PERSISTENT RETAIN

Same as RETAIN PERSISTENT: the two orders mean the same thing

A PERSISTENT qualifier may follow VAR and VAR_GLOBAL, and the VAR_INPUT and VAR_OUTPUT of a function block or program. A function keeps no values between calls, so a function that declares a PERSISTENT variable is an error (P4099).

FUNCTION_BLOCK Counter
    VAR_INPUT PERSISTENT
        start : DINT := 0;
    END_VAR
    VAR_OUTPUT RETAIN PERSISTENT
        count : DINT;
    END_VAR
    count := count + 1;
END_FUNCTION_BLOCK

PROGRAM main
    VAR PERSISTENT
        run_hours : DINT := 0;
    END_VAR
    VAR
        counter : Counter;
    END_VAR
    counter();
    run_hours := counter.count;
END_PROGRAM

See Also