Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

callback function is called too frequently

Dear community,
I am using NI9237 with cDAQ9172. I want to acquire 10 samples/second data and average them every 50 samples.  So my call back function would be called every 5 seconds.  But it is called sometimes every tic for the reasons I can't understand.  Please help me to configure it the right way.
 My code looks like:
 
#define NSAMPLES 50
{
..........................

error = DAQmxCreateTask("ADataAquision",&m_taskHandle);

error = DAQmxRegisterEveryNSamplesEvent(m_taskHandle,DAQmx_Val_Acquired_Into_Buffer,NSAMPLES,0,cb,callbackData);

error = DAQmxCfgSampClkTiming(m_taskHandle,

"",10, DAQmx_Val_Rising, DAQmx_Val_ContSamps, NSAMPLES);

error = DAQmxRegisterDoneEvent(m_taskHandle,0,DAQDoneCallBack,NULL);

error = DAQmxStartTask(m_taskHandle);

......................

}

I32 WINAPIV cb(TaskHandle taskHandle, I32 everyNsamplesEventType, U32 nSamples,

void *callbackData)

{

.......................

error = DAQReadAnalogF64(m_taskHandle, -, 10, DAQmx_Val_GroupByChannel, data, dataLen, nSamples, NULL);

...........................

return 0;

}

0 Kudos
Message 1 of 7
(7,136 Views)

I have encounted new detailes about the problem: If I call  DAQCreateAIVoltageChan(...) , everything works fine, if I call DAQAddGlobalChansToTask, call back is called too often.

 

error = DAQmxCreateTask("ADataAquision",&m_taskHandle);

// I create a global virtual name channel in MAX

DAQAddGlobalChansToTask(m_taskHandle,name);    // Call back is called too often

DAQCreateAIVoltageChan(m_taskHandle,"cDAQ1Mod8/ai0","",DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,NULL); // works just fine

error = DAQmxRegisterEveryNSamplesEvent(m_taskHandle,DAQmx_Val_Acquired_Into_Buffer,NSAMPLES,0,cb,callbackData);

error = DAQmxCfgSampClkTiming(m_taskHandle,

"",10, DAQmx_Val_Rising, DAQmx_Val_ContSamps, NSAMPLES);

error = DAQmxRegisterDoneEvent(m_taskHandle,0,DAQDoneCallBack,NULL);

error = DAQmxStartTask(m_taskHandle);

0 Kudos
Message 2 of 7
(7,124 Views)

 

 

Hi Oksana,

 

Thank you for posting to the NI forums.  I haven’t found any documentation on this type of behavior, and I haven’t been able to reproduce it either.  Would you mind posting back with an attachment of your code?  Also, are you using Visual Studio, and if so, which version?

 

I’ll continue looking into this problem and post back with my results. 

 

Ed W.

Applications Engineer

National Instruments

0 Kudos
Message 3 of 7
(7,099 Views)

Hi Edd,

Thank you for looking into my problem. I develop under VS 2005. I don't think the version is a matter. I created a simple win 32 application which reproduces the problem. What I found so far is that the problem exist with the voltage with excitation channel. If I created a normal voltage global virtual channel in MAX (for NI9211 e.g.), everything works as expected.

// PressChWithExcitProblem.cpp : Defines the entry point for the console application.

//

#include

"stdafx.h"

#include

"NIDAQmx.h"

#include

"windows.h"

#define

NSAMPLES 50

 

int32

_cdecl cb(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData)

{

int32 read=0, error;

float64 data[1000], sum = 0;

error = DAQmxReadAnalogF64(taskHandle, -1

/*read all*/, 10, DAQmx_Val_GroupByChannel, data, sizeof(data), &read, NULL);

printf(

"Tic: %d/t Read:%d\n", GetTickCount(), read);

return 0;

}

int32

_cdecl DAQDoneCallBack(TaskHandle taskHandle, int32 status, void *callbackData)

{

DAQmxClearTask(taskHandle);

return 0;

}

int

_tmain(int argc, _TCHAR* argv[])

{

int error;

TaskHandle tHandle;

error = DAQmxCreateTask(

"myTask", &tHandle);

// CTL16_PresInlet is a votage with excitation global virtual channel created via MAX

// If you create a simple voltage channel in MAX, and add it instead, everything works as expected

error = DAQmxAddGlobalChansToTask(tHandle,

"CTL16_PresInlet");

error = DAQmxRegisterEveryNSamplesEvent(tHandle,DAQmx_Val_Acquired_Into_Buffer,NSAMPLES,0,cb,NULL);

// 10 Hz aqcuisiotion frequency, 50 samples => the call back fnctn shell be called every 5 sec

// but it is called sometimes every tic.

error = DAQmxCfgSampClkTiming(tHandle,

"",10, DAQmx_Val_Rising, DAQmx_Val_ContSamps, NSAMPLES);

error = DAQmxRegisterDoneEvent(tHandle,0,DAQDoneCallBack,NULL);

error = DAQmxStartTask(tHandle);

printf(

"Acquiring samples continuously. Press Enter to interrupt\n");

getchar();

return 0;

}

0 Kudos
Message 4 of 7
(7,093 Views)

Hi Oksana,

 

I’ve been able to reproduce this issue in C++ and in LabVIEW, but I’m still trying to find the exact the cause of this behavior.  I’ll post back when I have more information.

 

Also, I noticed that some of the comments in your code indicate that the callbacks worked with a simple voltage channel.  Did you mean a strain channel - the 9172 does not support voltage tasks.  When I try running the code with a voltage task and the 9172, I don't receive any data back.

 

Ed W.

Applications Engineer

National Instruments

0 Kudos
Message 5 of 7
(7,080 Views)
Hi Oksana,

The 9237 can be used with a voltage with excitation task, but not a simple voltage channel, as I think you have discovered. Your problem probably stems from the minimum sampling rate of the 9237.  Please see this thread (particularly the first two posts by Salvador) for details, but you won't be able to run as slow as 10 S/s.  For your application, you can probably sample at a higher rate (like 5 kS/s) and average more points.

Regards,
Kyle
0 Kudos
Message 6 of 7
(7,064 Views)

Greetings Edd and Kyle,

Thank you for your help. It fully explains the software behavior, which means I shell revise my architecture a bit.  To answer the question about the simple voltage channel, I have a copmpact daq with a zoo of chassis, on which I was planning to collect data in a single task.

Thanks again for your time.

Oksana

0 Kudos
Message 7 of 7
(7,055 Views)