I have several variables from device Function Blocks that I want to be persistent if there was a download or a power cycle - VFD speed setpoints, analog alarm setpoints, PID parameter values, etc.
These are all declared within the Function Block as VAR PERSISTENT and added to a Persistent Variables List. The data is retained just fine if I download but I did notice that if I downloaded a different Codesys program to my HMI, then revert back to downloading the program that had the persistent variables, all those variables were initialized to either 0 or False.
So if for some reason, a customer’s HMI became non-functional, if I downloaded a backup copy of their Codesys file to a replacement HMI, none of those variable values would have remained.
What’s the best way to truly back up all the values of persistent variables for this case? I know there is a rather complex method of writing them to a .txt file then loading that file after a first time download but it appears that the Weintek Codesys does not support file operations.
Hi @JDControlsLLC ,
Thank you for contributing to our forum.
When using persistent variables, there is a save interval of approximately one minute. This delay allows the system to write the variable values to memory within the project. As such, please allow at least one minute to ensure the data is properly saved and retained.
Additionally, we recommend using the Retain
attribute. During testing, we found that after a power-cycle values in variables marked using the Retain
attribute remain unchanged. Similarly, when changing the CODESYS project, values assigned to variables of the same name used in a previously declared variable marked with the Retain
attribute remained unchanged. Here is a post with more information about memory attributes LINK
Please let us know the results of these suggestions. If the issue persists, could you provide more details about your project setup so we can assist you further?
What I did wasn’t changing the Codesys project to one with the same variable names. I mean as in I had a project loaded for one customer, then downloaded over it with a Codesys project for an entirely different customer to test code for that project.
Downloading the original project back to the HMI, even though I had a bunch of variables declared as VAR PERSISTENT, and added to a Persistent Variables list, all went to values of 0 or False, depending on variable type.
That’s what I want to avoid as it can cause a lot of headaches. So I need a way to ensure that those values are backed up.
In my particular example I had a bunch of device types - discrete valves, VFDs, analog inputs, etc. with various settings entered from the HMI. Things like alarm delay times, alarm setpoints, speed setpoints, etc.
Based on this happening, if a customer’s HMI crashed and needed to be replaced, downloading the backup Codesys program would leave all those values as 0/False.
How can I either avoid that happening or at least do some sort of export/import operation to get the values restored to whatever they were when the Codesys project was last saved?
Hi @JDControlsLLC
Just wanted update you that we are still working on a solution and will update you soon. Thank you for your patience.
Thanks and take your time, there is no rush on this. I’ve been in the process of building out a control library for Codesys + EBP with reusable popups, templates, etc. and creating some demo control system HMI applications to show potential customers. It’s a side project that I hope to turn into a solution for customers.
This runs on my HMI here that has Codesys activated and I had all sorts of alarm limits, VFD speeds, PID parameters, etc. stored for these demo applications. Then I downloaded over it because I was developing an HMI, with Codesys for PLC control, for a customer’s project.
After that was tested and done, I downloaded over it again with my control library stuff to get back to tinkering and noticed all the values were 0/false. That’s what sparked this question.
My thinking is what if a customer’s HMI goes bad for whatever reason? I could show up and install a new HMI, download the Codesys program, but all those configuration values are wiped and that’s no good.
Also, every time I use this HMI for developing a different project and revert back to my control library project, I’m going to lose all those values.
Hi @JDControlsLLC ,
Thank you for your patience. When downloading a different CODESYS project, previously retained data will be reset (perhaps through garbage collection) to make room for retained data within the new project. Therefore, to backup the value of CODESYS tags, a possible solution we came up with is using a Data Transfer (Global) object that is bit-triggered to transfer your data from CODESYS to Extended Memory (EM) registers. This will backup the previously retained values to an .emi file on the USB. Then, when you need to restore these values after overwriting the original project, you can plug the USB drive into your HMI and trigger a bit to read and transfer data from the emi file into CODESYS
When you want to retrieve your saved memory again, you set up data transfer objects that go from the EM registers back to the CODESYS memory registers. This is triggered by a separate bit.
Note: Objects 1 and 2 are set to LB 0 and will transfer data from CODESYS registers to EM registers (USB), while objects 3 and 4 do the opposite and transfer the data from EM registers back into CODESYS registers.
Below is a link to a demo project. Note that the EM register method is the easier solution we recommend. However, it can also be done through the recipe database, where a macro is used to retrieve values from the memory registers, store them in the recipe database, and then export them to a USB drive. Later, those values can be imported back in:
Project Demo Link
Ok I finally had a chance to do some testing with this. At first I thought it was going really poorly because I was unaware that triggering the backup to an .emi file rendered the HMI unusable for about 30 seconds. Some graphics disappeared and I thought I broke something. However, it did work just as I hoped.
I’ve got a button to trigger backing up a discrete valve open alarm timer setpoint to EM0, register 0. That writes to my USB. Then another button to trigger restoring that value back to the alarm delay timer after I’ve changed it.
This does achieve what I wanted but I do have some questions:
1] Is what I saw normal where the HMI is sort of frozen for about 30 seconds while the backup is executing? And will this be significantly longer if I’m backing up data to several dozen EMx registers?
2] Is there a local HMI bit that goes high while the backup is taking place? Maybe I could use it to trigger a popup window for notifying the user that a backup is in progress.
Hi @JDControlsLLC,
This is the first time we have used this method to “backup” parameter data, but it is not normal for the HMI to freeze. We will try to replicate this here in office as well and, if we can replicate it, advise a fix. I suppose that the HMI must re-establish a connection to the .emi file on the USB when the data transfer is triggered which may cause a slight increase in CPU load, but I don’t think that the the UI objects that already exist should not be made unusable by this method.
Using EM memory does not trigger a bit, but you can use the bit that you’ve declared to trigger the data transfer as in @Aaron.S example above.
Hi @JDControlsLLC,
I was not able to replicate the frozen HMI screen. Could you let me know how many data transfers you’re running from a single trigger?
Also, what brand of USB are you using for the data backup?
Sorry for the delay, I’ve been pretty busy and haven’t had time to mess with this in a week or so.
I’ve only got two transfers, both INT values, that transfer to the .emi file. I’m not really sure what kind of USB it is. Something I got from a vendor training a couple years ago.
When I hit the button to execute the transfer, that’s when it freezes up for 30 seconds or so.
Executing the transfer of those two values back into Codesys to populate those variables does not have any effect on the screen and it works perfectly.
Hi @JDControlsLLC,
Since the HMI does not maintain a constant connection with the EM file on your USB it may take a second or two to re–establish the connection but the long delay that you described does not occur with the units in our office. I would suggest that you try using a different USB drive just in case it is due to some kind of connection issue.
Retaining data from two separate projects that are periodically downloaded to the same unit can be a bit tricky. However, if the issue with the EM solution described above does not improve with a new USB drive, we will advise an alternative approach.
@JDControlsLLC,
As an alternative, you can use the “Save” and “Load” buttons within this project: 
Simply copy both buttons into your current project, and set the address of each data point you’d like to backup within the “data” array of each object, by reconfiguring the old addresses or by using the “New value…” button if you have more than 3. Then set the “len” value to the length of the array:
When in use, clicking the “Save” button will save data in a params.txt file on the USB. Then selecting the “Load” button will restore this data back to the original addresses.
@JDControlsLLC,
In case you have already downloaded the project file provided, I have updated the link above with a new project in which I made a few minor changes to the existing code.