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?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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:POINTERTOCmpApp.APPLICATION;res:DINT;// Get application pointerapplication_ptr:=CmpApp.AppFindApplicationByName('Application',ADR(res));IFapplication_ptr<>0THEN// Restore the variablesCmpApp.AppRestoreRetainsFromFile(application_ptr,'/home/pi/retains.ret');// ...// Store the variablesCmpApp.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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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...
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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?
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 :
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
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.
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.
I'm glad it helped !
Also you should check out this post, there are other (maybe better) solutions for managing persistant variables :)
Hi,
definitely. I will check it out.
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!
Ok, I figured out that I do not call the program in the main task.
Thank you for the source code, it works!
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!
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...
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.
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