<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to Retain Variables</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/bfe46f72be/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/Engineering/thread/bfe46f72be/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/Engineering/thread/bfe46f72be/</id><updated>2020-02-27T14:07:37.788000Z</updated><subtitle>Recent posts to Retain Variables</subtitle><entry><title>Retain Variables</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/bfe46f72be/?limit=25#1a09" rel="alternate"/><published>2020-02-27T14:07:37.788000Z</published><updated>2020-02-27T14:07:37.788000Z</updated><author><name>jbarrio</name><uri>https://forge.codesys.com/u/jbarrio/</uri></author><id>https://forge.codesys.com833b281375183c22351c0a8bf3259857f167a8bd</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hello:&lt;br/&gt;
saving variables every 2s is a bit dangerous, the risk of corrupting the SD is very great in a power cut.&lt;br/&gt;
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.&lt;br/&gt;
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.&lt;/p&gt;
&lt;p&gt;regards&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Retain Variables</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/bfe46f72be/?limit=25#75fd" rel="alternate"/><published>2020-02-27T14:07:37.474000Z</published><updated>2020-02-27T14:07:37.474000Z</updated><author><name>eschwellinger</name><uri>https://forge.codesys.com/u/eschwellinger/</uri></author><id>https://forge.codesys.com7a9f946c6f2375d2334c43ba2afedfc3fd6fc79b</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hi,&lt;br/&gt;
as I told I would not recommend to do this that way,&lt;br/&gt;
use the real retain mechanism with UPS.&lt;br/&gt;
On this exesive writings you could detstroy the sd card and even if you write and the powersupply is switched off &lt;br/&gt;
it may happen that the sd card is corrupt.&lt;br/&gt;
The real retain only write on shutdown and restore the values on startup.&lt;br/&gt;
BR&lt;br/&gt;
Edwin&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Retain Variables</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/bfe46f72be/?limit=25#6273" rel="alternate"/><published>2020-02-27T14:07:37.143000Z</published><updated>2020-02-27T14:07:37.143000Z</updated><author><name>nabilla</name><uri>https://forge.codesys.com/u/nabilla/</uri></author><id>https://forge.codesys.com7c7a2c0ac39e9dd401f229015f0d172e1ef63a5b</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Edwin Schwellinger hat geschrieben:&lt;/strong&gt;&lt;br/&gt;
Hi,&lt;br/&gt;
on pi this is here:&lt;br/&gt;
var/opt/codesys/PlcLogic/Application# &lt;br/&gt;
Application.app  Application.crc  Application.ret&lt;br/&gt;
BR&lt;br/&gt;
Edwin&lt;br/&gt;
I would not recommend to do this by saving the file manually this is a unreliable workaround isnt it?&lt;br/&gt;
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&lt;br/&gt;
need to do anything in the application - the runtime System take care on all)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Thanks for your reply.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nv"&gt;ton1&lt;/span&gt;&lt;span class="ss"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;IN&lt;/span&gt;:&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;NOT&lt;/span&gt;&lt;span class="ss"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;ton1&lt;/span&gt;.&lt;span class="nv"&gt;Q&lt;/span&gt;&lt;span class="ss"&gt;)&lt;/span&gt;, &lt;span class="nv"&gt;PT&lt;/span&gt;:&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;T&lt;/span&gt;&lt;span class="sc"&gt;#2&lt;/span&gt;&lt;span class="nv"&gt;S&lt;/span&gt;, &lt;span class="nv"&gt;Q&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; , &lt;span class="nv"&gt;ET&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="ss"&gt;)&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;IF&lt;/span&gt; &lt;span class="nv"&gt;ton1&lt;/span&gt;.&lt;span class="nv"&gt;Q&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt;
   &lt;span class="nv"&gt;iCounter&lt;/span&gt; :&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;iCounter&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
   &lt;span class="nv"&gt;xSaveRetain&lt;/span&gt; :&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;TRUE&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
   &lt;span class="nv"&gt;AppStoreRetainsInFile&lt;/span&gt;&lt;span class="ss"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;pApplication&lt;/span&gt;, &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;C:\RetainData.ret&lt;/span&gt;&lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="ss"&gt;)&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;END_IF&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;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:.&lt;/p&gt;
&lt;p&gt;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? &lt;/p&gt;
&lt;p&gt;Regards,&lt;br/&gt;
Lala&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Retain Variables</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/bfe46f72be/?limit=25#1a89" rel="alternate"/><published>2020-02-27T14:07:36.215000Z</published><updated>2020-02-27T14:07:36.215000Z</updated><author><name>eschwellinger</name><uri>https://forge.codesys.com/u/eschwellinger/</uri></author><id>https://forge.codesys.com2e47bff2ab2d7269230e2e8644e4a018597bd028</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hi,&lt;br/&gt;
on pi this is here:&lt;br/&gt;
var/opt/codesys/PlcLogic/Application# &lt;br/&gt;
Application.app  Application.crc  Application.ret  &lt;/p&gt;
&lt;p&gt;BR&lt;br/&gt;
Edwin&lt;/p&gt;
&lt;p&gt;I would not recommend to do this by saving the file manually this is a unreliable workaround isnt it?&lt;br/&gt;
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&lt;br/&gt;
need to do anything in the application - the runtime System take care on all)&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Retain Variables</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/bfe46f72be/?limit=25#5cdb" rel="alternate"/><published>2020-02-27T14:07:35.849000Z</published><updated>2020-02-27T14:07:35.849000Z</updated><author><name>nabilla</name><uri>https://forge.codesys.com/u/nabilla/</uri></author><id>https://forge.codesys.com793a738de9e623d3c98b1068fba8edff1b2499f4</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TimvH hat geschrieben:&lt;/strong&gt;&lt;br/&gt;
It depends on whether the PLC has a battery backed up memory and if the battery is OK.&lt;br/&gt;
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.&lt;br/&gt;
You could try the following:&lt;br/&gt;
1)&lt;br/&gt;
Add the following libraries to your project:&lt;br/&gt;
- Standard.library&lt;br/&gt;
- CmpApp.library&lt;br/&gt;
2)&lt;br/&gt;
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).&lt;br/&gt;
Then do a warm reset and start the PLC again. Then you can test if the counter value is restored.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nt"&gt;PROGRAM&lt;/span&gt; &lt;span class="nt"&gt;PLC_PRG&lt;/span&gt;
&lt;span class="nt"&gt;VAR&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nt"&gt;xStartUp&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;BOOL&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nt"&gt;TRUE&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nt"&gt;xSaveRetain&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;BOOL&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nt"&gt;pApplication&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;POINTER&lt;/span&gt; &lt;span class="nt"&gt;TO&lt;/span&gt; &lt;span class="nt"&gt;CmpApp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;APPLICATION&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nt"&gt;diMyResult&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;DINT&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nt"&gt;ton1&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;TON&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;END_VAR&lt;/span&gt;
&lt;span class="nt"&gt;VAR&lt;/span&gt; &lt;span class="nt"&gt;RETAIN&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="nt"&gt;iCounter&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;INT&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;END_VAR&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="k"&gt;IF&lt;/span&gt; &lt;span class="nv"&gt;xStartUp&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt;
   &lt;span class="nv"&gt;pApplication&lt;/span&gt; :&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;CmpApp&lt;/span&gt;.&lt;span class="nv"&gt;AppFindApplicationByName&lt;/span&gt;&lt;span class="ss"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Application&lt;/span&gt;&lt;span class="s1"&gt;'&lt;/span&gt;, &lt;span class="nv"&gt;ADR&lt;/span&gt;&lt;span class="ss"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;diMyResult&lt;/span&gt;&lt;span class="ss"&gt;))&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
   &lt;span class="nv"&gt;CmpApp&lt;/span&gt;.&lt;span class="nv"&gt;AppRestoreRetainsFromFile&lt;/span&gt;&lt;span class="ss"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;pApplication&lt;/span&gt;, &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;C:\MyRetainData.ret&lt;/span&gt;&lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="ss"&gt;)&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
   &lt;span class="nv"&gt;xStartUp&lt;/span&gt; :&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;FALSE&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;END_IF&lt;/span&gt;
&lt;span class="nv"&gt;ton1&lt;/span&gt;&lt;span class="ss"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;IN&lt;/span&gt;:&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;NOT&lt;/span&gt;&lt;span class="ss"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;ton1&lt;/span&gt;.&lt;span class="nv"&gt;Q&lt;/span&gt;&lt;span class="ss"&gt;)&lt;/span&gt;, &lt;span class="nv"&gt;PT&lt;/span&gt;:&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;T&lt;/span&gt;&lt;span class="sc"&gt;#2&lt;/span&gt;&lt;span class="nv"&gt;S&lt;/span&gt;, &lt;span class="nv"&gt;Q&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; , &lt;span class="nv"&gt;ET&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="ss"&gt;)&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;IF&lt;/span&gt; &lt;span class="nv"&gt;ton1&lt;/span&gt;.&lt;span class="nv"&gt;Q&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt;
   &lt;span class="nv"&gt;iCounter&lt;/span&gt; :&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;iCounter&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;END_IF&lt;/span&gt;
   
&lt;span class="k"&gt;IF&lt;/span&gt; &lt;span class="nv"&gt;xSaveRetain&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt;
   &lt;span class="nv"&gt;AppStoreRetainsInFile&lt;/span&gt;&lt;span class="ss"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;pApplication&lt;/span&gt;, &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;C:\MyRetainData.ret&lt;/span&gt;&lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="ss"&gt;)&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
   &lt;span class="nv"&gt;xSaveRetain&lt;/span&gt; :&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;FALSE&lt;/span&gt;&lt;span class="c1"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;END_IF&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Hi TimvH, I try your code in simulation mode. it works. I can restore the retain data, and also found the retain file (*.ret).&lt;br/&gt;
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.&lt;br/&gt;
 What's wrong with this?&lt;/p&gt;
&lt;p&gt;Regards,&lt;br/&gt;
Lala&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Retain Variables</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/bfe46f72be/?limit=25#ce90" rel="alternate"/><published>2020-02-27T14:07:35.539000Z</published><updated>2020-02-27T14:07:35.539000Z</updated><author><name>galexis</name><uri>https://forge.codesys.com/u/galexis/</uri></author><id>https://forge.codesys.com7fd61eb90e7123b32cbcbe3d9821fc7d0eeb7ca8</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Ok, solution in files.&lt;/p&gt;
&lt;p&gt;&lt;a class="" href="https://forum.codesys.com//download/file.php?id=1431" rel="nofollow"&gt;Mémorisation variables.project &lt;span&gt;&lt;span&gt;[176.71 KiB]&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Retain Variables</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/bfe46f72be/?limit=25#384b" rel="alternate"/><published>2020-02-27T14:07:34.618000Z</published><updated>2020-02-27T14:07:34.618000Z</updated><author><name>eschwellinger</name><uri>https://forge.codesys.com/u/eschwellinger/</uri></author><id>https://forge.codesys.comdae5de9f11f8cf146af29dff6c061177bdac0a53</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hi,&lt;br/&gt;
should work on Raspi too.&lt;br/&gt;
You'll find the file in /root/&lt;/p&gt;
&lt;p&gt;BR&lt;br/&gt;
Edwin&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Retain Variables</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/bfe46f72be/?limit=25#6915" rel="alternate"/><published>2020-02-27T14:07:34.309000Z</published><updated>2020-02-27T14:07:34.309000Z</updated><author><name>galexis</name><uri>https://forge.codesys.com/u/galexis/</uri></author><id>https://forge.codesys.comc069cd22cf5d78af05e6882c594279e708660d60</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;On raspberry pi and codesys, where I have to record files ? /home/pi/MyRetainData.ret ?&lt;br/&gt;
I have pt the same code on raspberry, cmpApp library, but it doesn't work. Problem of address files ? access ?&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Retain Variables</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/bfe46f72be/?limit=25#85a8" rel="alternate"/><published>2020-02-27T14:07:34.004000Z</published><updated>2020-02-27T14:07:34.004000Z</updated><author><name>galexis</name><uri>https://forge.codesys.com/u/galexis/</uri></author><id>https://forge.codesys.com8142e09b1cd26d9131c882528d37ba7add462b9f</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;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.&lt;br/&gt;
I continu to search.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Retain Variables</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/bfe46f72be/?limit=25#293b" rel="alternate"/><published>2020-02-27T14:07:33.703000Z</published><updated>2020-02-27T14:07:33.703000Z</updated><author><name>timvh</name><uri>https://forge.codesys.com/u/timvh/</uri></author><id>https://forge.codesys.combc46bc9c650a5dd8971d50997132105bee58e3ac</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;First of all check if your device already supports retain variables (battery backed-up memory).&lt;br/&gt;
If this is the case, you don't need to add any code to your application (like above) and it is done automatically.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;PS, there are also alternative ways to store your variables in a file.&lt;br/&gt;
See the help of CODESYS for "Data Persistence". The "application composer" "persistence manager" could be a better solution.&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>