ni.com is currently undergoing scheduled maintenance.

Some services may be unavailable at this time. Please contact us for help or try again later.

Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Programming callbacks in C++ classes

I know this question may be slightly off-topic for this board, but I'll ask anyway.

I'm a noob to C++ programming, and I've been trying for a couple of days to implement a NIDAQmx callback in a C++ class.

Is there a way to create multiple instances of a callback method? As far as I can tell, DAQmxRegisterEveryNSamplesEvent will only allow you to pass a static pointer as DAQmxEveryNSamplesEventCallbackPtr.

when using:
class niscope
{
    public:
       niscope(void);
    private:
        DAQmxEveryNSamplesEventCallbackPtr callback;
        int32 CVICALLBACK EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData);
};

niscope::niscope
{
    callback = this->EveryNCallback;
}

I get compile errors like this:
error: argument of type ‘int32 (niscope::)(TaskHandle, int32, uInt32, void*)’ does not match ‘int32 (*)(TaskHandle, int32, uInt32, void*)’

but if I declare EveryNCallback as static it compiles fine, but a static member cannot access other class instances, so it's a catch-22.

Can anyone help?

Thanks,
John
0 Kudos
Message 1 of 4
(4,182 Views)
Ignore my last, I now realize that this was a stupid question. There is no need to implement the DAQ routines and callbacks as class members.

0 Kudos
Message 2 of 4
(4,168 Views)
I am trying to create a class using the DAQmxRegisterEveryNSamplesEvent function that will call another member of the function.  I am also receiving the same error and although I would like to change things this must be wrapped in a class.  Is there anyway to make this work?Smiley Indifferent
0 Kudos
Message 3 of 4
(3,975 Views)
Hi Born2Burn,

You need to create a free function that calls your member function. You can use the callbackData argument to hold a pointer to your object. Note that I haven't compiled or tested this, so it may have some bugs, but hopefully it gives you the right idea:

class A
{
   ...
   int32 everyN(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples);
   ...
};

int32 CVICALLBACK A_everyN_function(
TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void* callbackData)
{
   A* a = reinterpret_cast<A*>(callbackData);
   return a->everyN(taskHandle, everyNsamplesEventType, nSamples);
}

int32 registerEveryN(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, uInt32 options, A* a)
{
   return DAQmxRegisterEveryNSamplesEvent(taskHandle, everyNsamplesEventType, nSamples, options, &A_everyN_function, reinterpret_cast<void*>(a));
}

Brad
---
Brad Keryan
NI R&D
Message 4 of 4
(3,957 Views)