Saving Variable Values


Recommended Posts

I have a Labjack U3 being controlled by DAQFactory. In turn they run a weather station for me. All is well until a power failure or I need to restart the computer and then of course all the values for the variables are lost. Exporting them to an outside file would be a useful backup. What would be the most efficient way of doing this please?

Thanks

Mike

Link to comment
Share on other sites

The easiest way to persist variables is by either using registry variables, or by using a Test D/A channel and giving it a history and Persist of 1. Registry variables are a little slower to access than memory variables. They are limited to strings and integers, so you have to either scale floating values or convert to a string. Because of these items, you will usually read them in for initialization and then update them whenever the corresponding memory variable is changed. That's pretty hard to maintain. Using channels is easier. You can use them just like variables and they are pretty fast. However, if you have a smaller version of DAQFactory, you might have a channel count limitation from your license.

Alternatively, you can write a file, but like registry variables, you'll have to rewrite the file whenever a value is changed.

Link to comment
Share on other sites

Just a thought, this is why I like to use V.Channels for my "constants"...Turn the history count down to 1 (or 2) and use them for "sticky settings" or math constants! They will stick around upon restart... :)

Link to comment
Share on other sites

V channels don't stick around on restart. They only stick around if you actually Save your document, which you can trigger with system.saveDocument(), but that's not really designed to be called a lot. If you do you run the risk of corrupting your .ctl document.

Link to comment
Share on other sites

I almost forgot about that... :bonk:

I am pretty sure I scripted in System.SaveDocument() into our "Quit" button, so the V.Chans would be saved... :)

Link to comment
Share on other sites

Which is ok, unless you have a power failure or other problem and DAQFactory doesn't close with the quit button. Personally I think using Test channels is a better choice as persist files are updated immediately. I never use V channels for anything myself, not that there is anything wrong with them. Regular Test channels are just much more useful.

Link to comment
Share on other sites

write_read_s_chan.txt

Depending how often you want to backup... Irun this code every 30 minutes for standard channels, virtual channels and misc variables.

This only works if your *.ctl file does NOT start collecing data on startup.!!!!!!!!!!

This code only shows standard channels but others are similar. My data interval is every 6 minutes.

When you restart your *.ctl file, you must clear all channel (virtual and standard) data, read in all your channel data and any misc vars, fill missing data for the period the system was down, then start data collection.

Link to comment
Share on other sites

Thank you for all the thoughts guys. Sorry to have not replied earlier, but work gets in the way of enjoying yourself! I think the consensus is that TestChannels are the way to go, so that is what i shall look at. I had thought of exporting the values via scripting, but the Channels method seems more effective.

Best wishes

Mike

Link to comment
Share on other sites

It seems that Test Channels were not as easy as I thought. Checked out pages 336/337 in the manual and cannot work out the process. I have created the test channel, but my problem is how to store data in it. I am using the data from another channel modified by a complex formula. The results are displayed elsewhere in the DAQfactory, and are specified as global variables. How can I put those into the channel?

Sorry to be so naive about this but I really cannot find anywhere how to do it.

Thanks

Mike

Link to comment
Share on other sites

The easiest way to do it is to create a Test D/A channel. Then you can just do:

myChannel = someComplexFormula

If you use other types of test channels, you have to make sure and set timing to 0, and use AddValue(). AddValue() is faster than using a D/A and =, but its not as convenient, so unless you need the speed, use D/A and =. After that, the channel is just like any other channel. In your case, set the History and Persist to 1 so the last setting remains.

Link to comment
Share on other sites

Thanks Guru that is what I am attempting to do, but where does the "myChannel = someComplexFormula" go. Would it go in Sequences, or somewhere else, that is what I am really confused about. Silly, i know, but can't work the logic of it at the moment.

Thanks

Mike

Link to comment
Share on other sites

  • 5 months later...

I am trying to store a value in a Channel as a "constant". This Constant is a say calibration value which will change from time to time.

 

I have tried to make a new Channel according to the post above : Quote

 

The easiest way to do it is to create a Test D/A channel. Then you can just do:

myChannel = someComplexFormula  END Quote

 

What do I fill in the Channel Table View to allow this to happen?

 

I am clearly missing the point completely.

 

I am a Labjack U3 on Wondows 8.1. All is working well.

 

I am currently using the Registry to store calibration values and want to bring these values within direct control of DAQFactory as this willl be more reliable.

 

I have had issues with Profiles becoming damaged and this results in the loss of the registry data!

 

Help someone please.

 

Thank you.

 

Neil

Link to comment
Share on other sites

OK, first, to answer your direct question, create a new channel, Device Type = "Test", D# = 0, I/O type = "D to A", and Channel # = 0 (or unique among your other test D/A channels).

 

Now, as to settings, since registry access is getting challenging with Windows, I thought I'd offer a nice little pattern for persisting settings to a file using objects:

 

class CSettings
   // change to list all the settings variables
   local somesetting = 3
   local string somestringsetting = "defaultValue"
   
   /// do not change anything below this point
   local string transient settingsPath = "c:\DAQFactory\settings.json"
   
   function loadSettings()
      private handle = file.Open(settingsPath, 1, 0, 0, 1)
      private string datain = file.Read(handle)
      file.Close(handle)
      fromJson(datain)
   endfunction
   
   function saveSettings()
      private handle = file.Open(settingsPath, 0, 1, 0, 1)      
      file.Write(handle, toJson())
      file.Close(handle)
   endfunction
   
endclass


global gSettings = new(CSettings)

The above code creates a class called CSettings that is a template to hold various settings.  In this case, I have somesetting (numeric) and somestringsetting.  You should change those and add to create your own variables.  Leave everything else as is.  At the end, it instantiates this class into an object and stores it in gSettings.  You can rename gSettings to something shorter if you want.  Then, to access your settings, you just put:

 

gSettings.somesetting

 

or

 

gSettings.somestringsetting

 

You can do this from anywhere.  Finally, you can save whatever the current settings are by doing:

 

gSettings.saveSettings()

 

You can then restore settings by doing:

 

gSettings.loadSettings()

 

Run the above code once in startup first, or any time you add or change the settings variables you want to work with.  

Link to comment
Share on other sites

Wow what a fantastic answer. Now I shall try to implement that!

 

Got the Test Channel to work - thank you. Simple when you know how. I had been using the I/O type as A/D  and of course that is driven continuously as an input.

 

Many thanks this has bee a great help

 

Neil

Link to comment
Share on other sites

  • 3 weeks later...

I have now got back to try to save variables to a json file as above.

 

1.   I have a sequence From_DAQ_Web with the given code above. I have changed the items to save to :

local somesetting = Gsomesetting
   local string somestringsetting = Gsomestringsetting
   local string strMessage1 = GstrMessage1

 

2. I have another sequence with  gSettings.saveSettings() - only.

 

3. I have another sequence Variables as below :

             global Gsomesetting = 2222222
             global string Gsomestringsetting = "Fools"
             global string GstrMessage1 = "and horses"

Link to comment
Share on other sites

OH dear posted that before I was finished - messed that one. Hope you can join the two together.

 

OK working from memory I had not given my variables. They are :

       local somesetting = Gsomesetting
       local string somestringsetting = Gsomestringsetting
       local string strMessage1 = GstrMessage1

 

I run the Variables sequence followed by the From_DAQ_WEB and then the Save_Settings sequence

 

If I then go look in the json file I can see the saved data. So far so good.

 

If I then change the Variables values and re-run the sequences and nothing happens. I have been sure to close the json file.

Looks like it thinks the file is open and will not write to it. Date and time does change though.

 

I am struggling with the local declaration of the variable. How do I bring the saved data back into the program to view it globally?

 

I have scoured the manual but just cannot figure out how to do this. I use the Watch and Command /Alert windows but cannot see the local declarations in Watch.

 

I apprecaite your advice or anyones.

 

Thank you

Link to comment
Share on other sites

Further to the previous.

 

When I change the json file name and then re-run the sequenses a new file is created with the changed data. If I then repeat the process and change the data it create a fresh json file with the previous old data i it?

 

Not sure if that is any help to solve the issue.

 

Thanks

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.