NI LabVIEW,CVI,数据采集等产品讨论区

取消
显示结果 
搜索替代 
您的意思是: 

DAQmxRegisterEveryNSamplesEvent callback not called in MFC

我已经装好NI-DAQmx驱动后,在下面的文件夹中找到连续采集的例子,C:\Users\Public\Documents\National Instruments\NI-DAQ\Examples\DAQmx ANSI C\Analog In\Measure Sound Pressure\Cont Acq Snd Press Samps-Int Clk 。此例程是采集声压的例子。在VS2010创建控制台程序,可以采集回来数据。但是移植到MFC程序框架中,编译通过,但是DAQmxRegisterEveryNSamplesEvent不会执行。即在MFC框架下无法采集到数据。
注:开发环境以及硬件:VS2010,NI9234采集卡。对声压数据进行采集。NI采集共有13通道进行声压数据的采集。
以下是控制台程序可以运行,MFC程序在附件中:

#include <afxwin.h>
#include <stdio.h>
#include <stdlib.h>
#include "NIDAQmx.h"
#include "wavhead.h"
#pragma comment(lib,"NIDAQmx.lib")
#include <iostream>
#include <atltime.h>
#include <string.h>
#include <atlstr.h>
using namespace std;
double data[3328];
double Inputbuffer[6656];
short databuf[3328]={0};
//int flag=0;
//int frm_count=0;
//double *p =(double *)calloc(3328,sizeof(double));
//short *m =(short *)calloc(3328,sizeof(short));
//pwave_header pwaveheader=(pwave_header)malloc(sizeof(struct WavHeader));

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

int32 CVICALLBACK EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData);
int32 DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData);
int main(void)
{
int32 error=0;
TaskHandle taskHandle=0;
char errBuff[2048]={'\0'};
DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
DAQmxErrChk (DAQmxCreateAIMicrophoneChan(taskHandle,"cDAQ1Mod1/ai0:3,cDAQ1Mod2/ai0:3,cDAQ1Mod3/ai0:3,cDAQ1Mod4/ai0","",DAQmx_Val_PseudoDiff,DAQmx_Val_Pascals,10,100.0,DAQmx_Val_Internal,0.002,NULL)); // 设定最大电平 灵敏度 激励电流值值等
DAQmxErrChk (DAQmxCfgInputBuffer(taskHandle,33280));
DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",25600.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,256)); //设定采样率 采样模式 每通道采样数(连续采样中为缓冲区大小设置)

DAQmxErrChk (DAQmxRegisterEveryNSamplesEvent(taskHandle,DAQmx_Val_Acquired_Into_Buffer,256,0,EveryNCallback,NULL));

DAQmxErrChk (DAQmxRegisterDoneEvent(taskHandle,0,DoneCallback,NULL));

DAQmxErrChk (DAQmxStartTask(taskHandle));

//DAQmxStopTask(taskHandle);
printf("Acquiring samples continuously. Press Enter to interrupt\n");
getchar();

Error:
if( DAQmxFailed(error) )
DAQmxGetExtendedErrorInfo(errBuff,2048);
if( taskHandle!=0 ) {
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 EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData)
{
int32 error=0;
char errBuff[2048]={'\0'};
static int totalRead=0;
int32 read=0;
//float64 dddd=0.001;
int l=0;

//double *p =(double *)calloc(3328,sizeof(double));
//short *m =(short *)calloc(3328,sizeof(short));*/
/*pwave_header pwaveheader;
pwaveheader=(pwave_header)malloc(sizeof(struct WavHeader)); */
/*recoder_wav_init(pwaveheader);
char *file="D:\\NI\\NILabview\\test_1\\test_2\\test_1\\20171128.wav";
if(flag==0)
{
WriteWavhead(file,pwaveheader);
flag=1;
}
else{*/
DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,256,0.13,DAQmx_Val_GroupByChannel,data,3328,&read,NULL));
if( read>0 ) {
printf("Acquired %d samples. Total %d\n",(int)read,(int)(totalRead+=read));
fflush(stdout);
//Multiplication(p,3328,m);

/*FILE *fp3=fopen("data7.txt","w+");
for(int l=0;l<3328;l++)
{
fprintf(fp3,"%f\n",data[l]);
printf("%f\n",data[l]);
}
fclose(fp3);
memcpy(Inputbuffer+3328,data,3328*sizeof(double));
memmove(Inputbuffer,Inputbuffer+3328,256*sizeof(double));
*/

/*FILE *fp2=fopen("data8.txt","w+");
for(int l=0;l<6656;l++)
{
fprintf(fp2,"%f\n",Inputbuffer[l]);
printf("%f\n",Inputbuffer[l]);
}
fclose(fp2); */
/*frm_count=frm_count+3328;
pwaveheader->datasum = frm_count;
pwaveheader->sizesum = frm_count+36;
WriteWav(file,pwaveheader,3328,(short*)p);*/
//DAQmxStopTask(taskHandle);

}
//DAQmxStopTask(taskHandle);

Error:
if( DAQmxFailed(error) ) {
DAQmxGetExtendedErrorInfo(errBuff,2048);
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
printf("DAQmx Error: %s\n",errBuff);
}

return 0;

}

int32 DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData)
{
int32 error=0;
char errBuff[2048]={'\0'};
DAQmxErrChk (status);

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

0 项奖励
1 条消息(共 1 条)
2,356 次查看