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:
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 MAXTo 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 LabVIEWNow 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 TasksYou 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
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 LabVIEWBefore 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:
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
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 TasksThe 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:
DAQmx Start Task: Running Your TaskBefore 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 GenerateThe 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 TaskAfter 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 TaskWhen 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 LineYou will create a VI that sets the state of a single digital line using DAQmx VIs.
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 WaveformYou 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.
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. |