Transition from Traditional NI-DAQ(Legacy) to NI-DAQmx using ANSI C and NI LabWindows™/CVI: Part Two

Overview

Note: This page is about NI-DAQ also known as Traditional NI-DAQ (Legacy). NI-DAQmx replaced Traditional NI-DAQ (Legacy) in 2003. NI strongly recommends using NI-DAQmx for new designs and migrating existing Traditional NI-DAQ (Legacy) applications to NI-DAQmx. Please review the Getting Started with NI-DAQmx guide for more information on migration.
This is the second document in the four-part series, “Transitioning from Traditional NI-DAQ (Legacy) to NI-DAQmx Using ANSI C and NI LabWindows™/CVI™.” Refer to the “Links to Other Topics in this Series” section for the full series. Configuring Your Measurement

The organization of the driver application programming interface (API) determines how you configure the measurement operation. Traditional NI-DAQ (Legacy) function calls are based on the type of operation that needs to occur. For example, if you make a temperature measurement (which is a relatively slow operation), you need to use one of the many analog input function calls. Depending on the function call used, you apply scaling to the data returned by the function; some calls require scaling to voltage, some do not. If you require any custom scaling, you must create your own scaling operation.

NI-DAQmx is organized based on the type of measurement you need instead of the type of operation. It offers support to create channels specifically for measuring temperature, strain, position, and so on. All parameters required for a measurement are grouped together, making the NI-DAQmx API intuitive and easy to learn.

The following sections explain how to configure the various types of measurements using the two drivers.

Contents

Accessing Help in NI-DAQmx

This series references two manuals that are shipped with NI-DAQmx.

  • NI-DAQmx Core Help – references the language-agnostic manual for NI-DAQmx. The NI-DAQmx Core Help explains NI-DAQmx concepts and provides background about the various features and capabilities of the driver. Refer to Start » Programs » National Instruments » NI-DAQ » NI-DAQmx Help to access this manual.
  • NI-DAQmx C Reference Help – references the manual for the DAQmx C API. This reference is specific to the C API and provides documentation for NI-DAQmx function calls. There are also concept topics specific to using the DAQmx C API. Part of the NI-DAQmx driver software, this manual is located at Start » All Programs » National Instruments » NI-DAQ » NI-DAQmx C Reference Help.

 

Configuration

Your goal is to create an example data acquisition application that performs a finite analog input operation using the internal sample clock of the data acquisition device.

Configuring Your Measurement in Traditional NI-DAQ (Legacy)

You can configure the Traditional NI-DAQ (Legacy) measurement with the following code.

 

 i16 iDevice = 1; // specify device

i16 iChan = 1; // specify channel

i16 iGain = 1; // specify channel

f64 dSampRate = 1000.0; // specify sample rate

u32 ulCount = 100; // specify number of samples

static i16 piBuffer[100] = {0}; // initialize data buffer

AI_Configure (iDevice, iChan, 0, 10, 0, 0); // Configure Device Channel and Gain

DAQ_Rate (dSampleRate, 0, , ); // Set Acquisition Rate

This configuration code sets up the finite analog input operation, meeting the requirements of the example application you are trying to create. However, this configuration does not take into account the type of measurement you are making. For example, to have meaningful data when you are reading thermocouple voltages, you need to perform scaling on the data returned. You do this by creating a scaling algorithm yourself.

Configuring Your Measurement in NI-DAQmx

With NI-DAQmx, you configure every operation by creating a task and then creating the appropriate channel. The following code shows how you configure the same operation using the NI-DAQmx C API.

 

 static TaskHandle gTaskHandle= 0;

DAQmxCreateTask("",&gTaskHandle); // Create a new Task

// Create an Analog Input Channel for Device 1, channel 0 in Differential mode

// The measurement range is from -10.0 to +10.0 Volts
DAQmxCreateAIVoltageChan(gTaskHandle, ("Dev1/ai0","",DAQmx_Val_Diff,10,10,DAQmx_Val_Volts,NULL);

/* Configure the sample clock to be the internal clock, sampling at 1000 Hz on the rising edge of the sample clock. The acquisition mode is finite and we will return 1000 samples per channel. */

DAQmxCfgSampClkTiming(gTaskHandle,"",1000,DAQmx_Val_Rising, DAQmx_Val_FiniteSamps,1000);

The functions are organized by group-related parameters to make using the API more intuitive. The API targets the particular type of measurement.

Configuring and Scaling Your Measurement in Traditional NI-DAQ (Legacy)

In the previous Traditional NI-DAQ (Legacy) example, you configure but do not scale the data. The following code shows how to configure and scale your measurement with the Traditional NI-DAQ (Legacy) API.

 

 i16 iDevice = 1; // specify device

i16 iChan = 1; // specify channel

i16 iGain = 1; // specify channel

f64 dSampRate = 1000.0; // specify sample rate

u32 ulCount = 100; // specify number of samples

static i16 piBuffer[100] = {0}; // initialize data buffer

AI_Configure (iDevice, iChan, 0, 10, 0, 0); // Configure Device Channel and Gain

DAQ_Rate (dSampleRate, 0, , ); // Set Acquisition Rate 

DAQ_VScale(iDevice, iChan, iGain, dGainAdjust, dOffset, ulCount, piBuffer, pdVoltBuffer);


You then need to apply a scaling function to scale a voltage to a thermocouple measurement.

In the previous code, you acquire the voltage data from the thermocouple and use your own conversion functions to get the temperature data. The acquisition operation is not configured in terms of the measurement being made.

Configuring and Scaling Your Measurement in NI-DAQmx

In the previous NI-DAQmx example, you configure the channel by using CreateVoltageChannel. If you need to configure an analog input operation connected to a thermocouple, you configure the channel with the following code.

 

 static TaskHandle gTaskHandle= 0;

DAQmxCreateTask("",&gTaskHandle); // Create a new Task

/* Create an Thermocouple Channel for Device 1, channel 0 for a B Type Thermocoupleto read in a measurement in Celsius between 0 and 100 C */

DAQmxCreateAIThrmcplChan (gTaskHandle, "Dev1/ai0", "", 0.0, 100.0, DAQmx_Val_DegC, DAQmx_Val_B_Type_TC, DAQmx_Val_ConstVal, 25.0, "");


When you use the previous configuration, the data returned consists of temperature measurements because NI-DAQmx internally scales the data.

Custom Scaling

NI-DAQmx fully supports using custom scales as well. For more information about custom scaling, refer to the NI-DAQmx Core Help under NI-DAQmx Help >> Key NI-DAQmx Concepts >> Custom Scales.  

Interactive Configuration

In most cases, you do not need to change the configuration for your data acquisition operation at run time. The following sections explain the options available for configuring your data acquisition operation interactively.

NI-DAQmx
NI-DAQmx provides the interactive DAQ Assistant configuration tool that you can use to experiment with the operation as you configure. Use the DAQ Assistant, which is accessible from the NI LabWindows/CVI, to interactively create, edit, and run NI-DAQmx tasks. Tasks that you create and configure in Measurement & Automation Explorer (MAX) configuration software are accessible from any NI-DAQmx application on your system.

Using DAQ Assistant in MAX

Using the DAQ Assistant in MAX

Figure 1. DAQ Assistant in MAX



You can test your configurations using DAQ Assistant without having to write a single line of code. Also, you can view the connection diagram for your particular tasks based on the accessory that is configured for your data acquisition device.

Preconfiguration

NI-DAQmx
After you configure your task in MAX using DAQ Assistant, you can load it using the DAQmxLoadTask ("", ); function. Once you have loaded the task, you can use it or make changes to the task configuration before using it.

Timing

This particular example application does not have any special timing and triggering requirements, but understanding timing and triggering support is important for creating useful data acquisition applications.

Timing in Traditional NI-DAQ (Legacy)

Setting up the timing parameters for Traditional NI-DAQ (Legacy) is done programmatically. In the following code, the sampling rate is set to 1,000 samples per second using the internal clock as the sampling clock.

 

 // sets the sample rate on device 1 to 1000 samples/second
DAQ_Set_Clock (1, 0, 1000.0, 0, );

 

Timing in NI-DAQmx

NI-DAQmx timing functions configure the timing for hardware-timed data acquisition operations. You use the timing functions to specify whether the operation is continuous or finite, select the number of samples to acquire or generate for finite operations, and create a buffer, when needed.

You can use DAQ Assistant to interactively configure these parameters and test the task.





Figure 2. Configuring Timing with DAQ Assistant and LabWindows/CVI




For information about the various types of timing supported by NI-DAQmx, refer to the "Timing and Triggering" section in the NI-DAQmx Core Help.

 

Triggering

Triggering in Traditional NI-DAQ (Legacy)

You can set an analog trigger type programmatically with the following code.

 

 Configure_HW_Analog_Trigger(iDevice, ND_ON, lLowValue, lHighValue, ND_ABOVE_HIGH_LEVEL, ND_THE_AI_CHANNEL);

/* Setup for external start trigger into PFI0. You can change the "source" to an analog input channel, if desired. */

Select_Signal(iDevice, ND_IN_START_TRIGGER, ND_PFI_0, ND_LOW_TO_HIGH);

 

Triggering in NI-DAQmx

The NI-DAQmx trigger class configures a trigger to perform a specific action. The most commonly used actions are a start trigger and a reference trigger. NI-DAQmx also supports the following trigger configurations:

  • AdvanceTrigger
  • ArmStartTrigger
  • HandshakeTrigger
  • PauseTrigger
  • ReferenceTrigger
  • StartTrigger
  • WatchdogExpirationTrigger






Figure 3. Configuring Triggers in DAQ Assistant



The following code shows how to configure the analog start trigger with APFI0 as the source at a 3.0 V level with 0 V hysteresis in your C code with NI-DAQmx.

 

  //Configure APFI0 to be the analog trigger, setup the trigger to be
//asserted on the Falling slope at 3.0 volts

DAQmxCfgAnlgEdgeStartTrig(gtaskHandle,"APFI0", DAQmx_Val_FallingSlope,3.0);

The triggering options are fully supported in DAQ Assistant, so you can load an existing task with these options configured or set the trigger options programmatically.

For information about the various types of timing supported by NI-DAQmx, refer to the Timing and Triggering section in the NI-DAQmx Core Help.

 

Links to Other Topics in this Series

Was this information helpful?

Yes

No