07-14-2017 05:43 PM - edited 07-14-2017 05:46 PM
The following standalone working C++ code is a result of troubleshooting issues related to combinations of sampling rate (ADC) and number of samples using a NI PCIe 6363 Device via the DAQmx API for High-Speed Data Acquisition. The details of the original problem may be found in:
"What are valid combinations of sampling rate and number of samples using a NI PCIe 6363 Device via the DAQmx API"
and
"Need help troubleshooting a specific sampling rate (180K) and number of samples (60K) using a NI PCIe 6363 Device via the DAQmx API"
This code allows many combinations of sampling rate and the number of samples in case this configuration is needed.
#include "stdafx.h"
#include "NIDAQmx.h"
#include <stdio.h>
#include <vector>
constexpr int NI_SAMPLES = 60000;
constexpr double NI_ADC_RATE = (double)(3 * NI_SAMPLES);
#define _USE_INT_FOR_DATA_BUFFER_ 314 // <- comment out this line to use a double for each Analog element in the array/sample buffer
#ifdef _USE_INT_FOR_DATA_BUFFER_
static int16 NI_data[NI_SAMPLES];
#else
static float64 NI_data[NI_SAMPLES];
#endif
#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else
/*********************************************/
// Error handler
/*********************************************/
static void ErrorHandler(TaskHandle& taskHandle, int32 error)
{
char errBuff[2048] = { '\0' };
if (DAQmxFailed(error))
{
DAQmxGetExtendedErrorInfo(errBuff, 2048);
if (taskHandle != 0)
{
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
taskHandle = 0;
}
printf("DAQmx Error: %s\n", errBuff);
char ch = getchar();
}
}
/*********************************************/
// Callback Code
/*********************************************/
static int32 CVICALLBACK EveryNSamplesCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData)
{
int32 error = 0;
int32 read = 0;
#ifdef _USE_INT_FOR_DATA_BUFFER_
DAQmxErrChk(DAQmxReadBinaryI16(taskHandle, NI_SAMPLES, 10.0, DAQmx_Val_GroupByScanNumber, NI_data, NI_SAMPLES, &read, NULL));
// DAQmxErrChk(DAQmxReadBinaryI16(taskHandle, -1, 10.0, DAQmx_Val_GroupByScanNumber, NI_data, (2* NI_SAMPLES), &read, NULL));
#else
DAQmxErrChk(DAQmxReadAnalogF64(taskHandle, NI_SAMPLES, 10.0, DAQmx_Val_GroupByScanNumber, NI_data, NI_SAMPLES, &read, NULL));
#endif
if (read > 0)
{
printf("Acquired %d samples\n", read);
}
Error:
ErrorHandler(taskHandle, error);
return 0;
}
/*********************************************/
// Console Code
/*********************************************/
int main()
{
TaskHandle taskHandle = 0;
int32 error = 0;
DAQmxErrChk(DAQmxCreateTask("", &taskHandle));
DAQmxErrChk(DAQmxCreateAIVoltageChan(taskHandle, "Dev2/ai4", "", DAQmx_Val_Diff, -10.0, 10.0, DAQmx_Val_Volts, NULL));
DAQmxErrChk(DAQmxCfgSampClkTiming(taskHandle, "", NI_ADC_RATE, DAQmx_Val_Rising, DAQmx_Val_ContSamps, NI_SAMPLES));
DAQmxErrChk(DAQmxSetBufInputBufSize(taskHandle, NI_ADC_RATE));
DAQmxRegisterEveryNSamplesEvent(
taskHandle,
DAQmx_Val_Acquired_Into_Buffer,
NI_SAMPLES,
0,
EveryNSamplesCallback,
NULL);
DAQmxErrChk(DAQmxStartTask(taskHandle));
char ch = getchar();
DAQmxErrChk(DAQmxStopTask(taskHandle));
Error:
ErrorHandler(taskHandle, error);
}