« Close

Datasheets and User Guides

App Notes

Software & Driver


6.2 - Scripting basics

6.2.1 Assignment

The most basic useful sequence is probably one that sets a few output channels to different values. Assignment is done by simply specifying the object to assign a value to, an equal sign, and the value to assign. For example:

MyOutputChannel = 3 
AnotherOutput = 4 * 3 / 2 
YetAnotherOutput = 5 / InputChannel[0] 

The part to the left of the equal sign must be something that can take a value and cannot be an expression. This can typically be a channel or a variable. The part to the right of the equal sign can be any valid expression. If a sequence was created with just these three lines and run, the sequence would set MyOutputChannel to 3, AnotherOutput to 6 and YetAnotherOutput to 5 divided by the most recent value of InputChannel. The sequence would then stop.

6.2.2 Variables

Channels provide a place to store input and output readings and their associated histories. Often you'll need other places to store other values, such as calibration constants, counters, and flags. For this DAQFactory has variables. There are five types of variables in DAQFactory: global, local, private, registry, and static. Each variable type can be either a string or a number. DAQFactory also supports arrays of values. To use a variable you must create it first by declaring it. Declaring a variable is done in script by specifying the type of variable and the variable name. Like all other names in DAQFactory, a variable name must start with a letter and contain only letters, numbers and the underscore. For example, to declare a global variable called "Slope":

global slope

Or a private variable called "count":

private count

For string variables, we add the word string:

global string UserName

You can also do assignment while declaring the variable:

global slope = 3.92

In the examples that follow, will use a global variable called ID to store the LabJack ID, and then use this in place of a constant in all our script. Then, if we change the ID of the LabJack, we'll only have to change the script in one place.

So what do the data types mean?

global: these variables are accessible everywhere in DAQFactory, much like Channels.

private: these variables are only visible in the script that declared them. Use privates whenever possible, especially for counters used in for loops, etc.

local: these variables are used in custom devices, protocols, and user components. They are a bit more advanced and covered in the regular DAQFactory User's Guide.

static: these variables are like privates, but unlike privates maintain their values when the sequence that declared them restarts. This is a rather advanced variable type.

registry: these variables are globals, but are actually stored in the Window's registry and so maintain their values when DAQFactory restarts. There are limitations. Numeric registry variables are signed 32 bit values so must be between -2147483647 and +2147483647. Registry variables also don't support arrays. Finally, registry variables are declared and accessed differently. Please see the DAQFactory User's Guide for more information.

6.2.3 Calling Functions

There are a wide variety of functions available in DAQFactory scripting to allow you to do many advanced things like send email, FTP documents, pop up windows, and of course perform basic math functions like Sin, Cos and Tan.

Calling a function in DAQFactory is pretty much like every other scripting language, math tool, or even Excel. For example to get the Sin of 0.92, we would do:


This is a function that returns a value, in this case 0.7956016200364. There are also functions that perform an action, but don't return a value:


Functions can take a varying number of parameters. The Sin() example has one parameter, 0.92, while the email. send() example has none. To specify more than one parameter in a function, you should separate each parameter by a comma:


DAQFactory also allows you to create your own functions using Sequences. In fact, a sequence that you create in the Workspace can either be started on its own to run concurrently with other script, or called as a function from another script. If it is started on its own, it cannot have any parameters and it will not return a value. When called as a function you can pass in up to 20 parameters and return a value. So, for example, if you decided you don't like the fact that sin() takes angles in radians and want to create a function called MySin() that takes degrees instead:

1) Right click on SEQUENCES: in the Workspace and select Add Sequence

2) Enter the name

MySin and click OK.

3) In the script editor that appears, enter the following code:

function MySin(degs) 
   private result = sin(degs * pi() / 180) 

4) Click Apply and Compile, then click in the command part of the Command / Alert window (the bottom part) and put:

? MySin(90) 

and hit Enter. You'll see it will display 1, the sin of 90 degrees.

A few points:

a) you don't have to do the function declaration as shown in the first line of the MySin script, but doing so allows you to name your parameters and thus makes your code cleaner.

b) we could easily have combined the 2nd and 3rd lines of the script and skipped creating a private variable, but we wanted to show variable declaration in action.

6.2.4 Conditional Statements

Probably the most commonly used scripting statement is the if statement. The if statement allows you to check for a particular condition and perform a different action depending on whether that condition is true or not. For example, to set the Out channel to 3 if Pressure > 5 we would do:

if (Pressure[0] > 5) 
   Out = 3 

We use [0] after Pressure because Pressure is a channel with history and we only want to look at the most recent reading of Pressure. We can also add an else statement to perform a different action if the condition is not true. So, if we want to set Out to 4 if Pressure isn't > 5, we would do:

if (Pressure[0] > 5) 
   Out = 3 
   Out = 4 

We can perform more than one statement inside an if too:

if (Pressure[0] > 5) 
   Out = 3 
   Valve = 1 

We can also nest if statements:

if (Pressure[0] > 5) 
   Out = 3 
   if (Temperature[0] < 80) 
      Valve = 1 
      Valve = 0 
   Out = 4 

This last example will set Out to 3 if Pressure > 5, and to 4 if Pressure <= 5. If, and only if, Pressure > 5, it will also set Valve to 1 if Temperature < 80, and to 0 if Temperature >= 80. If Pressure <= 5, Valve will not be changed. You have to admit its easier to understand in script than in the last 3 sentences! (but only because the script is properly indented...)

6.2.5 Loops and Delay

Another common scripting element is the loop. There are several ways to do a loop in DAQFactory, but the most common is probably the while statement, especially while(1). The while / endwhile block will execute a group of statements as long as the while statement is true. So:

while (Pressure[0] > 5) 
   ? "Pressure High!!!!" 

This loop will, as long as Pressure > 5, print out a statement every second. Once Pressure goes <= 5, the code after the endwhile (if any) will execute.

The most common while loop is while(1). Since 1 is the same as true, this loop will execute forever, or at least until the sequence that contains it is stopped from outside. Because it executes forever, you should really only use while(1) inside of a sequence and not inside of a component action or an event. That said, its great for doing things like calibration loops:

while (1) 
   CalValve = 0 
   CalValve = 1 

The above loop will set the CalValve off for 10 minutes, then on for 1 minute and repeat this as long as the sequence runs.

It is important to notice that we always put a delay() inside of our loops. Delay() will pause the execution of the script for the specified number of seconds. Without the delay() the loop would run as fast as your computer will allow and probably starve out Windows, making it look like DAQFactory has hung.


Hi,  I'm very naive when it comes to scripting or coding....was hoping I could get some help?

I am attempting to create an array that can randomly choose a number from (0-2). Once a particular number (ex. 0) is pulled a specific operation will occur. Once a different number in the array (ex. 2) is pulled a different operation will be preformed...(so forth)

This is what I believe I have going on....

         global RandNumGen = RandUniform(0,2,1)             //Variable RandNumGen is "controlling"  RandUniform to create an array from (0-2)

               while (RandNumGen == 0)                           //listing the randomly chosen number in an array size of (1).                      
                       FIO4 = 1                                              //Now, while the variable RandNumGen is equal to (0); turn on FIO4(LED)
                endwhile                                                     //End loop

I understand that if I set the Variable RandNumGen to equal (0), then the light obviously turns on, but I cant seem to get the light to turn on even when the RandNumGen eventually gets to (0) in the array?

For the best DAQFactory scripting support, I suggest you post on AzeoTech's forum.