06-01-2022 03:34 AM
Hi!
I've set up a C++ project in Visual Studio (2019) that uses DAQmx and am trying to read data from a hardware device and display this on the screen in real time. To do this I want to start a task, collect data, and use DAQmxRegisterEveryNSamplesEvent to read from the buffer and display the mean value of the latest data. I have managed to start a task, collect data through the task and read a finite number of data points from the buffer.
When I change the code to collect continuously and use DAQmxRegisterEveryNSamplesEvent I get an exception:
Exception thrown at 0x716F39EC (nidmxfu.dll) in QtDAQmx_test3.exe: 0xC0000005: Access violation reading location 0x00000000.
I cannot debug this exception since I do not have (and cannot get) access to the pdb files for DAQmx (see discussion https://forums.ni.com/t5/Measurement-Studio-for-VC/symbol-server-for-debugging/m-p/1866225).
Everything seems to run smooth until the call to DAQmxRegisterEveryNSamplesEvent. The error message suggests that I pass a NULL pointer into the call for DAQmxRegisterEveryNSamplesEvent, but breaking does not show any arguments being NULL (unless they should be).
My code looks roughly as follows. I removed all error handling to increase the readability. Full cpp file attached as .c (not allowed to attach as cpp apparently):
int main(int argc, char *argv[])
{
TaskHandle handle = initDataCollection();
startTask(handle);
DAQmxRegisterEveryNSamplesEvent(handle, DAQmx_Val_Acquired_Into_Buffer, 100, 0, readData, NULL);
return 0;
}
readData looks like this, but the exception occurs before I enter the function.
int32 CVICALLBACK readData(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void* callbackData)
{
int32 read;
float64 collection_buffer[100];
std::vector<float64> data;
DAQmxErrChk(DAQmxReadAnalogF64(taskHandle, 100, 10.0, DAQmx_Val_GroupByChannel, collection_buffer, 100, &read, NULL));
data.assign(collection_buffer, collection_buffer + read);
float64 mean = std::accumulate(data.begin(), data.end(), 0.0) / data.size();
printf("Mean: %f\n", mean);
return 0;
}
initDataCollection and startTask, in case it should be relevant:
static TaskHandle initDataCollection(void)
{
TaskHandle taskHandle = 0;
DAQmxErrChk(DAQmxCreateTask("", &taskHandle));
DAQmxErrChk(DAQmxCreateAIVoltageChan(taskHandle, "Dev3/ai0", "data_collection_example", DAQmx_Val_Cfg_Default, -10.0, 10.0, DAQmx_Val_Volts, NULL));
DAQmxErrChk(DAQmxCfgSampClkTiming(taskHandle, "", 1000.0, DAQmx_Val_Rising, DAQmx_Val_ContSamps, 100));
return taskHandle;
}
static void startTask(TaskHandle taskHandle)
{
DAQmxErrChk(DAQmxStartTask(taskHandle));
return;
}
What could be the problem?
Solved! Go to Solution.
06-09-2022 04:33 AM
Found the issue. DAQmxRegisterEveryNSamplesEvent needs to be called before the task is started.
int main(int argc, char *argv[])
{
TaskHandle handle = initDataCollection();
DAQmxRegisterEveryNSamplesEvent(handle, DAQmx_Val_Acquired_Into_Buffer, 100, 0, readData, NULL);
startTask(handle);
return 0;
}