02-05-2015 10:29 AM
I am following this example code, ContGen-IntClk.c.
#include <NIDAQmx.h>
#include <stdio.h>
#include <math.h>
#define DAQmxErrChk (functionCall) if( DAQmxFailed(error = (functionCall)) ) goto Error; else
#define PI 3.1415926535
#define DAQmxFailed(error) ((error)<0)
int32 CVICALLBACK DoneCallback (TaskHandle taskHandle, int32 status, void *callbackData);
int main(void)
{
int32 error = 0;
TaskHandle taskHandle = 0;
float64 data [1000];
char errBuff [2048] = {'\0'};
int i = 0;
for (; i < 1000; i++) data [i] = 9.95 * sin( (double) i * 2.0 * PI / 1000.0);
/*********************************************/
// DAQmx Configure Code
/*********************************************/
DAQmxErrChk (DAQmxCreateTask ("",&taskHandle) );
DAQmxErrChk (DAQmxCreateAOVoltageChan (taskHandle,"Dev1/ao0","",-10.0,10.0,DAQmx_Val_Volts,NULL) );
DAQmxErrChk (DAQmxCfgSampClkTiming (taskHandle,"",1000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000) );
DAQmxErrChk (DAQmxRegisterDoneEvent (taskHandle,0,DoneCallback,NULL) );
/*********************************************/
// DAQmx Write Code
/*********************************************/
DAQmxErrChk (DAQmxWriteAnalogF64 (taskHandle,1000,0,10.0,DAQmx_Val_GroupByChannel,data,NULL,NULL) );
/*********************************************/
// DAQmx Start Code
/*********************************************/
DAQmxErrChk (DAQmxStartTask(taskHandle));
printf ("Generating voltage continuously. Press Enter to interrupt\n");
getchar ();
Error:
if ( DAQmxFailed (error) )
DAQmxGetExtendedErrorInfo (errBuff,2048);
if ( taskHandle != 0 )
{
/*********************************************/
// DAQmx Stop Code
/*********************************************/
DAQmxStopTask (taskHandle);
DAQmxClearTask (taskHandle);
}
if ( DAQmxFailed(error) )
printf ("DAQmx Error: %s\n",errBuff);
printf ("End of program, press Enter key to quit\n");
getchar ();
return 0;
}
int32 CVICALLBACK DoneCallback (TaskHandle taskHandle, int32 status, void *callbackData)
{
int32 error = 0;
char errBuff [2048] = {'\0'};
// Check to see if an error stopped the task.
DAQmxErrChk (status);
Error:
if( DAQmxFailed (error) )
{
DAQmxGetExtendedErrorInfo (errBuff,2048);
DAQmxClearTask (taskHandle);
printf ("DAQmx Error: %s\n", errBuff);
}
return 0;
}
I would like to ask what generates the call to DoneCallback() function. Thanks.
Solved! Go to Solution.
02-05-2015 10:47 AM
That's easy to find out yourself
Search the code for this function name and you will see that this callback is due to DAQmxRegisterDoneEvent, so this callback function will be executed when the acquisition task stops.
02-05-2015 10:55 AM
how did I miss that?
02-05-2015 10:57 AM
by the way, I do not see the arguments passed into this callback function. how does it get its arguments then?
02-05-2015 11:09 AM
I already provided the link to the help of this function... : The last argument of DAQmxRegisterDoneEvent () is the callback data, in this case the NULL pointer
02-05-2015 11:15 AM
Sorry for the confusion, DAQmxRegisterDoneEvent() calls DoneCallback(). However, as you can see:
DAQmxErrChk (DAQmxRegisterDoneEvent (taskHandle,0,DoneCallback,NULL) );
DoneCallback() does not come with any argument or parentheses. So I wonder where they went.
02-05-2015 11:27 AM
ah, I see. This is because the type of callback function (int32 CVICALLBACK Callback (TaskHandle taskHandle, int32 status, void *callbackData)) is well defined, so only a pointer to the function is needed.
02-05-2015 11:32 AM
I do not quite get how that works. do you not have to have all arguments when calling a function? otherwise, where does it get all the arguments from, when called?
02-05-2015 11:43 AM
Yes, but all the arguments are already available...
So: DAQmxRegisterDoneEvent will internally call the callback function DoneCallback with all its arguments, i.e., it will call
DoneCallback (taskHandle, status, callbackData); those arguments are known to DAQmxRegisterDoneEvent because, for example, the taskhandle is the first parameter to DAQmxRegisterDoneEvent...
02-05-2015 11:51 AM
so the declaration of DoneCallback() also specifies its arguments?