LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

ContGen-IntClk - DoneCallback

Solved!
Go to solution

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.

0 Kudos
Message 1 of 17
(5,844 Views)
Solution
Accepted by topic author hn_sofec

That's easy to find out yourself Smiley Wink

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.

 

Message 2 of 17
(5,841 Views)

how did I miss that?

0 Kudos
Message 3 of 17
(5,837 Views)

by the way, I do not see the arguments passed into this callback function.  how does it get its arguments then?

0 Kudos
Message 4 of 17
(5,834 Views)

I already provided the link to the help of this function... Smiley Wink: The last argument of DAQmxRegisterDoneEvent () is the callback data, in this case the NULL pointer

0 Kudos
Message 5 of 17
(5,826 Views)

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.

 

0 Kudos
Message 6 of 17
(5,820 Views)

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.

0 Kudos
Message 7 of 17
(5,818 Views)

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?

0 Kudos
Message 8 of 17
(5,817 Views)

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...

0 Kudos
Message 9 of 17
(5,813 Views)

so the declaration of DoneCallback() also specifies its arguments?

0 Kudos
Message 10 of 17
(5,811 Views)