This series references two manuals that are shipped with NI-DAQmx.
After configuring the task, you need to start the task to actually begin the operation. You start the configured task by using the DAQ_Start() function.
DAQ_Start(iDevice, iChan, iGain, piBuffer, ulCount, iSampTB, uSampInt); |
With NI-DAQmx, you do not need to start the task explicitly. Calling the Read and Write methods automatically starts the task. You can also call start the task explicitly:
DAQmxStartTask(taskHandle); |
For information about the various types of timing supported by NI-DAQmx, refer to the “Task State Model” section in the NI-DAQmx Core Help.
Refer to the NI-DAQmx C Help “Task State Model” topic for more information about the task state model in the DAQmx ANSI C API.
Traditional NI-DAQ (Legacy) has many functions to perform reads:
A simple low-level read:
AI_VRead_Scan (1, piBuffer); |
Or,
DAQ_Check(iDevice, &iDAQstopped, &ulRetrieved); |
Or, an all inclusive function that sets up the acquisition and performs the read in one call:
DAQ_Op(iDevice, iChan, iGain, piBuffer, ulCount, dSampRate); |
NI-DAQmx uses one read function, regardless of the measurement type:
// Read 1000 points into array ‘data’ of length 1000 DAQmxReadAnalogF64 (taskHandle, 1000, 10.0, DAQmx_Val_GroupByChannel, data, 1000, &numRead, NULL)); |
DAQmx ANSI C API calls, such as the AO_VWrite() and WFM_OP() (for analog output), DIG_Out_Line and DIG_Out_Prt (for digital output) generate output signals using the Traditional NI-DAQ (Legacy) driver. You use the Write function calls to write the data to the internal driver buffer before the data can be generated at the output channels.
// Scale data from voltage to binary data array // Generate binary data WFM_Op(iDevice, iNumChans, piChanVect, piBuffer, ulCount, ulIterations, dUpdateRate); |
You can use the same calls for generating data on multiple channels by passing a multidimensional array and specifying more channels.
To write out data using the NI-DAQmx ANSI C API, use the DAQmx calls.
The following example shows how to write an array to memory and start generation.
// Create and configure the task, channel, and sample rate DAQmxCreateTask("",&gTaskHandle); DAQmxCreateAOVoltageChan(gTaskHandle,chan,"",min,max,DAQmx_Val_Volts,NULL); (DAQmxCfgSampClkTiming(gTaskHandle,"",rate,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000); // Write ‘data’ to buffer DAQmxWriteAnalogF64(gTaskHandle,sampsPerCycle,0,10.0,DAQmx_Val_GroupByChannel,data,&written,NULL); // Start generation DAQmxStartTask(gTaskHandle); // Wait 10 seconds until generation is complete DAQmxWaitUntilTaskDone (gTaskHandle, 10.0); // Stop and Clear Task DAQmxStopTask(gTaskHandle); DAQmxClearTask(gTaskHandle); |
Traditional NI-DAQ (Legacy) event message functions are an efficient way to monitor your background data acquisition processes without dedicating your foreground process for status checking. The Event Message dispatcher notifies your application when a user-specified data acquisition event occurs. Using event messaging eliminates continuous polling of data acquisition processes.
NI-DAQmx events serve a different purpose. The next section explains the events NI-DAQmx supports.
NI-DAQmx software events provide an asynchronous notification mechanism for a set of data acquisition events. Unlike hardware events, software events do not require you to use a thread to wait until data is available. Using event-based programming, you write an application that continues to perform work while waiting for data without resorting to developing a multithreaded application.
NI-DAQmx includes the following software events:
For more information about events NI-DAQmx supports, refer to the NI-DAQmx Core Help topic by selecting Contents >> Key NI-DAQmx Concepts >> Timing and Triggering.
You use the DAQ_Clear() function to release any resources that were reserved during the configuration. In most cases, you do not need to call DAQ_Clear() explicitly.
You use the Stop and Clear Task functions to deterministically release any NI-DAQmx resources that are reserved when the task is configured.
// Stop and Clear Task DAQmxStopTask(gTaskHandle); DAQmxClearTask(gTaskHandle); |
Each Traditional NI-DAQ (Legacy) function returns a status code that indicates whether the function performed successfully. When a Traditional NI-DAQ (Legacy) function returns a code that is a negative number, it means that the function did not execute. When a positive status code is returned, it means that the function did execute but with a potentially serious side effect.
For example:
// Acquire binary data iStatus = DAQ_Op(iDevice, iChan, iGain, piBuffer, ulCount, dSampRate); // Check for error iRetVal = NIDAQErrorHandler(iStatus, "DAQ_Op", iIgnoreWarning); // Scale to voltage iStatus = DAQ_VScale(iDevice, iChan, iGain, dGainAdjust, dOffset, ulCount, piBuffer, pdVoltBuffer);< /SPAN > // Check for error iRetVal = NIDAQErrorHandler(iStatus, "DAQ_VScale", iIgnoreWarning); |
Each NI-DAQmx C function returns a status code. A common method of dealing with NI-DAQmx errors is to define a function to check the status of each call. This is an example of a finite voltage acquisition with error handling:
// Define error checking function #define DAQmxErrChk(functionCall) if( DAQmxFailed(error= (functionCall)) ) goto Error; else TaskHandle taskHandle= 0; DAQmxErrChk (DAQmxCreateTask("",&taskHandle)); // Create Task // Create voltage input channel DAQmxErrChk(DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai0","",DAQmx_Val_Cfg_Default,- 10,10,DAQmx_Val_Volts,NULL)); // Configure sample clock timing DAQmxErrChk(DAQmxCfgSampClkTiming (taskHandle,"",1000,DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,1000)); DAQmxErrChk (DAQmxStartTask(taskHandle)); // Start Task // Read data DAQmxErrChk (DAQmxReadAnalogF64 (taskHandle,1000,10.0,DAQmx_Val_GroupByChannel,data,1000,&numRead,NULL)); Error: if( DAQmxFailed(error) ) DAQmxGetExtendedErrorInfo(errBuff,2048); if( taskHandle!= 0 ) { // DAQmx Stop Code DAQmxStopTask(taskHandle); DAQmxClearTask(taskHandle); } if( data ) free(data); if( DAQmxFailed(error) ) MessagePopup("DAQmx Error",errBuff); |
The mark LabWindows is used under a license from Microsoft Corporation.