Manually saving PersistentVars value

damian177
2022-04-12
2022-06-30
1 2 > >> (Page 1 of 2)
  • damian177 - 2022-04-12

    Hi,
    I use RPi and Persistent Vars and as you well know it doesn't work, so
    It is possible manually form Codesys application save/update the Persistent Vars value ?

     
  • damian177 - 2022-04-12

    I do not understand this code:

    PROGRAM AppRetain
    VAR
       fbDelete             : FILE.Delete; (* Function block to delete the file *)
       sFileName            : STRING := 'App10.ret';
       pApp                 : POINTER TO APPLICATION;
       Result               : RTS_IEC_RESULT; (* Result code *)
       xInit                : BOOL;
       xSave                : BOOL;
       xRestore             : BOOL;
    END_VAR
    
    IF NOT xInit THEN
       pApp := AppGetCurrent(pResult:=ADR(Result));
       xInit := TRUE;
    END_IF
    
    IF xSave THEN   
       (* First delete the retain file.
          This is necessary, because the function AppStoreRetainsInFile appends the data at the end of the file. *)
       fbDelete(xExecute:=TRUE, sFileName:=sFileName);
       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 *)
          fbDelete(xExecute:=FALSE);
          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);
          END_IF
       END_IF
    END_IF
    
    
    (* Restore the Retain Variables from the file.
       For storing and restoring, the same pointer to application (pApp) must be used.*)
    IF xRestore THEN
       Result := AppRestoreRetainsFromFile(pApp, sFileName);
       xRestore := FALSE;
    END_IF
    

    Where is my variables (in above code) which one are created "PersistentVars" in my project ?

    The sFileName should be difference than my Application name to work properly ?

     

    Last edit: damian177 2022-04-12
  • paulpotat

    paulpotat - 2022-04-12

    I don't know if it's good practice, but I simply do this to manage the persistant variables in my program:

    application_ptr : POINTER TO CmpApp.APPLICATION;
    res : DINT;
    
    // Get application pointer
    application_ptr := CmpApp.AppFindApplicationByName('Application', ADR(res));
    
    // Restore the variables
    CmpApp.AppRestoreRetainsFromFile(application_ptr, '/home/pi/retains.ret');
    
    // ...
    
    // Store the variables
    CmpApp.AppStoreRetainsInFile(application_ptr, '/home/pi/retains.ret');
    

    To answer your questions, sFilename should be something ending with ".ret" extension I think.
    Also, be aware that those store and restore functions works on all your persistant variables at once (so you don't have to specifiy which one you want to store / restore).

    Hope this helps
    BR

     
  • yannickasselin - 2022-04-14

    I personnaly use the persistence manager. It works great and is very flexible. It is a bit of work to setup but once it is done, it is easy to use.

     
    πŸ‘
    1
  • damian177 - 2022-04-14

    I try this solution (Persistence Manager) but it do not work. I did as described:
    1) In Codesy - View -> Modules
    2) Add Toplevel Module Instance
    3) Add Persistence Manager
    4) Select the Application - Device.Application
    5) Added group "Persistence1"
    6) Added Submodule Instance "PersistenceChannel"
    7) Setup the Persistence Parameter - xSaveOnChange on TRUE
    8) Add Submodule Instance - and Select ASCIIFileStorage
    9) Created GVL called "PersistentVars" in my project and add persistence variables with attribute:
    ~~~
    VAR_GLOBAL
    {attribute 'ac_persist':='PersistenceChannel'}
    mem_trans: ARRAY[1..50] OF BYTE;
    {attribute 'ac_persist':='PersistenceChannel'}
    id_trans:BYTE;
    END_VAR
    ~~~

    10)Generator configuration - Check only"Persistence generator"
    11)Composer->Generate

    Now in my project I have "PersistenceConfig" like in attachment.

    But it do not works... What can be wrong ?

     

    Last edit: damian177 2022-04-14
  • damian177 - 2022-04-19

    Any ideas ?

     
  • damian177 - 2022-04-19

    Before "Generate" I choose only "Persistence generator". I didn't chose "Standard generator" because after that my environment is freeze., Why it doesn't work ?
    therefore they are not generated tasks : HIGH and LOW ....

     
  • ludecus

    ludecus - 2022-04-22

    What's the version of your CODESYS IDE? I worked at latest with the 3.5 SP17 Patch 0, where the persistence manager surely worked.

    The instruction you described is fully correct and should work. Please contact CODESYS with your issue.

     
  • damian177 - 2022-04-22

    I am using Codesys 3.5 SP16 Patch 2.
    Once difference I noticed is that in configuration (like in attachement) I have set HIGH and LOW task. I no have this task in my projekt. it could be the problem cause ?

     
  • ludecus

    ludecus - 2022-04-22

    For sure, your PM generator is pretty wrong.
    The device tree should look like the attachment.
    I use 3 persistence channels device, system and operating.

     

    Last edit: ludecus 2022-04-22
  • josepmariarams - 2022-04-22

    Persistent vars works fine in Codesys for Raspberry. I am using ver 3.5.13 and it works.

    CoDeSys saves the persistence file on Raspbian shut-down. Are you stopping Raspbian normally?.

    If you save your persistent variables to a recipe, you can save and load via persistence utils, too.

     
  • josepmariarams - 2022-04-22

    Persistent vars works fine in Codesys for Raspberry. I am using ver 3.5.13 and it works.

    CoDeSys saves the persistence file on Raspbian shut-down. Are you stopping Raspbian normally?.

    If you save your persistent variables to a recipe, you can save and load via persistence utils, too.

     
  • josepmariarams - 2022-04-22

    Persistent vars works fine in Codesys for Raspberry. I am using ver 3.5.13 and it works.

    CoDeSys saves the persistence file on Raspbian shut-down. Are you stopping Raspbian normally?.

    If you save your persistent variables to a recipe, you can save and load via persistence utils, too.

     
  • josepmariarams - 2022-04-22

    Persistent vars works fine in Codesys for Raspberry. I am using ver 3.5.13 and it works.

    CoDeSys saves the persistence file on Raspbian shut-down. Are you stopping Raspbian normally?.

    If you save your persistent variables to a recipe, you can save and load via persistence utils, too.

     
  • josepmariarams - 2022-04-22

    Persistent vars works fine in Codesys for Raspberry. I am using ver 3.5.13 and it works.

    CoDeSys saves the persistence file on Raspbian shut-down. Are you stopping Raspbian normally?.

    If you save your persistent variables to a recipe, you can save and load via persistence utils, too.

     
  • josepmariarams - 2022-04-22

    Persistent vars works fine in Codesys for Raspberry. I am using ver 3.5.13 and it works.

    CoDeSys saves the persistence file on Raspbian shut-down. Are you stopping Raspbian normally?.

    If you save your persistent variables to a recipe, you can save and load via persistence utils, too.

     
  • josepmariarams - 2022-04-22

    Persistent vars works fine in Codesys for Raspberry. I am using ver 3.5.13 and it works.

    CoDeSys saves the persistence file on Raspbian shut-down. Are you stopping Raspbian normally?.

    If you save your persistent variables to a recipe, you can save and load via persistence utils, too.

     
  • josepmariarams - 2022-04-22

    Persistent vars works fine in Codesys for Raspberry. I am using ver 3.5.13 and it works.

    CoDeSys saves the persistence file on Raspbian shut-down. Are you stopping Raspbian normally?.

    If you save your persistent variables to a recipe, you can save and load via persistence utils, too.

     
  • josepmariarams - 2022-04-22

    Persistent vars works fine in Codesys for Raspberry. I am using ver 3.5.13 and it works.

    CoDeSys saves the persistence file on Raspbian shut-down. Are you stopping Raspbian normally?.

    If you save your persistent variables to a recipe, you can save and load via persistence utils, too.

     
  • josepmariarams - 2022-04-22

    Persistent vars works fine in Codesys for Raspberry. I am using ver 3.5.13 and it works.

    CoDeSys saves the persistence file on Raspbian shut-down. Are you stopping Raspbian normally?.

    If you save your persistent variables to a recipe, you can save and load via persistence utils, too.

     
  • josepmariarams - 2022-04-22

    Persistent vars works fine in Codesys for Raspberry. I am using ver 3.5.13 and it works.

    CoDeSys saves the persistence file on Raspbian shut-down. Are you stopping Raspbian normally?.

    If you save your persistent variables to a recipe, you can save and load via persistence utils, too.

     
  • damian177 - 2022-04-25

    In new environment generator works good. But in new codesys Ido not have Build(F11) i Rebuild , why? Please find in attachement.

    @josepmariarams I do Persistant Variables on file because my Rpi time to time isrestart by lack of power...- so I don't stopp Raspian normally.I hope it will work.

     
  • spiritcore - 2022-04-27

    I think I have the same issue, it works if I declare the variables in PLC_PRG but doesn't work with GVL...
    Did the same as here:
    https://forge.codesys.com/forge/talk/Engineering/thread/e837426615/#aa4c
    Is that a bug?
    UPDATE: not a bug, just needed to call the var from GVL in another program.

     
    πŸ‘
    2

    Last edit: spiritcore 2022-04-27
1 2 > >> (Page 1 of 2)

Log in to post a comment.