Read and Write Persistant Variable Values


Recommended Posts


Im working on developing a test procedure thats reading a 4-20mA output from a device using a LabjackU3-HV. I have the input scaled down through a resistor network and I am using a conversion on the channel reading the 4-20 voltage. Right now I am using set global variables for the conversion that I captured by applying a constant 4mA and a constant 20mA value and used the voltages measured to develop the y=mx+b values.

What I would like to do is run a calibration sequence from time to time that would store the constants required to convert the measured voltage into the correspondin mA reading.

Here is a simple example of the calibration script I use to get the conversion values.

//Declare variables

Global LowmA

Global HimA

Global Riserun

Global Yint

// Prompt user to apply 4mA signal

system.MessageBox("Please apply constant 4mA signal then hit OK")

// wait for levels to settle


// Capture and record current shunt voltage for 4mA

LowmA = uv5_analog_output[0]

// Prompt user to apply 20mA Signal

system.MessageBox("Please apply constant 20mA signal then hit OK")

//wait for value to settle


//Capture and record current shunt voltage for 4mA

HimA = uv5_analog_output[0]

//Calculate Rise over Run

Riserun = ((hima - lowma)/16)

// Calculate Y intercept

Yint = (lowma-(4*riserun))

Then the conversion becomes (Value-yint)/Riserun placed on the measured value. In the test procedure I set vint and riserun to the calibrated values and that works perfectly. I would however like to have a calibration file that can be updated and referenced in the ConfigureLJ sequence I run upon opening the test procedure.

Any suggestions for a way to acieve this would be greatly appreciated.

Link to comment
Share on other sites

Four simple ways (and probably a host of others). Method #4 is the most elegant and most scalable, but requires scripting. Methods 1 and 2 work well for small apps and are super simple:

1) use registry variables. These are globals that persist to the registry. The problem with them is that they are limited to integers or strings, so if you have floating values (which you likely do) you either need to convert the number to a string and back, or add a multiplier to make it into an integer. For example, to save the value:

registry.strYInt = doubletostr(Yint)

and to restore it:

global YInt = strtodouble(registry.strYInt)

2) Use Test channels. Just create a channel for each value, device Type "Test", I/O type "D to A" and use them instead of global variables. Set the History and Persist to 1 and the values will remain. Set them to something larger if you want to see a history of calibrartion values over time that persists.

3) Use File. functions to create your own file. The File.readDelim() and file.writeDelim() make it pretty easy. You'll need to Open() and close() the file as well.

4) Use an object and the file.functions along with json support. This is for release 5.85+:

class CCals
   local LowmA = 0
   local HimA = 0 
   local Riserun = 0
   local Yint = 0

global cals = new(CCals)

Now you can reference the cal variables using . notation, for example:

cals.lowmA = uv5_analog_output[0]

To persist it, we still use File. functions, but its a bit easier:

   private handle ="c:\daqfactory\myCals.json",0,1,0,1)
   File.write(handle, cals.toJson())
   ? strLastError

To read, its very similar:

   private handle ="c:\daqfactory\myCals.json",1,0,0,1)
   private string datain =
   ? strLastError

Link to comment
Share on other sites

  • 4 weeks later...

Hi all,

I'm tring to apply the solution number 4, but I get an error while I execute the code relative to read the file.

I copy and paste the code from this post in 3 different sequences.

I am able to declare the class and to write the data in the file "mycals.json".

But when I try to read the data from the file I get the follow error:

"Failed to parse JSON String: read Line 4" where "read" is the name of the sequence where I copy the code.

I get the same error whit Xp and vista, DF version is 5.85.

What I am doing wrong?

Many thanks in advantage.


Link to comment
Share on other sites

Possibly nothing. Put a ? statement between the and the cals.fromJson():

? datain

That way you can see what gets read from the file. Post the result, or simply post the file.

Also, you probably don't want to call a sequence (or anything else for that matter) "read". "read" is a reserved word in sequence scripting and may interfere with the naming of your sequence.

Link to comment
Share on other sites


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