Retain / Persistent Variables in Codesys for Raspberry Pi

2022-04-12
2024-08-31
  • princessdvlk - 2022-04-12

    Hello all,
    I have a plc based on raspberry pi. I use Codesys v3.5.17.30 version for my programming. I want to create a variable whose value must be retained in the memory so that the plc must remember it even after the power failure. I often work on Persistent Variables in Codesys for the same and it works but for some reason with this raspberry pi hardware, it doesn't work.
    Could someone please help me how to get this done?

     
  • paulpotat

    paulpotat - 2022-04-13

    Hello,
    I had problems with that too, what you can do is use the AppRestoreRetainsFromFile and AppStoreRetainsInFile functions from the CmpApp library.

    Basically, you can use them like so :

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

    Your variables will be stored in a .ret file in the filesystem of your raspberry, so they will survive the reboot operations.
    One important part is that you don't need to select the variables you want to store / restore, because calling these functions will affect every persistant variables in your program at once.

    You can find a more detailed example of usage in this project.

    I hope this helps,
    BR

     
    • princessdvlk - 2022-04-14

      Hi!
      Thank you so much for your reply and for sharing this. I will check the project example too and write back to you.
      Fingers Crossed.

       
    • princessdvlk - 2022-04-14

      Hi,
      I tried your code and oh my god, you were right! It worked. YAY!
      I am so glad it worked because I was trying hard for the past 4 days but nothing clicked.
      However, I observed that to retain the changed values, the raspberry has to be shutdown properly instead of direct plug off. I mean when I directly turned off the supply and switched it back on, the pi showed me the previous values but not the changed ones. So, I shut it down properly and it worked!!
      Thank you so much for sharing this critical piece of information and helping me out.

       
      👍
      1
      • paulpotat

        paulpotat - 2022-04-14

        I'm glad it helped !
        Also you should check out this post, there are other (maybe better) solutions for managing persistant variables :)

         
        • princessdvlk - 2022-05-09

          Hi,
          definitely. I will check it out.

           
  • pietrobalint - 2024-08-17

    Hi,

    I am very new in Codesys so let me apologize for my dumb question.
    Could you explain how to impement this code mentioned before?

    Thank you very much!

     
  • pietrobalint - 2024-08-17

    Ok, I figured out that I do not call the program in the main task.
    Thank you for the source code, it works!

     
  • micik - 2024-08-20

    Hello pietrobalint,
    could you please how exactly you call this program? if not in the main task? Caould you please share a screenshot of how exactly you made it work?
    Thank you!

     
  • pietrobalint - 2024-08-20

    Hi micik,

    I am not sure I am using well the code, because I checked the directory and there is no retains.ret file.
    Now I deleted the code from my project and the values are still works as persistant variables after ssh reboot it seems to me they retain as their previous value. So I am a bit confused, maybe 4.12.0.0 Raspberry Pi SL solved the persitent variable problem...

     
  • pietrobalint - 2024-08-31

    I made hardware reboot and it still not working.
    I also tried Persistence Manager which need Application Composer Single License but Codesys support said that my runtime do not compatibile with it and it will avaiable in 2025 only. So I hope I will figured out how to use this code.

     
  • pietrobalint - 2024-08-31

    If I select "Update boot aplication" while I download the code the persistent variables got a constant startup value. But during the running I am still not able make the persistent variables to hold they actual value.

     

    Last edit: pietrobalint 2024-09-01

Log in to post a comment.