Retain Variables

ScottJones
2013-07-02
2018-01-08
  • ScottJones - 2013-07-02

    I need to save setting that are entered in Wachendorff Opus A1 RVC display and would like to use Retain Variable Flag. I have set the flag for the Variables that I want to save after power off, for Retain and Persistant. But it does not work. I have not found any good documentation on using Retained variables. Can anyone lead in me in the right direction on what I need to do in order to use RETAIN variables and save the settings that are made. Any thoughts would be appreciated. Thanks

    Scott

     
  • TimvH

    TimvH - 2013-07-03

    It depends on whether the PLC has a battery backed up memory and if the battery is OK.
    With the CODESYS Control RTE on PC based systems many tiimes there is no battery backup possible. In that case it is possible to save the retain data to a file and restore it after starting up again. Maybe that also works in your case.
    You could try the following:

    1)
    Add the following libraries to your project:
    - Standard.library
    - CmpApp.library

    2)
    Use the following code. In this case you have to manually set the xSave bit to TRUE to save it (could be a SAVE button which you create in your visualisation).
    Then do a warm reset and start the PLC again. Then you can test if the counter value is restored.

    PROGRAM PLC_PRG
    VAR
       xStartUp : BOOL := TRUE;
       xSaveRetain: BOOL;
       pApplication : POINTER TO CmpApp.APPLICATION;
       diMyResult : DINT;
       ton1 : TON;
    END_VAR
    VAR RETAIN
       iCounter: INT;
    END_VAR
    
    IF xStartUp THEN
       pApplication := CmpApp.AppFindApplicationByName('Application', ADR(diMyResult));
       CmpApp.AppRestoreRetainsFromFile(pApplication, 'C:\MyRetainData.ret');
       xStartUp := FALSE;
    END_IF
    ton1(IN:= NOT(ton1.Q), PT:= T#2S, Q=> , ET=> );
    IF ton1.Q THEN
       iCounter := iCounter + 1;
    END_IF
       
    IF xSaveRetain THEN
       AppStoreRetainsInFile(pApplication, 'C:\MyRetainData.ret');
       xSaveRetain := FALSE;
    END_IF
    
     
  • ScottJones - 2013-07-11

    TimvH, Thanks for the information an I think I almost got it, but it is not working yet. I have added the code example you posted to my program and as soon as I download the program to the device I get an Exception error, "Exception Detected see .". I am not sure why but in the original sample code you posted you have a variable "diMyResult" and was wondering what that variable needs to be set too. I posted the code I wrote below hoping you see a problem. Also, do you know where I can find any documentation on the CmpApp Library that goes into some detail on how the functions work. I can't find anything that was helpful. Thanks again. ScottJones

         pResult : RTS_IEC_RESULT ;
       retname : STRING;
       pApp : POINTER TO CmpApp.APPLICATION;]
    
    pApp := cmpapp.AppGetCurrent (ADR(pResult));
    retname := concat (pApp^.szName, '.ret');
    IF firststart THEN
       
       Heartbeat := ADR(CANStack^.m_uiHeartBeatTime); // Initialize the CAN Network Heartbeat
       Heartbeat^ := 2000;
       
       // Restore Retained Values 
       
       pApp := CmpApp.AppFindApplicationByName ('Application', ADR(pResult));
       CmpApp.AppRestoreRetainsFromFile (pApp, retname );
       
          
       firststart := FALSE;
       
    END_IF
    
    IF SaveRetain THEN
       pResult:= AppStoreRetainsInFile (pApp, retname );
       SaveRetain := FALSE;
       
    END_IF
    
     
  • TimvH

    TimvH - 2013-07-12

    It is probably related to the target that you get an exception, because I don't see any wrong coding.
    Maybe you can set a breakpoint in the first line and step through the application to see when the exception occurs.
    The "call stack" (menu: View) maybe also gives you an idea where the program was halted.

    A reason could be that you haven't specified the correct filepath + name.
    Can you write files to your controller, if yes, how do you specify the filename?

     
  • ScottJones - 2013-07-14

    Tim, Thank you for the help. I was able to get it working. I had originally had the variables initialized when they were declared, but deleted the initialization values when I started working on getting the Retain Variables to save to a file. When I did that, I created a division by zero error that I did not see right away. Also, I am not sure this is specific to Wachendorff displays but I did not need to use the Restore Retain function. Retains are restored automatically when the display is powered up. Again, thanks for you help it really helped me figure my problem out. Scott Jones

     
  • galexis - 2015-11-15

    Hi,
    I would like to do the same thing with raspberry pi, but I don't undestand where I have to put this code and which language is it.
    Could you post a project example ?
    Thank you very much.

     
  • TimvH

    TimvH - 2015-11-15

    First of all check if your device already supports retain variables (battery backed-up memory).
    If this is the case, you don't need to add any code to your application (like above) and it is done automatically.

    If it doesn't support it, you can use the code in the earlier posts and paste it in a "structured text" POU to check if it is working on your device.

    PS, there are also alternative ways to store your variables in a file.
    See the help of CODESYS for "Data Persistence". The "application composer" "persistence manager" could be a better solution.

     
  • galexis - 2015-11-15

    Thank you for your answer. Auto record of persistent data work only with proper shutdown. With my Rpi, nothing work: persistent are reset to 0 with normaly shutdown.
    I continu to search.

     
  • galexis - 2015-11-22

    On raspberry pi and codesys, where I have to record files ? /home/pi/MyRetainData.ret ?
    I have pt the same code on raspberry, cmpApp library, but it doesn't work. Problem of address files ? access ?

     
  • eschwellinger

    eschwellinger - 2015-11-22

    Hi,
    should work on Raspi too.
    You'll find the file in /root/

    BR
    Edwin

     
  • nabilla - 2018-01-06

    TimvH hat geschrieben:
    It depends on whether the PLC has a battery backed up memory and if the battery is OK.
    With the CODESYS Control RTE on PC based systems many tiimes there is no battery backup possible. In that case it is possible to save the retain data to a file and restore it after starting up again. Maybe that also works in your case.
    You could try the following:
    1)
    Add the following libraries to your project:
    - Standard.library
    - CmpApp.library
    2)
    Use the following code. In this case you have to manually set the xSave bit to TRUE to save it (could be a SAVE button which you create in your visualisation).
    Then do a warm reset and start the PLC again. Then you can test if the counter value is restored.

    PROGRAM PLC_PRG
    VAR
       xStartUp : BOOL := TRUE;
       xSaveRetain: BOOL;
       pApplication : POINTER TO CmpApp.APPLICATION;
       diMyResult : DINT;
       ton1 : TON;
    END_VAR
    VAR RETAIN
       iCounter: INT;
    END_VAR
    
    IF xStartUp THEN
       pApplication := CmpApp.AppFindApplicationByName('Application', ADR(diMyResult));
       CmpApp.AppRestoreRetainsFromFile(pApplication, 'C:\MyRetainData.ret');
       xStartUp := FALSE;
    END_IF
    ton1(IN:= NOT(ton1.Q), PT:= T#2S, Q=> , ET=> );
    IF ton1.Q THEN
       iCounter := iCounter + 1;
    END_IF
       
    IF xSaveRetain THEN
       AppStoreRetainsInFile(pApplication, 'C:\MyRetainData.ret');
       xSaveRetain := FALSE;
    END_IF
    

    Hi TimvH, I try your code in simulation mode. it works. I can restore the retain data, and also found the retain file (*.ret).
    And then I try to connect to my raspberry pi with same code, but in directory C:\, I can't find the retain file. and of course, the data won't be save and restore.
    What's wrong with this?

    Regards,
    Lala

     
  • eschwellinger

    eschwellinger - 2018-01-06

    Hi,
    on pi this is here:
    var/opt/codesys/PlcLogic/Application#
    Application.app Application.crc Application.ret

    BR
    Edwin

    I would not recommend to do this by saving the file manually this is a unreliable workaround isnt it?
    I would use the retain as they are designed for (having a UPS and a proper shutdown on Linux is mandatory - then you do not
    need to do anything in the application - the runtime System take care on all)

     
  • nabilla - 2018-01-06

    Edwin Schwellinger hat geschrieben:
    Hi,
    on pi this is here:
    var/opt/codesys/PlcLogic/Application#
    Application.app Application.crc Application.ret
    BR
    Edwin
    I would not recommend to do this by saving the file manually this is a unreliable workaround isnt it?
    I would use the retain as they are designed for (having a UPS and a proper shutdown on Linux is mandatory - then you do not
    need to do anything in the application - the runtime System take care on all)

    Thanks for your reply.

    ton1(IN:= NOT(ton1.Q), PT:= T#2S, Q=> , ET=> );
    IF ton1.Q THEN
       iCounter := iCounter + 1;
       xSaveRetain :=TRUE;
       AppStoreRetainsInFile(pApplication, 'C:\RetainData.ret');
    END_IF
    

    I modify the code, I use the timer for saving data (not using button anymore). For simulation mode, It's work, I can find the retain file in C:\User\AppData\Local\VirtualStore. But when I login with raspberry, there isn't retain file in C:.

    I need to save the value of counter. So when the raspberry start again after shutdown, the counter continue counting. I've already try with set the counter as VAR RETAIN PERSISTENT, it's working when shutdown or login-logout. But when I modify the file and download it, the value back to 0. Is it the way retain persistent work? or I did something wrong?

    Regards,
    Lala

     
  • eschwellinger

    eschwellinger - 2018-01-07

    Hi,
    as I told I would not recommend to do this that way,
    use the real retain mechanism with UPS.
    On this exesive writings you could detstroy the sd card and even if you write and the powersupply is switched off
    it may happen that the sd card is corrupt.
    The real retain only write on shutdown and restore the values on startup.
    BR
    Edwin

     
  • jbarrio - 2018-01-08

    Hello:
    saving variables every 2s is a bit dangerous, the risk of corrupting the SD is very great in a power cut.
    If you do not need the variables to be saved so many times, using a file is an option, I have it set to save every 12 hours and I have no problems and the display has a save button. I have not had problems with this system yet.
    What surprises me is that there is no way to save the retention variables without having to do a complete shutdown. It would be good for the system to give this option in some way, and to use the retain variable system, which I think can not be.

    regards

     

Log in to post a comment.