In general, data acquisition programming with DAQmx involves the following steps:
- Create a Task and Virtual Channels
- Configure the Timing Parameters
- Start the Task
- Perform a Read operation from the DAQ
- Perform a Write operation to the DAQ
- Stop and Clear the Task.
Data acquisition in text based-programming environment is very similar to the LabVIEW NI-DAQmx programming as the functions calls are the same as the NI-DAQmx VI's.
Create a Task and Virtual Channels
A virtual channel is a collection of settings such as a name, a physical channel, input terminal connections, the type of measurement or generation, and can include scaling information.
A task, an important concept for NI-DAQmx, 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. Tasks can be created for analog in and output, digital input and output and counter operations.
To create a task and an analog input channel in C, use the following function calls:
DAQmxCreateAIVoltageChan (taskHandle, "Dev1/ai0", "Voltage", DAQmx_Val_Cfg_Default, -10.0, 10.0, DAQmx_Val_Volts, NULL);
To create a task and an analog input channel in .NET, instantiate the Task object and create a Channel as shown below:
analogInTask = new Task();
myChannel = analogInTask.AIChannels.CreateVoltageChannel(
"dev1/ai0", //The physical name of the channel
"myChannel", //The name to associate with this channel
AITerminalConfiguration.Differential, //Differential wiring
-10, //10v minimum
10, //10v maximum
AIVoltageUnits.Volts //Use volts
Configure the Timing Parameters
Most NI data acquisition devices use a sample clock to control the rate at which samples are acquired and generated. This sample clock sets the time interval between samples. Each tick of this clock initiates the acquisition or generation of one sample per channel.
In software, you can specify the interval (how fast the clock acquires or generates signals) by specifying the sample rate. You can also limit the sample rate by the signal conditioning you apply to the signals or the number of channels in your application.
To configure the timing parameters in C, call the DAQmxCfgSamp function as shown below:
DAQmxCfgSampClkTiming(taskHandle, "", 10000.0, DAQmx_Val_Rising,
To configure the timing parameters in .NET, use the Task.Timing property as shown below:
"/Dev1/PFI0", // external clock source line or use "" for internal clock
10000, // expected rate of external clock or actual rate of internal clock
SampleClockActiveEdge.Rising, // acquire on rising or falling edge of ticks
SampleQuantityMode.ContinuousSamples, // continuous or finite samples
1000 // number of finite samples to acquire or used for buffer size if continuous
When a device controlled by NI-DAQmx does something, it performs an action. Two very common actions are producing a sample and starting a waveform acquisition. Every NI-DAQmx action needs a stimulus or cause. When the stimulus occurs, the action is performed. Causes for actions are called triggers. Triggers are named after the actions they cause, such as a start trigger to start an acquisition.
The NI-DAQmx Trigger function configures a trigger to perform a specific action. The most commonly used actions are a start trigger and a reference trigger. A start trigger initiates an acquisition or generation. A reference trigger establishes the location, in a set of acquired samples, where pretrigger data ends and posttrigger data begins. Both of these triggers can be configured to occur on a digital edge, an analog edge, or when an analog signal enters or leaves a window.
To configure a start trigger on a rising digital signal coming in on PFI line 0 of the device, use the DAQmxCfgDigEdgeStartTrig function in C:
DAQmxCfgDigEdgeStartTrig (taskHandle, "PFI0", DAQmx_Val_Rising);
To configure a start trigger in .NET, use ConfigureDigitalEdgeTrigger in the Task.Triggers.StartTrigger collection as shown below:
DigitalEdgeStartTriggerEdge triggerEdge = DigitalEdgeStartTriggerEdge.Rising;
Start the Task
The NI-DAQmx Start Task function explicitly transitions a task to the running state. In the running state, the task performs the specified acquisition or generation. A task will implicitly transition to the running state, or automatically start, if the NI-DAQmx Start Task function is not used when the NI-DAQmx Read function executes. This implicit transition also occurs if the NI-DAQmx Start Task function is not used and the NI-DAQmx Write function executes with its auto start input specified accordingly.
To start a task in C, use the DAQmxStartTask function:
To start a task in .NET, use the Task.Start function:
Read or Write Operation
NI-DAQmx provides multiple functions for reading and writing data. In many cases, you can use multiple options. The read and write functions have two major selection criteria: data format and data organization. Data format specifies the type of the data that is returned. Counter reads, for example, can return integers or floating-point formats. The second category, data organization, deals with the structure the data is returned in. Analog reads, for example, have a variety of array and scalar organizations.
Depending on the data operation, data may be acquired or generated. In acquisition mode, a read operation is required to read a specified number of samples from the buffer. In data generation mode, sample are been written out to the data acquisition board buffer.
To read data in C, use the DAQmxReadAnalog function call:
DAQmxReadAnalogF64(taskHandle, 1000, 10.0, DAQmx_Val_GroupByChannel, data, 1000, &read, NULL)'
To read data in .NET, create a ChannelReader object, bind it to Task.Stream and then call a read function as shown below:
//Create the reader and attach it to the stream
AnalogSingleChannelReader reader = new AnalogSingleChannelReader(analogInTask.Stream);
//Perform the read
double data = reader.ReadMultiSample(100);
For more information on reading and writing data in .NET, see the "Reading and Writing with the NI-DAQmx .NET Library" section of the NI-DAQmx .NET 2.0 Framework Help.
Stop and Clear task
After completing a task, stop the task and de-allocate all reserved resources. The NI-DAQmx Clear Task function clears the specified task. If the task is currently running, the function first stops the task and then releases all of its resources. Once a task has been cleared, it cannot be used unless it is recreated. Thus, if a task will be used again, the NI-DAQmx Stop Task function should be used to stop the task, but not clear it.
To stop and clear a task in C use the following functions:
To stop and clear a task in .NET use the Task.Stop and Task.Dispose methods as shown below: