function and var questions


Recommended Posts

1)   Is the “var.myVar” declaration a double byte value? Are integer, single byte unsigned values used in DAQfactory or just the standard double byte? Is “var.” a private or global type or is it dependant on context?

Also, from the DAQ user guide example below, FileHandle is declared each time it is used. Is this necessary when using the “var.” declaration?

       Var.FileHandle = File.Open("myfile.txt",1,0,0,0)

       Var.strIn = File.Read(Var.FileHandle,10)



2)   I am creating a function sequence to use a lookup table for interpolating data to return a temperature. The function will be used throughout the document. Do I put the function statement (declaration) in an auto start sequence?

Can I use this function in a conversion expression?


Link to comment
Share on other sites

I put this in the auto start sequence and get an error C1000nat third line.

function LUTtmp(var.R)
   for(var.i = 0, i < NumRows(LUT), i++)
      if(R => LUT[i][0])
         var.T = (LUT[i-1][tt] * (LUT[i][0] - R) + LUT[i][tt] * (R - LUT[i-1][0])) / (LUT[i][0] - LUT[i-1][0])


Link to comment
Share on other sites

You really should not use var. notation.  It is left over from like 15 years ago.  You should instead be declaring your variables.  I mention this because var. is not valid in the function declaration.  It should just be:

function LUTtemp(R)

Your for() loop can be replaced as:

for (private i = 0, i < numrows(LUT), i++)

And the line with var.t:
private T = (LUT[i-1][tt] ....

Also note that you reference a variable tt in that line which is presumably global?

Note also that creating variables with var. makes them global in scope.


Link to comment
Share on other sites

Sorry, I didn't see your first post when I replied.

1) numbers in DAQFactory are always double precision float (64 bit) which means they can accurate represent all other common numeric data types (signed/unsigned 8, 16, 32 bit integers and 32 / 64 bit floating) except a 64 bit integer.  The to. and from. functions are useful if you want to force your numbers into these types.  

As to the second part, as I mentioned, you just shouldn't be using var. at all.  There may still be old samples that use it, but you should move away from it.  Use:

global myVariable

to declare a global variable, and:

private myPrivateVar

to declare a private variable.  You can do these inline with a for() statement only.  You can assign at the same time:

private myPrivateVar = x+3

2) the function should just be a sequence of the same name.  It does not need to be called except when needed, so no, not in an autostart sequence unless you actually need to call the function.  This is different from classes which need to be "run" in order to declare them.

Yes you can use a function in a conversion, but be careful.  Conversions need to run fast or they will hold up your data acquisition, and calling a user function is kind of slow even with the simplest of functions.  It is often better to create a background sequence that processes the data separately.  It really depends on how complex your application is.

Link to comment
Share on other sites

This sequence interpolates a temp value from a thermistor lookup table, resistance to temp with tempC in column 1 and tempF in column 2, ie. tt.   I can't get it to work.  The expression box in the pic shows pink, so I'm assuming the function is not being recognized.

I've changed things as suggested:

function LUTtmp(R)
    for(private i = 0, i < NumRows(LUT), i++)
      if(R => LUT[i][0])
         private T = (LUT[i-1][tt] * (LUT[i][0] - R) + LUT[i][tt] * (R - LUT[i-1][0])) / (LUT[i][0] - LUT[i-1][0])

Auto start sequence:

global string strTmpType = Registry.strTmpType
global tt
if(strTmpType == "C")
   tt= 1
else(strTmpType == "F")
   tt = 2
   // Error Window

Expression or conversion call:

function LUTtmp(Temp1[0])


Link to comment
Share on other sites

Well, first I'd make everything simpler by choosing a unit and using just that lookup table.  There is no reason to have a separate lookup table for F and C since you can just take the result and convert it.  Also, you can make this run a lot faster by simply using the search() function as long as the lookup table is in order (which you are assuming from your script...).

Finally to actually call the function, you call it like any built in function:


The keyword "function" does not belong in the expression.  It is only used in declaring the function.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.