Retain Variables with CODESYS Control SL
Default behavior
The default behavior of retain variables of CODESYS Control SL products is to write retain variables as a file when the controller is shutting down.
To make sure that retain variables can be safely stored even in the event of a power failure, a controlled shutdown of the runtime system must be guaranteed. For example, use an uninterruptible power supply (UPS).
Moreover, the retain variables can be actively written to and read from via the application.
pApp : POINTER TO CmpApp.APPLICATION;
result : DINT;
pApp := CmpApp.AppFindApplicationByName('Application', ADR(result));
//Store values of retain and persistent variables to file
CmpApp.AppStoreRetainsInFile(pApp, '/your/retain/file.ret');
//Get values of retain and persistent variables from file
CmpApp.AppRestoreRetainsFromFile(pApp, '/your/retain/file.ret');Retain Variables in Shared Memory
Alternatively, a mechanism could also be enabled which cyclically writes retain variables to shared memory. For more information, see the following: Retain Data in Shared Memory
CmpRetainDoubleBufferedInFile
RetainDoubleBufferedInFile is another mechanism which can be used for writing retain variables. When this mechanism is enabled, retain and persistent variables are written cyclically to the drive. When this happens, the process alternates between writing to two different files.
Basically, RetainDoubleBufferedInFile is enabled via SHM by means of a setting in /etc/CODESYSControl_User.cfg on the target device. In this file, under the [ComponentManager] section, replace the entry Component.X=CmpRetain with Component.X=CmpRetainDoubleBufferedInFile.
[ComponentManager] Component.7=CmpRetainDoubleBufferedInFile
For more information about retain variables, see the following: Data persistence