AutoDoc
I have written a small test program in FBD that counts a CTU up on a one second rate (see attached file).  The variables I use for the CTU.PV and CTU.CV, I've set to "Retain" thinking that if my RPi loses power, that when he starts back up and auto-starts the OpenPLC logic again, the CTU.PV and CV values would be equal to where they were when the RPi went down.  This is not the case.  When the logic starts back up, CV starts at 0 and PV returns to its initial value instead of where I set it before the power cycle.  Am I missing something here?
Reply 0 0
thiagoralves
This should be the correct behavior. However, the implementation is broken. I might try to fix that in the future.

There is a "half implementation" of that called persistent storage, but I commented it out of the code because it was generating some compatibility issues with some platforms. Basically the persistent storage retains all variables, even if they don't have the retain property set. Check this forum post for more details: https://openplc.discussion.community/post/save-setpoints-to-the-sd-card-8488163
Reply 0 0
AutoDoc
Thanks for the quick reply.  I'll try to test the fix shortly and will report back here.  If a miracle occurs and I find some free time, I'll have a look at the source code and see if I can get it working better.  The problem is that I'm a PLC programmer and electrical guy, and not so much a C++ programmer, but hey, I didn't use to be a PLC programmer and I learned that so how hard can C++ be? [wink]
Reply 0 0
AutoDoc
I changed code and rebuilt as written here and restarted the RPi and now, from my Modbus Master (Radzio), all I see for my two integers is zero.  I browsed to port 8080 of the RPi and re-compiled the (unchanged) logic and it compiled OK and then restarted the OpenPLC object code and all was well.  I changed my counter setpoint and restarted the RPi (sudo reboot now), and when the OpenPLC logic auto-started (called from /etc/rc.local) the new counter setpoint was still there so it did work.  I then pulled power from the RPi, let it sit for 10 seconds, and re-powered it and the PLC logic auto-started OK, but had the old initial value in the counter setpoint.  No more time to work on this now, but will eventually get back to it.
Reply 0 0
thiagoralves
Can you show me the PLC log after power-cycling the PLC? Perhaps the persisten.file got corrupted and it had to discard it.
Reply 0 0
AutoDoc
Where can I find the log?  Also, where can I find the persistent file?
Reply 0 0
thiagoralves
On the OpenPLC webserver (the webpage you open to upload new ladder logic programs), click on PLC log. The persistent.file is located in the /core folder
Reply 0 0
AutoDoc
I reproduced problem as before and took log.  It is attached.  Also attached are two persistent files...One while running along, and the other after a power cycle when my setpoint went back to its initial value (60) instead of the last value (100).  BTW, I found the persistent file not in .../core, but in its parent directory .../OpenPLC_v2.
Reply 0 0
thiagoralves
The logfile shows that OpenPLC failed to read the contents of the persistent.file. Apparently the size it read was smaller than it expected. I'll have to check the code to see what is happening
Reply 0 0
AutoDoc
I'm wondering if it's maybe getting corrupted when we lose power.  Again, I'm no C++ programmer, but perhaps the file gets opened and stays that way the entire time that the program is running.  If so, would it be better (and not too time consuming) to open/write/close each time a variable changed?
Reply 0 0
thiagoralves
It is doing exactly that. There might be other issues with the code that I couldn't find on a quick look.
Reply 0 0
Reply