Attribute 'TcRetain'

This pragma causes the following variables to be created as Retain Variables and to retain their value after an uncontrolled termination (power failure). The pragma thus represents an alternative to using the keyword VAR RETAIN.

The so-called Retain Handler ensures that the Retain Variables are written at the end of a PLC cycle and only in the corresponding area of the NovRam. The handling of the Retain Handler is described in chapter "Retain data" of the C/C++ documentation.

If self-defined Data Unit Types (DUTs) are to be used as Retain Variables, the data types must be available in the TwinCAT type system. You can either use the option Convert to Global Type or you can create structures directly as STRUCT RETAIN. However, the Retain Handler then handles all occurrences of the structure.

Retain Data cannot be used for POUs (function blocks) as a whole. However, individual elements of a POU can be used.

Syntax: {attribute 'TcRetain'}

Insertion location: Line above the declaration line of a variable

Sample:

PROGRAM MAIN
VAR
    {attribute 'TcRetain'}
    nVar1  : INT;
END_VAR