Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Using EveryNCallback function with a C++ class (not VB)

Hello all,

 

I am trying to incorporate one of the ANSI C examples into my code which contains C++ classes. I receive errors because of having to declare the EveryNCallback and DoneCallback functions as static to keep their C function signature. This of course is not allowable when using these functions as members of a class. Could anyone help please?

 

Best,

Paolo

0 Kudos
Message 1 of 8
(5,011 Views)

Hi Paolo,

                 Could you please give me the error code numbers you are receiving so I can look into the issue further?

                 I look forward to hearing from you.

Kind regards 

Seb

Message 2 of 8
(4,999 Views)

"invalid use of member 'temperature::window' in static member function"

 

when I use window->"some function" this in the call back function. There is no error number.

 

 

0 Kudos
Message 3 of 8
(4,975 Views)

Hi Paolo,

 

http://isocpp.org/wiki/faq/pointers-to-members#memfnptr-vs-fnptr demonstrates how to use a C callback function to invoke a method on a C++ object.

 

Note that the DAQmx event functions have a "void *callbackData" parameter that you can use to pass the C++ object's "this" pointer to the static callback function. You can use static_cast to downcast the void* to your C++ class type before calling the method.

 

Brad

---
Brad Keryan
NI R&D
Message 4 of 8
(4,949 Views)

Firstly, thank you for the help. I have attempted to code the advice you gave but it still does not seem to compile properly. Can you suggest what is wrong with my code please?

 

Here is the code I am trying to configure:

 

forceDriver.h


 

static int32 CVICALLBACK static_callback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void* callbackData);

 

int32 EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples);
int32 DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData);

 



 

 

forceDriver.cpp


 

DAQmxErrChk (DAQmxRegisterEveryNSamplesEvent(taskHandle,DAQmx_Val_Acquired_Into_Buffer,2500,0,&forceDriver::static_callback,panel));

 

**START TASK**

 

int32 forceDriver::EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples)

{

       panel->(member_function);

}

 

int32 CVICALLBACK forceDriver::static_callback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void* callbackData)
{
forceDriver* static_object = static_cast<forceDriver*>(callbackData);
return static_object->EveryNCallback(taskHandle, everyNsamplesEventType, nSamples);
}



 

where panel is the object I have created. 

 

0 Kudos
Message 5 of 8
(4,932 Views)

Hi Paolo,

 

> I have attempted to code the advice you gave but it still does not seem to compile properly.

 

Without the text of the compiler errors, I can only speculate as to what's wrong.

 

> Can you suggest what is wrong with my code please?

 

I think you're on the right track with forceDriver::static_callback(). It receives a void*, downcasts it into an instance of forceDriver*, and calls a method on it. I'm assuming that static_callback was declared inside the class body of forceDriver and the "panel" variable that you passed to DAQmxRegisterEveryNSamplesEvent() is a pointer to a forceDriver.

 

However, I don't understand what the body of forceDriver::EveryNCallback() is supposed to do. You're passing the DAQmx API a pointer to a C function (really a static member function) that invokes your member function, so you shouldn't have to dereference any pointers to member functions. I don't know whether "panel" is in scope but you should be able to use "this" instead, because forceDriver::EveryNCallback() is not static.

 

Brad

---
Brad Keryan
NI R&D
0 Kudos
Message 6 of 8
(4,927 Views)

Hi Brad,

 

Firstly, thanks for the help and explaining how it works. Is it possible to use the callbackData parameter to pass an object into the callback? panel for me is a pointer to an object from a class out of scope. Will this be a problem?

 

Best,

Paolo

0 Kudos
Message 7 of 8
(4,873 Views)

> Is it possible to use the callbackData parameter to pass an object into the callback?

 

Yes, that's what it's there for.

 

> panel for me is a pointer to an object from a class out of scope. Will this be a problem?

 

As long as it's in scope when you pass it to the function, it should be fine. My comments about scope were mainly about the variables that weren't declared in the snippet of code that you posted.

---
Brad Keryan
NI R&D
0 Kudos
Message 8 of 8
(4,870 Views)