I managed to made more tests with your above example. I changed default value bRestore := TRUE; - to make load saved retain values at startup.
In some cases, it seems to work fine, but mostly don’t work.
The procedure I am using:
-Change some retain variables values
- xSave set to TRUE to save values in file. No error appears and everything seems to be ok. If I set bit bRestore, I get right saved values.
-Logout, power OFF Raspberry, Power ON Raspberry
-Login and check the retain variables. Sometimes the values are ok (saved values), but in most cases they are not. Even if I set bRestore, I don’t get the right values.
I am asking you if you can check this situation with your Raspberry. I am using B+ and Codesys V3.5 SP5 Patch 2.
Here is the code:
fbDelete : FILE.Delete; (* Function block to delete the file *)
(* The retain file <application-name>.ret is placed in the directory of the bootproject *)
sFileName : STRING := 'App1.ret';
pApp : POINTER TO APPLICATION;
Result : RTS_IEC_RESULT; (* Result code *)
xInit : BOOL;
xSave : BOOL;
bRestore : BOOL := TRUE;
(* The retain variables are stores in a file and get retrieved afterwards. *)
IF NOT xInit THEN
pApp := AppGetCurrent(pResult:=ADR(Result));
xInit := TRUE;
IF xSave THEN
(* First delete the retain file.
This is necessary, because the function AppStoreRetainsInFile appends the data at the end of the file. *)
IF fbDelete.xDone OR (fbDelete.xError AND fbDelete.eError = File.Error.NOT_EXIST) THEN
(* Attention: It takes at least one cycle until xDone is TRUE *)
xSave := FALSE;
(* Now, it's the time to save the retains. *)
IF pApp <> 0 THEN
(* Store the variables in a file*)
Result := AppStoreRetainsInFile(pApp,sFileName);
(* Restore the Retain Variables from the file.
For storing and restoring, the same pointer to application (pApp) must be used.*)
IF bRestore THEN
Result := AppRestoreRetainsFromFile(pApp, sFileName);
bRestore := FALSE;
I noticed that when variables are not read sucessfully from file after reboot I get the RTS_IEC_RESULT = 30, otherwise is result=0.
After some further tests I noticed that if I shut down Raspberry with sudo shutdown, then variables are successfully restored after power ON.
If I just plug off power supply, then restore from file fails. How can we make this work even if the raspberry is not shut down with the graceful shutdown?