Hi! I'm trying to make a C++ class that handles a DAQmx data aquisition task. I am using DAQmxRegisterEveryNSamplesEvent to call a callback when I collected enough samples to process. I wanted to add the callback as a member function of my class, but doing so causes errors:
E0167 argument of type "int32 (__cdecl NiDAQmxHandler::*)(TaskHandle handle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData)" is incompatible with parameter of type "DAQmxEveryNSamplesEventCallbackPtr"
C3867 'NiDAQmxHandler::readData': non-standard syntax; use '&' to create a pointer to member
Removing the function from the class and simply keeping it in the cpp file works fine.
What is the syntax for adding a callback as a member function of a class?
Here's a slimmed down example:
main.cpp
#include "nidaqmxhandler.h"
int main(int argc, char *argv[])
{
NiDAQmxHandler taskhandler;
taskhandler.initDataCollection();
taskhandler.startTask();
return 0;
}
nidaqmxhandler.h
#pragma once
#include <NIDAQmx.h>
class NiDAQmxHandler
{
public:
NiDAQmxHandler();
~NiDAQmxHandler() = default;
void initDataCollection(void);
void startTask();
private:
int32 CVICALLBACK readData(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void* callbackData);
TaskHandle taskHandle;
};
nidaqmxhandler.cpp
#include "nidaqmxhandler.h"
#include <vector>
#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else
int32 CVICALLBACK NiDAQmxHandler::readData(TaskHandle handle, int32 everyNsamplesEventType, uInt32 nSamples, void* callbackData)
{
printf("Test successful");
return 0;
}
void NiDAQmxHandler::initDataCollection(void)
{
DAQmxCreateTask("", &taskHandle);
DAQmxCreateAIVoltageChan(taskHandle, "Dev3/ai0", "", DAQmx_Val_Cfg_Default, -10.0, 10.0, DAQmx_Val_Volts, NULL);
DAQmxCfgSampClkTiming(taskHandle, "", 1000.0, DAQmx_Val_Rising, DAQmx_Val_ContSamps, 100);
DAQmxRegisterEveryNSamplesEvent(taskHandle, DAQmx_Val_Acquired_Into_Buffer, 100, 0, readData, NULL);
return;
}
void NiDAQmxHandler::startTask()
{
DAQmxStartTask(taskHandle);
return;
}
Thanks!