[Windows] How to synchronize "Persistent Data" among Filesystem ?

2020-02-22
2020-05-18
  • andre-teprom

    andre-teprom - 2020-02-22

    Hi there,

    Is there any Windows native command line able to immediately flush persistent data from Codesys to Disk ?

    I just came accross the following code, intended to save(/retrieve) data from Code(/File), but at some point it instantiate the Linux command sync which at first sight seems like there is no similar native option under CMD prompt command.

    I found this link, which gives the solution by installing an executable, which I wanted to avoid:

    Thanks in advance,
    Andre.

    PROGRAM PLC_PRG
    VAR
       first: BOOL:= TRUE;   
       application: POINTER TO APPLICATION;
       result: POINTER TO RTS_IEC_RESULT;
       storeRetains: BOOL := FALSE;
    END_VAR
    //NOTE: Flash memory has a high but limited amount of write cycles before it will begin to wear outThis means that the user should be careful not to write to flash
    //too often to avoid wearIt is recommeneded to only write persistent variables when changes are made to the persistent variable and not in a cyclic manner.
    //This function is executed at startup only once to load what is in the function.
    IF(first) THEN
       //Get the current application for use by the restore retains function below
       application:=AppGetCurrent(pResult:=result);
       
       //This function restores the retained variables from the file 'Application.ret' into the current application.  
       //If you keep the file name of your persistant variable file 'Application.ret' this function should not be needed as CODESYS should automatically retore
       //the retain file at startupThis is just shown as an example in case the user would like to maintain different retain files
       AppRestoreRetainsFromFile(pApp:=application, pszFilname:='Application.ret');
       first:= FALSE;
    END_IF
    //this function will execute one time when the 'storeRetains' boolean variable is turned to true;
    IF(storeRetains) THEN
       //this is a test retain variableIt will start at 0 since it is initialized that way in the PersistentVars object.
       //Everytime this function is executed the persistent variable will increase by 1 and store its new value
       test_persist_var:= test_persist_var + 1;
       //Get the current application for use by the restore retains function below
       application:=AppGetCurrent(pResult:=result);
       //Store the variables listed in the 'PersistentVars' object into the file 'Application.ret'.
       //You may change the file name to anything you prefer but the 'Application.ret' should be loaded automatically by CODESYS so if the file name is changed you
       //may need to load the file yourself as shown above
       AppStoreRetainsInFile(pApp:=application, pszFilname:='Application.ret');
       
       //CODESYS and the Linux filesystem keep files open for approximately 40 seconds after the write to reduce wear on the flash memoryIf power is abruptly lost
       //during that period of time the retains file can be corrupted and all user data can be lostIssuing the 'sync' command below to the Linux filesystem
       //will force the file to write and sync with the filesystem making it very difficult for the file to be corrupted.
       ShellCommand_System(command:='sync');
       
       //turn the 'storeRetains' variable back to false so this fuction won't execute until it is turned back to true
       storeRetains:= FALSE;
    END_IF
    
     
  • i-campbell

    i-campbell - 2020-02-23

    Does CAA File.Flush() do what you want?

     
  • andre-teprom

    andre-teprom - 2020-02-23

    I-Campbell hat geschrieben:
    Does CAA File.Flush() do what you want?

    Hi,

    Perhaps, but the above code already uses the library to handle files, and to be honest I`m unaware if there is replacement option for all functions used there. Is there any advantage that you know on using the CAA instead of the CmpApp?

    BTW:
    In the meantime, I tryied to add this library on the project, but seems as not available anywhere.
    Do you know any link from where I could download it?

    Thanks,
    Andre

     
  • i-campbell

    i-campbell - 2020-02-24

    Umm... So you're using the same version I am, CODESYS 3.5.15.30 so...
    Library Manager > Add Library
    Make sure the buttons are as shown
    insert the File Access, which gives you CAA File
    (alternatively, depress the "Show Advanced Libraries" button and search for CAA File)

    IMG: Screen Shot 2020

     
  • andre-teprom

    andre-teprom - 2020-02-24

    Worked like a charm ! (at least now it is able to compile )
    Since I'm out of office I cannot test on target device, but I'll update this thread with results along this week.

    BTW: I'm not sure whether I did not exactly as you meant by "CAA File.Flush()" stated above:
    Instead, the only option was for declaring as that:

       sFileName      :   File.CAA.FILENAME   :=   'Application.ret';
       hFileHandle   :    File.CAA.HANDLE;
       FileFlush      :   File.Flush();
    

    And instantiating this way:

       FileFlush ( xExecute:= storeRetains );
    

    Seems it correct?

    Thank you,
    Andre.

     
  • i-campbell

    i-campbell - 2020-02-24

    You would need to do this flush in addition to your CmpApp code.
    Open the file, flush the file, close the file.
    Look forward to your results.

     
  • GaryPratt

    GaryPratt - 2020-05-18

    I believe "ShellCommand_System()" is in a library provided by a hardware vendor (Cross Control). If file.flush doesn't work for you, you might see if your hardware vendor provides a way to execute shell commands.

     

Log in to post a comment.