Counter/Timer

cancel
Showing results for 
Search instead for 
Did you mean: 

Callback

I'm trying to get a DAQ callback to work with a 6602 and C in Windows.
All I need is a message/function call when the output of one of my
counters goes high/low. I see a few examples dealing with other
cards, but can't find an example for a timer. Any help?
Thanks
0 Kudos
Message 1 of 4
(4,350 Views)
Mark,
Unfortunately, there is no callback function that detects the change of the counter output when it goes high/low. Even though a function like that does not exist, there is a function (for Trad DAQ - "CTR_State") that will return the OUT logic level of the specified counter, but you would have to "poll" the counter to check its state. This function will not detect a change if you are not calling it. You can find the counter functions for the Traditional DAQ driver in the Traditional NI-DAQ Function Reference Help, located in All Programs -> National Instruments -> NI DAQ, and then Search for CTR. If you are using NI-DAQmx, you can find the NI-DAQmx C Reference Help in the same location as the Traditional. There are "change detection" functions in DAQmx, but
they are for Digital I/O, not specifically for counters. If you connected your counter to an Input, you'd be able to use the change detection functions.

As far as examples, there aren't any specifically for this type of performance. There are examples for the 6602, and these can be found in the NI-DAQ\Examples\ folder. Select the type of programming language you are using, and then look for the counter folder(s).

DJ Loberg
National Instruments
www.ni.com/ask
0 Kudos
Message 2 of 4
(4,349 Views)

I found this example in nidaqmx example directory (Read Dig Chan-Change Detection Event) 

Iit compiles , but it doesn't work..... i have this error

 

Error.JPG

 

 

 

 

WHY ? What does it mean ?

 

 

 

 

 

Here is the entire code, in red (the wich generates the error).

 

 

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <NIDAQmx.h>

#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else

static TaskHandle    taskHandle;
static uInt32        numLines;
static uInt8        cachedData[200];

int32 CVICALLBACK ChangeDetectionCallback(TaskHandle taskHandle, int32 signalID, void *callbackData);

void Cleanup (void);

int main(void)
{
    int32       error=0;
    char        errBuff[2048]={'\0'};

    /*********************************************/
    // DAQmx Configure Code
    /*********************************************/
    DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
    DAQmxErrChk (DAQmxCreateDIChan(taskHandle,"Dev1/port0/line0:7","",DAQmx_Val_ChanPerLine));
    DAQmxErrChk (DAQmxCfgChangeDetectionTiming(taskHandle,"Dev1/port0/line0:7","Dev1/port0/line0:7",DAQmx_Val_ContSamps,1));
    DAQmxErrChk (DAQmxRegisterSignalEvent(taskHandle,DAQmx_Val_ChangeDetectionEvent,0,ChangeDetectionCallback,NULL));
    DAQmxErrChk (DAQmxGetTaskNumChans(taskHandle,&numLines));

    /*********************************************/
    // DAQmx Start Code
    /*********************************************/
    DAQmxErrChk (DAQmxStartTask(taskHandle));

    puts("Continuously reading. Press Enter key to interrupt\n");

    puts("Timestamp                 Data read   Changed Lines");

    getchar();

Error:
    if( DAQmxFailed(error) )
    {
        DAQmxGetExtendedErrorInfo(errBuff,2048);
        Cleanup();
        printf("DAQmx Error: %s\n",errBuff);
    }
    printf("End of program, press Enter key to quit\n");
    getchar();
    return 0;
}

int32 CVICALLBACK ChangeDetectionCallback(TaskHandle taskHandle, int32 signalID, void *callbackData)
{
    int32   error=0;
    uInt8   data[200]={0};
    int32   numRead;
    uInt32  i=0;
    char    buff[512], *buffPtr;
    char    errBuff[2048]={'\0'};
    char    *timeStr;
    time_t    currTime;

    if( taskHandle ) {
        time (&currTime);
        timeStr = ctime(&currTime);
        timeStr[strlen(timeStr)-1]='\0';  // Remove trailing newline.

        /*********************************************/
        // DAQmx Read Code
        /*********************************************/
        DAQmxErrChk (DAQmxReadDigitalLines(taskHandle,1,10.0,DAQmx_Val_GroupByScanNumber,data,8,&numRead,NULL,NULL));

        if( numRead ) {
            buffPtr = buff;
            strcpy(buff, timeStr);

            strcat(buff,"  ");
            buffPtr = buff + strlen(buff);
            for(;i<numLines;++i) {
                sprintf(buffPtr,"%d",data[i]);
                buffPtr++;
            }

            strcat(buff,"    ");
            buffPtr = buff + strlen(buff);
            for(i=0;i<numLines;++i) {
                sprintf(buffPtr,"%c",data[i]==cachedData[i]?'-':'X');
                buffPtr++;
                cachedData[i] = data[i];
            }
            puts(buff);
            fflush(stdout);
        }
    }
    return 0;

Error:
    if( DAQmxFailed(error) )
    {
        DAQmxGetExtendedErrorInfo(errBuff,2048);
        Cleanup();
        printf("DAQmx Error: %s\n",errBuff);
    }
    return 0;
}

void Cleanup (void)
{
    if( taskHandle!=0 )
    {
        /*********************************************/
        // DAQmx Stop Code
        /*********************************************/
        DAQmxStopTask(taskHandle);
        DAQmxClearTask(taskHandle);
        taskHandle = 0;
    }
}
 



There are 10 kinds of people. Those who understand binary notation, and those who do not.
0 Kudos
Message 3 of 4
(3,775 Views)

Hi blacksocket,

 

The error you are getting indicates that the hardware does not support your settings.  In this case, the 6602 does not support change detection (here is a list of hardware that does support this feature).  Let's use your other thread to look into possible alternatives.

 

 

Best Regards,

John Passiak
Message 4 of 4
(3,754 Views)