Getting Started with ULx for NI LabVIEW Part 2
In part one, we learned how what is the minimum amount of VIs needed to acquire one analog data sample and display it on the NI LabVIEW front panel. In part 2 we will add to this example by including:
Display the data in a stripchart,
Saving the data to file.
Taking one sample of data each time you click on a button is good, but having it acquire data continuously is better! To accomplish this, we will add a ‘While Loop.’
Here is where we left off:
From the Block Diagram, right click anywhere there is white space to pop up the Functions menu. Select Express » Exec Control » While Loop. Your cursor will change to this . Place the cursor between and above the two ULx VIs on the block diagram, click and hold your left mouse button down, then drag the cursor until it is to the right and below the data.VI. Your diagram will look like this:
Plus you will see a ‘Stop’ button on your Front Panel. Feel free to resize and move this on the panel to a size and place pleasing to you.
You can click the run button and the VI will run, but the data may change too quickly to be useful. Another problem is you don’t know if the data is being updated at a uniform rate or you may be getting too much data. Most stripchart and data logging applications require data to be collected in samples per second, per minute, or even per hour.
A basic question here is “Why do I need to use a while loop, when I can just click on the Continuous Run button of NI LabVIEW?” The answer is you can, but there are several reasons why you might not want to. This is a very simple example, most people will want to more or much more in their VI to get the data and manipulate it in the way needed. With the example the way it was, each time the VI executes it is creating the task then destroying the task, and then repeating the process. That is not as efficient as creating the task then looping on getting a new data sample and doing something useful with it. It may not sound like much of a ‘savings’ now, but when your VI’s get more complicated you will see the value.
To slow the looping down, we will add the Time Delay.VI. It is located on the Functions Palette, select Express » Exec Control, then click and drag it to inside the loop as shown here.
As you can see, a dialog box pops up with this VI, asking you to set the time delay. For the purposes of this example, I set it to 0.100 seconds so that I get a new sample 10 times a second or 100 mS.
Let’s go ahead and run this from the Front Panel. You’ll see the data is now updated at a much slower rate. To stop the execution, click on the Stop button you added and not the Abort Execution button on the NI LabVIEW menu.
“Why does this make a difference?” you may ask. As stated above, in this example, it doesn’t make much difference, but when your VIs are running multiple loops, and high speed scans, the Abort Execution button forces the VI to terminate in potentially bad ways such as not ending a scan correctly or leaving external devices such as relays, solenoids, belts and processes still running why may be hazardous to people and equipment around you. It is usually best to learn to do this the proper way, and as I have read in numerous NI LabVIEW books and the help, the right way to end looping is via the Stop button, not the Abort Execution.
In its current state, this example can run indefinitely. That is, as long as the USB-1608G stays connected, powered, and so long as no external influence damages the device. But what is something like that should occur? We would want 2 things to happen. First we’d want to stop the procedure, in this case our loop, and second, we’d want to be notified there was a problem. To handle these matters, we add error handling.
First we’ll add a way to show there is a problem. To do this, we add a Simple Error Handler.VI to our VI. Hover your mouse over the Read.VI, and move it to the Error out pin. When the cursor changes to the wiring tool, right click to pop up the menu, select Dialog & User Interface Palette, then click on Simple Error Handler.VI and drag it to the right of the Read.VI, outside the while loop. Wire the Read.VI’s error out pin to the Simple Error Handler.VI’s error in. Next hover mouse over the Simple Error Handler.VI, locate the type of dialog pin, right click, select Create» Constant. It will be added as “OK message,” which is fine for this application.
Now we will add a way to exit the While Loop when there is a problem. Within the while loop, place your mouse cursor on the error out wire from the Read.VI. When the wiring tool appears, right click, select Cluster, Class & Variant Palette. Add an Unbundle.VI below and to the right of the Read.VI. Wire the Read’s error out to the Unbundle’s error in. You may need to widen the while loop to make it all fit, if so, left mouse click on the blue drag handle on the right side of the while loop and drag it to the right. You’ll need to move the conditional terminal to the right as well. Delete the wire between the Stop button and the conditional terminal. You do this by left mouse clicking on the wire to select it, then press your keyboard’s “Delete” key. We want to add an OR gate so if the Stop button is clicked OR it there is an error the loop stops. Right mouse click on the output of the Unbundle.VI, select Boolean Palette and add an OR gate to the right of the Unbundle.VI. If the OR gate does not already have one of its inputs wired to the Unbundle.VI, wire the other input to the Stop button, and wire the output of the OR gate to the conditional terminal. Your Block diagram should look like this:
Run this now, then remove the USB cable from the USB-1608G. You should see the following error message pop up:
When you click on Continue, notice execution of the VI has stopped. This is exactly what we want to occur.
By now I hope you are getting the hang of this. There are 2 more goals to this Knowledge Base article, to add a stripchart and to log the data to a file. Because NI LabVIEW includes Express VIs for these functions, I hope you’ll find this easy to accomplish.
Display the data in a strip chart
In addition to displaying the data numerically, let’s add a Chart. From the Front panel, right click on the unused gray area, and select Express» Graph Indicators, click on Chart and drag it onto the Front Panel.
It should look like this:
You’ll need to add a wire from the data terminal (pin) of the Read.VI to the data pin on the Waveform Chart.VI. Your Block Diagram should look like this:
You can run this, but before you do, did you remember to plug the USB cable back in to your device after the error test?
Your Front Panel should look something like this while it is acquiring data:
Saving the data to file
Our last goal is to save the data to a file. Again, we will use an Express.VI to keep this simple. Right click to bring up the Functions Palette, select Express»Output and click on Write to Measurement File.VI and place it on the Block Diagram, in the loop, to the right of the Waveform Chart. Once placed, you will see a dialog box entitled “Configure Write to Measurement File.” So as to keep this as straight forward as we can, change the file path to something you’ll remember. Besides that, the only other change is Segment Headers, select ‘One header only’, and click on OK to close the dialog box and save the changes. Only one wire to add and we’re done. Wire the Signals input of the Write to Measurement File.VI to the data output of the Read.VI. Your now completed block diagram VI will look like this:
Let’s run our VI! Run it for a bit, then stop it. If all ran correctly you should have a data file waiting for you. Though the file is much longer that this, here is sample of contents of the file including the header:
In part 1, we learned how simple it can be to acquire one sample of data using a USB-1608G, ULx and NI LabVIEW by creating a simple VI. In part 2, we enhanced the VI we created in part 1 by adding the ability to acquire data at a steady rate, display the data in a stripchart, save the data to a log file, and notify the operator is there is an error.
In part 3, we will add scaling, alarming and a bit of digital control.
Getting started with simple Analog Input.zip