Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

continuous data acquisition ,how to use the callback fuction

I want to use the example in NI help forcontinuous data acquisition( C:\Program Files\National Instruments\NI-DAQ\Examples\DAQmx ANSI C\Analog In\Measure Voltage\Cont Acq-Int Clk)Smiley Indifferent,but when I use the function “int32 CVICALLBACK EveryNCallback”,the program didnt seem to work,i wonder if there some problem that i use that function in MFC ,next is my program

 

 

 

#include "stdafx.h"
#include "zhanglei.h"
#include "zhangleiDlg.h"
#include "NIDAQmx.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <stdio.h>
#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else
static int32 CVICALLBACK EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData);
static int32 CVICALLBACK DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData);
/////////////////////////////////////////////////////////////////////////////
void CZhangleiDlg::OnButton1() 
{
// TODO: Add your control notification handler code here
int32       error=0;
TaskHandle  taskHandle=0;
char        errBuff[2048]={'\0'};
/*********************************************/
// DAQmx Configure Code
/*********************************************/
DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai0","",DAQmx_Val_NRSE,-10.0,10.0,DAQmx_Val_Volts,NULL));
DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",10000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000));
DAQmxErrChk (DAQmxRegisterEveryNSamplesEvent(taskHandle,DAQmx_Val_Acquired_Into_Buffer,1000,0,EveryNCallback,NULL));
DAQmxErrChk (DAQmxRegisterDoneEvent(taskHandle,0,DoneCallback,NULL));
/*********************************************/
// DAQmx Start Code
/*********************************************/
DAQmxErrChk (DAQmxStartTask(taskHandle));
//printf("Acquiring samples 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();
}
float64     data1[1000];
stastc int32 CVICALLBACK EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData)
{
int32       error=0;
char        errBuff[2048]={'\0'};
static int  totalRead=0;
int32       read=0;
float64     data[1000];
/*********************************************/
// DAQmx Read Code
/*********************************************/
DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,1000,10.0,DAQmx_Val_GroupByScanNumber,data,1000,&read,NULL));
if( read>0 )
{//printf("Acquired %d samples. Total %d\r",read,totalRead+=read);
for (int i;i<1000;i++)
{
data1[i]=data[i];
char buffer[20];
// int  i = 3445;
// long l = -344115L;
// unsigned long ul = 1234567890UL;
_ltoa( data[0],buffer, 10 );
AfxMessageBox(buffer);
}
}
Error:
if( DAQmxFailed(error) ) {
DAQmxGetExtendedErrorInfo(errBuff,2048);
/*********************************************/
// DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
printf("DAQmx Error: %s\n",errBuff);
}
return 0;
}
stastc 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;
}

#include "stdafx.h"#include "zhanglei.h"#include "zhangleiDlg.h"#include "NIDAQmx.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif#include <stdio.h>

#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else
int32 CVICALLBACK EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData);int32 CVICALLBACK DoneCallback(TaskHandle taskHandle, int32 status, void*callbackData);

0 Kudos
Message 1 of 7
(4,395 Views)

what error shows up when running your code? Could you help post your error screenshot here?

无惧边界
0 Kudos
Message 2 of 7
(4,381 Views)

Then you may interested in below thread(showing the multi-thread callback function):

 

http://digital.ni.com/public.nsf/allkb/B099001BEEFF41F0482571DA002E2C36

无惧边界
0 Kudos
Message 3 of 7
(4,379 Views)

 

Li Yi

many thanks for attention

      the problem is that  there is no error ,my program can run without any problem  but the point is it cant acquise any

data. i just wonder if i didnt write the call back function in a right way.and can you give some example of how to use the callback fuction in MFC.

   thank you again for your kindness.

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

LiYi:

thank you 

I attach my simple demo as a attenment.,and thank you for pay attention on it.

0 Kudos
Message 5 of 7
(4,357 Views)

Since this is the example shipped with the DAQmx driver, I think there will no problem with this code.

I suggest you debug step by step to see what happens. After reviewing your code in the main function, I did not found any errors.

 

Note: Every N Samples event occurs each time the specified number of samples(in the device FIFO buffer) are transferred from the device to the DAQmx driver. The callback contains code to read the specified number of samples from the DAQmx driver.

 

Also please note that don't put any code in the Every N Samples function to suspend it -- Just read the data out.

无惧边界
0 Kudos
Message 6 of 7
(4,315 Views)
hi LiYi thank you very much for your patient, and I wonder can you provide a sample of how to use the callback funvtion in Mfc? I real need your help and thank you for all you did for me!
0 Kudos
Message 7 of 7
(4,299 Views)