Tasks in NI-DAQmx

A task is a collection of one or more virtual channels with timing, triggering, and other properties. Conceptually, a task represents a measurement or 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, follow these steps:

  1. Create or load a task. You can create tasks interactively with the DAQ Assistant or programmatically in your ADE 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, 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 nondefault 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.

The following example illustrates how to perform a measurement with a task:

Problem

Create an NI-DAQmx task to measure temperature in the range 50°C to 200°C using a J-type thermocouple that is wired to channel 0 on an M Series device configured as Device 1. Sample the temperature 10 times per second, and acquire 10,000 samples. Use LabVIEW or LabWindows/CVI to write your application.

Solution

  1. Call the Create Task function/VI and name your task myTemperatureTask.
  2. Call the Create AI Thermocouple Channel function/VI.
  3. Specify Dev1/ai0 as the physical channel for the device connected to the thermocouple signal.
  4. Specify myThermocoupleChannel as the name to assign to your virtual channel.
  5. Select the appropriate values for the thermocouple type and range inputs. NI-DAQmx applies these attributes to the virtual channel.
  6. Call the Timing Sample Clock function/VI, specifying a rate of 10 Hz and a sample mode of finite.
  7. Call the Start function/VI.
  8. Call the Read function/VI, specifying number of samples as 10,000.
  9. Call the Stop function/VI after the desired number of samples have been acquired.
  10. Call the Clear function/VI.

You have now created a task called myTemperatureTask that uses a local virtual channel called myThermocoupleChannel.

Note  You also can use the DAQ Assistant to create the same task and generate the code to run the task.