NI-DAQmx Tasks


In Chapter 10, you learned how to create virtual channels in MAX. These virtual channels correlate with physical channels on your DAQ device that can be read from or written to in LabVIEW using the NI-DAQmx VIs. Now you'll learn about tasks, an even more powerful framework that makes building your DAQ application easier.

Tasks are the key to getting things done in NI-DAQmx. A task is a collection of one or more virtual channels with timing, triggering, and other properties. An NI-DAQmx Task is a neat way to "wrap up" all the parameters related to the data acquisition task.

Let's explore this a little more, because tasks are unlike other structures in LabVIEW and take some getting used to.

Conceptually, a task represents a measurement (input signal acquisition) or generation (output signal generation) you want to perform. All channels in a task must be of the same channel type, such as analog input or counter output. With some devices, you can include channels from multiple devices in a task. To perform a measurement or a generation with a task, you would follow these steps:

1.

Create or load a task. You can create tasks interactively with the DAQ Assistant or programmatically in your ADE, or Application Development Environment, such as LabVIEW or LabWindows/CVI.

2.

Configure the channel, timing, and triggering properties as necessary.

3.

Optionally, perform various task state transitions to prepare the task to perform the specified operation.

4.

Read or write samples.

5.

Clear the task.

If appropriate for your application, you would repeat steps 2 through 4. For instance, after reading or writing samples, you can reconfigure the virtual channel, timing, or triggering properties, and then read or write additional samples based on this new configuration.

If properties need to be set to values other than their defaults for your task to be successful, your program must set these properties every time it executes. For example, if you run a program that sets property A to a non-default value and follow that with a second program that does not set property A, the second program uses the default value of property A. The only way to avoid setting properties programmatically each time a program runs is to use virtual channels and/or tasks created in the DAQ Assistant.

You've just learned all the basics of how tasks work. Now you will see how to create tasks in MAX that can be used in LabVIEW.

Creating NI-DAQmx Tasks in MAX

To create an NI-DAQmx Task in MAX, right-click on the Data Neighborhood in the Configuration tree and select Create New . . . from the pop-up menu (see Figure 11.24).

Figure 11.24. Launching the Create New . . . dialog from the Data Neighborhood node in MAX


In the resulting dialog, select NI-DAQmx Task and press the Next button (see Figure 11.25).

Figure 11.25. Selecting NI-DAQmx Task from the Create New . . . dialog


Select a measurement type (see Figure 11.26).

Figure 11.26. The Create New . . . dialog showing the possible measurement types for your new task


Select one or more physical channels from the list of devices and physical channels that support the measurement type you have selected, and press the Next button (see Figure 11.27).

Figure 11.27. Selecting one or more physical channels for your measurement task


Edit the name of the task (or use the default namethis can be changed later) and press the Finish button (see Figure 11.28).

Figure 11.28. Naming your new NI-DAQmx task


Your new task will appear beneath the My System>>Data Neighborhood>> NI-DAQmx Tasks in MAX's Configuration tree (see Figure 11.29). Click on the task with your mouse to view the task's configuration dialog. You can now configure and test the specific functionality of your task. Congratulationsyou're well on your way to using NI-DAQmx tasks in LabVIEW!

Figure 11.29. Viewing your new task's configuration dialog from within MAX


Referencing MAX DAQmx Tasks in LabVIEW

Now that you know how to create and access NI-DAQmx Tasks in MAX, you will learn how to access them from within LabVIEW. Simply place a DAQmx Task Name constant (found on the Measurement I/O>>DAQmxData Acquisition palette) onto the block diagram, as shown in Figure 11.30.

Figure 11.30. Placing a DAQmx Task Name constant from the palette onto your block diagram


Using the Operating tool, you can select an NI-DAQmx task that you have created in MAX, by clicking on the DAQmx Task Name constant, as shown in Figure 11.31.

Figure 11.31. Selecting an NI-DAQmx task, created in MAX, from the DAQmx Task Name constant's drop-down list


If you would like to create a new NI-DAQmx task in MAX, select the Browse . . . option in the DAQmx Task Name drop-down list, and then choose Create New . . . >>Max Task (see Figure 11.32). This will open the NI-DAQmx task wizardwhich you learned about earlier in the section, "Creating NI-DAQmx Tasks in MAX"where you can create a new NI-DAQmx task, which may then be selected from the DAQmx Task Name constant.

Figure 11.32. Creating a new task from a DAQmx Task Name constant's Browse . . . dialog


Generating Code from MAX DAQmx Tasks

You are now probably wondering how much work you are going to have to do to actually take some data using the NI-DAQmx task that you have created in MAX. As this section's title suggests, LabVIEW will make this very easy for you. Figure 11.33 shows the Generate Code>> options that are available from the DAQmx Task Name constant. Table 11.1 contains descriptions of each of these code generation options. Figure 11.34 shows the Configuration and Example code generated from an analog output NI-DAQmx task.

Figure 11.33. The Generate Code>> pop-up submenu of a DAQmx Task Name constant showing the options for generating LabVIEW code from an NI-DAQmx task


Table 11.1. Code Generation Descriptions
Generate CodeDescription

Example

Generates all the code necessary to run the task or channel, such as VIs needed to read or write samples, and VIs to start and stop the task, loops, and graphs. Select this option if the task or channel is specific to your system, and you do not use the task or channel on other systems.

Configuration

Generates the code associated with the configuration. LabVIEW replaces the I/O constant or control with a subVI that contains VIs and Property Nodes used for channel creation and configuration, timing configuration, and triggering configuration used in the task or channel. Select this option if you need a portable configuration that you can move to another system.

Configuration and Example

Generates configuration code and example code for the task or channel in one step.

Convert to Express VI

Replaces the DAQmx Task Constant with a DAQ Assistant Express VI. Similarly, you can convert a DAQ Assistant Express VI into a DAQmx Task Constant.


Figure 11.34. Configuration and Example code generated from an analog output NI-DAQmx task


The wonderful thing about the NI-DAQmx code generation feature is that it teaches you how to use NI-DAQmx by providing you with custom example VIs built just for you! How is that for a personal touch?

Using NI-DAQmx Tasks in LabVIEW

Before we get into the details of how each individual DAQmx VI works, you will want to see a bird's eye view of the DAQmx VIs and methodology. Fortunately, using NI-DAQmx tasks in LabVIEW is very easyit consists of the following steps:

1.

Create a task (or reference a MAX DAQmx task).

2.

Start the task.

3.

Read or Write, and repeat as necessary.

4.

Stop the task.

5.

Clear the task.

For some measurements and generations, we will need to configure timing, triggering, and other task properties before starting the task. And also, we may need to wait for a task to complete before clearing it.


So, nearly all of your NI-DAQmx applications will be logically organized something like Figure 11.35.

Figure 11.35. Block diagram showing the logical organization of NI-DAQmx applications


But it is good to know that

  • You don't always have to Start your task. The Read or Write operations will usually Start the task automatically.

  • You don't have to Stop the task before you Clear it. The Clear operation will Stop the task if it is running.

So, your NI-DAQmx application might look more like Figure 11.36.

Figure 11.36. Block diagram showing the logical organization of NI-DAQmx applications that do not call the (sometimes optional) Start or Stop task operations


NI-DAQmx is starting to look very simple now! In fact, all you have to do, in order to customize the task to your specific type of measurement or generation, is select the appropriate polymorphic VI member for the Create step and the Read or Write step. (We discuss polymorphic VIs at the beginning of Chapter 14, "Advanced LabVIEW Data Concepts." You might want to jump ahead and read that short section and learn how to use the Polymorphic VI Selector. Then come back here and continue to learn about DAQmx.)

We will learn about each of these VIs in the next sections, but we want to show you something very important first.

Many of the DAQmx VIs are polymorphic, and allow you to select a specific instance that matches the task you wish to perform. You make this selection using the Polymorphic VI Selector, shown in Figures 11.37 through 11.40.

Figure 11.37. Create analog input task


Figure 11.38. Read analog input


Figure 11.39. Create digital output task


Figure 11.40. Write digital output


The selection made with the Polymorphic VI Selector for the Create step must match the selection for the Read or Write step. If you do not select compatible modes, then the read or write operation will return an error when you try to run your application.


For example, if you create an analog input task, then you can only call the analog read operation, as shown in Figures 11.37 and 11.38.

Similarly, if you create a digital output task, then you must configure the write function as a digital write, as shown in Figures 11.39 and 11.40.

Keep this important fact in mind as you learn the specifics of these DAQmx VIs, next.

DAQmx Create Virtual Channel: Creating Tasks

The first step in using NI-DAQmx in LabVIEW is creating a task. You have already learned how to create a task in MAX and use a DAQmx Task Name control or constant to reference it in LabVIEW. However, you can also create a new task programmatically, using the DAQmx Create Virtual Channel (Measurement I/O>>DAQmx Data Acquisition palette).

DAQmx Create Virtual Channel (Measurement I/O>>DAQmx Data Acquisition palette) creates a virtual channel or set of virtual channels and adds them to a task. The instances of this polymorphic VI correspond to the I/O type of the channel, such as analog input, digital output, or counter output; the measurement or generation to perform, such as temperature measurement, voltage generation, or event counting; and, in some cases, the sensor to use, such as a thermocouple or RTD for temperature measurements.

Figure 11.41. DAQmx Create Virtual Channel


If you use this VI within a loop without specifying a task in, NI-DAQmx creates a new task in each iteration of the loop. Use the DAQmx Clear Task VI within the loop after you are finished with the task to avoid allocating unnecessary memory. Refer to the Task Creation and Destruction sections of the NI-DAQmx online help for more information about when NI-DAQmx creates tasks and when LabVIEW automatically destroys tasks.

The DAQmx Channel properties include additional channel configuration options.

This might be one of the biggest conceptual hurdles that you will face when learning NI-DAQmx: the fact that you use DAQmx Create Virtual Channel to create a new task from one or more physical channels.

The reason for the confusion is that DAQmx Create Virtual Channel does many things behind the scenes. Here are some important things to know about this VI:

  1. If task in is left unwired, a new task will be created. So, in addition to creating a virtual channel, DAQmx Create Virtual Channel can create a new task. In most cases, this is the preferred method for programmatically creating a new NI-DAQmx task.

  2. In addition to creating a new virtual channel from physical channels, the new virtual channel is added to the task. So, we never really interact with the virtual channelwe do not access virtual channel references.

DAQmx Start Task: Running Your Task

Before you can read from or write to a task, you need to start itset it to a running state. In some cases, simply calling DAQmx Read or DAQmx Write (which you will learn about shortly) will start the task automatically. However, it is generally best to use DAQmx Start Task.

DAQmx Start Task (Measurement I/O>>DA Qmx Data Acquisition palette) sets the task to the running state to begin the measurement or generation. Using this VI is required for some applications and is optional for others.

Figure 11.42. DAQmx Start Task


If you do not use this VI, a measurement task starts automatically when the DAQmx Read VI runs. The autostart input of the DAQmx Write VI determines if a generation task starts automatically when the DAQmx Write VI runs.

If you do not use the DAQmx Start Task VI and the DAQmx Stop Task VI when you use the DAQmx Read VI or the DAQmx Write VI multiple times, such as in a loop, the task starts and stops repeatedly. Starting and stopping a task repeatedly reduces the performance of the application.

Read and Write: Measure and Generate

The DAQmx Read and DAQmx Write are polymorphic VIs (which are discussed in Chapter 14) and have over 40 member VIs, each. Use the Polymorphic VI Selector, as shown in Figure 11.43, to select the specific measurement or generation type; but remember that it is critical to choose the correct one that matches the type of measurement or generation task that you have configured.

Figure 11.43. The Polymorphic VI Selector menu options of DAQmx Read, used for configuring the VI's mode of operation


Also, it is important to note that the inputs and outputs of VI will change, depending on the measurement or generation type that you choose.

DAQmx Read (Measurement I/O>>DAQmx Data Acquisition palette) reads samples from the task or virtual channels you specify. The instances of this polymorphic VI specify what format of samples to return, whether to read a single sample or multiple samples at once, and whether to read from one or multiple channels.

Figure 11.44. DAQmx Read


The DAQmx Read properties include additional configuration options for read operations.

DAQmx Write (Measurement I/O >>DAQmx Data Acquisition palette) writes samples to the task or virtual channels you specify. The instances of this polymorphic VI specify the format of the samples to write, whether to write one or multiple samples, and whether to write to one or multiple channels.

Figure 11.45. DAQmx Write


If the task uses on-demand timing, the default if you do not use the DAQmx Timing VI, this VI returns only after the device generates all samples. If the task uses any timing type other than on-demand, this VI returns immediately and does not wait for the device to generate all samples. Your application must determine if the task is done to ensure that the device generated all samples.

The DAQmx Write properties include additional configuration options for write operations.

DAQmx Stop Task

After you are done using your measurement or generation task, you will want to stop it (using DAQmx Stop Task)possibly to start it again at a later time.

DAQmx Stop Task (Measurement I/O>>DAQmx Data Acquisition palette) stops the task and returns it to the state the task was in before the DAQmx Start Task VI ran or the DAQmx Write VI ran with the autostart input set to TRUE.

Figure 11.46. DAQmx Stop Task


If you do not use the DAQmx Start Task VI and the DAQmx Stop Task VI when you use the DAQmx Read VI or the DAQmx Write VI multiple times, such as in a loop, the task starts and stops repeatedly. Starting and stopping a task repeatedly reduces the performance of the application.

DAQmx Clear Task

When you are done using your task, use DAQmx Clear Task to release any resources that have been reserved by the task.

DAQmx Clear Task (Measurement I/O>>DAQmx Data Acquisition palette) clears the task. Before clearing, this VI stops the task, if necessary, and releases any resources the task reserved. You cannot use a task after you clear it unless you recreate the task.

Figure 11.47. DAQmx Clear Task


If you use the DAQmx Create Task VI or the DAQmx Create Virtual Channel VI within a loop, use this VI within the loop after you are finished with the task to avoid allocating unnecessary memory.

Activity 11-4: Writing to a Digital Line

You will create a VI that sets the state of a single digital line using DAQmx VIs.

1.

Build the front panel shown in Figure 11.48.

Figure 11.48. Front panel of the VI you will create during this activity


Make sure that the line state Boolean is configured, via its pop-up menu, for Mechanical Action>>Switch When Released. This will ensure that we can set the line state ON and OFF, rather than just ON (which would happen if it "bounced back" automatically after it was pressed).

DAQmx Physical Channel Control

The digital line control on the front panel is a DAQmx Physical Channel (found on the Modern>>I/O>>DAQmx Name Controls palette).

By default, the DAQmx Physical Channel control will only display analog input channels. Pop up on it and select I/O Name Filtering to open the Filter Names dialog (see Figure 11.49). Set the I/O Type to Digital Output and the Port/Line Filtering to Lines Only. This will cause the drop-down list of physical channels to be restricted to digital output lines.

Figure 11.49. Filter Names dialog for configuring DAQmx Physical Channel controls, indicators, and constants


2.

Build the block diagram shown in Figure 11.50. (Note that we write the line state value to the digital line, whenever the button value has changed OR when the loop count is zero.) All DAQmx VIs used in this activity may be found on the Measurement I/O>>DAQmx Data Acquisition palette.

Figure 11.50. Block diagram of the VI you will create during this activity


3.

Configure DAQmx Create Virtual to create a digital output task by setting the Polymorphic VI Selector to Digital Output.

4.

Configure DAQmx Write to write one value to a single digital line by setting its Polymorphic VI Selector to Digital>>Single Channel>>Single Sample>> Boolean (1 line).

DAQmx Write

5.

Save the VI as Write to Digital Line.vi in your MYWORK folder.

Configure the digital line on the front panel and run your VI. Toggle the state and read the voltage on the digital output line. Watch it change between 0 VDC and +5 VDC.

Activity 11-5: Writing a Continuous Analog Waveform

You will create a VI that writes a continuous analog waveform to an analog output. You will write one point at a time, iteratively, in a While Loop.

1.

Build the front panel shown in Figure 11.51.

Figure 11.51. Front panel of the VI you will create during this activity


DAQmx Physical Channel Control

The analog output channel control on the front panel is a DAQmx Physical Channel (found on the Modern>>I/O>>DAQmx Name Controls palette).

By default, the DAQmx Physical Channel control will only display analog input channels. Pop up on it and select I/O Name Filtering to open the Filter Names dialog. Set the I/O Type to Analog Output. This will cause the drop-down list of physical channels to be restricted to analog output lines (see Figure 11.52).

Figure 11.52. Filter Names dialog showing the settings you will need for your DAQmx Physical Channel control in this activity


2.

Build the block diagram shown in Figure 11.53. All DAQmx VIs used in this activity may be found on the Measurement I/O>>DAQmx Data Acquisition palette.

Figure 11.53. Block diagram of the VI you will create during this activity


3.

Configure DAQmx Create Virtual to create an analog voltage output task by setting the Polymorphic VI Selector to Analog Output>>Voltage. Set its minimum and maximum values to 0 and 5 Volts.

4.

Configure DAQmx Write to write one value to a single analog output line by setting its Polymorphic VI Selector to Analog>>Single Channel>>Single Sample>>DBL.

5.

Save the VI as Write Continuous Analog Waveform.vi in your MYWORK folder.

Configure the analog output channel on the front panel and run your VI. Measure the voltage on the analog output line. Watch the sinusoid signal cycle between 0 VDC and +5 VDC.




LabVIEW for Everyone. Graphical Programming Made Easy and Fun
LabVIEW for Everyone: Graphical Programming Made Easy and Fun (3rd Edition)
ISBN: 0131856723
EAN: 2147483647
Year: 2006
Pages: 294

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net