Persistant Variables - anyone use them?
TrikinCurt
Posts: 158
Does anyone here use persistent variables? I was making it so users can save their own radio presets and persistent variables seemed like a good way. A few times my variables disappeared. Can anyone tell me in what situations they lose their values? Rebooting the controller doesn't do it, yet I came in this morning and they were gone!
Am I going to have to learn how to use file commands?
Am I going to have to learn how to use file commands?
0
Comments
I don't know how to explain how your values are getting lost. Could be a code issue, or it could be other people sending code to your master when you're away.
which means every time you resend code to your master the variables get reset.
this is also very nice
yuri wrote: The persistent variable in you main program should retain its value even after you resend code to your master. I just re-compiled and resent the program and my persistent variables are holding their previous value.
Are you sure your not initializing them in start up some where?
Jeff
No, there is nothing in the DEFINE_START section that modifies the values of my PVs. And no, they were not in any module - just in the normal expected places (.axs & .axi files). And no, I didn't change/delete them in my code between program uploads. Can you tell it's frustrating for those of us who have this issue?
I've opted for reading/writing XML files instead, which has been 100% rock solid reliable. If you are having trouble with PVs, I'd suggest going straight for XML files instead. My approach was to create a "prefs" structure with all the values I wanted to save in the one structure. You then just call variable_to_xml() to generate and XML string, and write the string to a file. Message me is you want to see my code.
Note that the AMX XML string does not have end-of-line characters, and I've found that the AMX master doesn't parse XML files correctly (using xml_to_variable) if you add them in to make editing easier. Use an XML editor like Pete's XML Editor http://www.iol.ie/~pxe/ to edit your files instead of Notepad.
Roger McLean
Swinburne University
Strange, I use them al the time and have never had a single issue with them. (At least nothing that couldn't be explained by something I did to goof them up.)
I use them for presets, Cable/Satellite channel linesups (upwards of 1000 names and channel numbers) power states, etc... I store weather info in them too so when I reboot, weather info comes up quicker on the touch panels.
I do know that you cannot use all the RAM available for them as the Netlinx master needs quite a bit of that space itself. I try to keep my useage to about 30-60% of the available RAM.
Other than that I?ve found that persistent variables are persistently reliable. I don?t use them as much anymore but I?ve had no problems with persistent variables at all.
I did run across something by mistake that I didn?t know. You can initialize a persistent variable to a value other than 0 when you declare it and that value only gets plugged in the very first time the code gets loaded.
Consider the following program:
After the program gets loaded, drop nFirstTime into the debugger and you?ll see the value is 1. Change the value to 0. If you then reboot or reload the code the value of nFirstTime will still be 0, it won?t get reset back to 1.
Maybe this is already common knowledge and I?m just stating the obvious but I didn?t know this option existed.
I had my troubles on a freshly purchased batch of NI-3100s. But I guess I can't vouch for how long they were standing in storage prior to us purchasing them. Unfortunately NetLinx masters use a timekeeper rather than plain CR batteries from the Axcent days. I think I'll just leave them be, since XML covers PV issues and RMS does covers time-syncing for me.
Roger McLean
Swinburne University
I don't think it works this way. It is exactly as stated: it is initialized to zero the FIRST time the program is loaded . If you resend it isn't the first time the program is loaded. They will reset to zero if you rename your program, or save as a file revision.
Richard
hmm, the problems i have had with persistent variables are probably due to using them in modules.
Can you define a persistent variable in main, then pass it to a module to use it, or doesn't that work?
Pauld
You mean passing variables from DEFINE_MODULE in your main right?
The other way around. If you need the variable to be persistent, you have to define it in your man program, then pass it to the module as a parameter. Since NetLinx passes parameters by reference, any changes to it in the module become the "new value" that will survive subsequent reloads.
that's what i was saying
<-- dutchie
I get a syntax error trying to pass in radioPresets that way. For what it is worth , the structure is defined
http://www.amxforums.com/showpost.php?p=23920&postcount=21
HTH