It seems that on the Codesys Linux Runtime SL with Version v3.5.21.2 the retain variables will save when you perform a graceful shutdown (from CLI or WMB) but not if you cycle power. One way to remedy this is to save the retains to a file and periodically save these off, and then subsequently load this file on startup. Here is example code to do this if it is needed.
PROGRAM RetainSaveProgram
VAR
pApp : POINTER TO APPLICATION;
sRetFile : STRING := ‘MyAppRetains.ret’; // file
nameresult : CmpErrors.RTS_IEC_RESULT;
res : DINT;
TSave : TON;
xFirstScan : BOOL := TRUE;
END_VAR
IF xFirstScan THEN
xLoad := TRUE;
END_IF
IF xSave THEN
pApp := CmpApp.AppGetCurrent(pResult := ADR(result));
IF pApp <> 0 THEN
result := CmpApp.AppStoreRetainsInFile(pApp, sRetFile);
IF result = 0 THEN
xSave := FALSE;
END_IF
END_IF;
END_IF;
TSave(IN := NOT TSave.Q, PT := T#300S);
IF TSave.Q THEN
xSave := TRUE;
END_IF
IF xLoad THEN
pApp := CmpApp.AppGetCurrent(pResult := ADR(res));
IF pApp <> 0 THEN
res := CmpApp.AppRestoreRetainsFromFile(pApp, sRetFile);
IF res = 0 THEN
xLoad := FALSE;
END_IF
END_IF;
END_IF;
xFirstScan := FALSE;
Here are the two libraries you need to add to your project.
![]()