用C語言給NI資料採集卡程式設計序實現多路資料的同時採集
因為寫的上一篇NI資料採集卡的程式有人留言說想要實現多路資料的同時採集,我沒有及時回覆,深感抱歉,在此寫一篇關於NI資料採集卡的多路資料同時採集的程式
第一個程式實現的功能:六路資料同時採集,採集有限個數據,並且儲存到txt文件中。採用的是參考單端接地的方式,參考單端接地指的是我們測量的每一路的值是該路相對AI GND的電壓值。對參考單端接地不瞭解的話可以看NI-DAQmx C Reference help
不多說,上程式。
#include <stdio.h>
#include "NIDAQmx.h"
#pragma comment(lib,"NIDAQmx.lib")
#include<iostream>
using namespace std;
#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else
int main(void)
{
int32 error=0;
int32 per_num;
float64 data[6000];
TaskHandle taskHandle=0;
char errBuff[2048]={'\0'};
DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,"Dev2/ai0:5","",DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,NULL));
DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",20000,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000));
DAQmxErrChk (DAQmxSetAITermCfg(taskHandle,"Dev2/ai0:5",DAQmx_Val_RSE));
//DAQmxErrChk (DAQmxRegisterEveryNSamplesEvent(taskHandle,DAQmx_Val_Acquired_Into_Buffer,PER_NUM0,0,EveryNCallback,this));
//DAQmxErrChk (DAQmxRegisterDoneEvent(taskHandle,0,DoneCallback,NULL));
/*********************************************/
// DAQmx Start Code
/*********************************************/
DAQmxErrChk (DAQmxStartTask(taskHandle));
DAQmxErrChk(DAQmxReadAnalogF64(taskHandle,1000,10.0,DAQmx_Val_GroupByScanNumber,data,6000,&per_num,NULL));
FILE *fp1=fopen("data1.txt","w+");
for(int l=0;l<6000;l++)
{
fprintf(fp1,"%f",data[l]);
fputc('\n',fp1);
}
fclose(fp1);
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;
}
這個程式實測過,可以使用,希望能夠對大家有幫助。
第二個程式實現的功能是對多路資料進行實時連續的採集與儲存,採用的也是參考單端接地的方式。程式如下:
#include <stdio.h>
#include "NIDAQmx.h"
#pragma comment(lib,"NIDAQmx.lib")
#include<iostream>
using namespace std;
#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);
float64 data[6000];
int main(void)
{
int32 error=0;
TaskHandle taskHandle=0;
char errBuff[2048]={'\0'};
/*********************************************/
// DAQmx Configure Code
/*********************************************/
DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,"Dev2/ai0:5","",DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,NULL));
DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",1000,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000));
DAQmxErrChk (DAQmxSetAITermCfg(taskHandle,"Dev2/ai0:5",DAQmx_Val_RSE));
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();
return 0;
}
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 dddd=0.001;
//float64 data[6000],last=0;
/*********************************************/
// DAQmx Read Code
/*********************************************/
DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,1000,10.0,DAQmx_Val_GroupByScanNumber,data,6000,&read,NULL));
if( read>0 ) {
for(int i=0;i<6000;i=i+6)
{
cout<<data[i]<<endl;
}
FILE *fp2=fopen("data2.txt","w+");
for(int l=0;l<6000;l++)
{
fprintf(fp2,"%f",data[l]);
fputc(',',fp2);
fprintf(fp2,"%f",dddd*l);
fputc('\n',fp2);
}
fclose(fp2);
}
Error:
if( DAQmxFailed(error) ) {
DAQmxGetExtendedErrorInfo(errBuff,2048);
/*********************************************/
// DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
printf("DAQmx Error: %s\n",errBuff);
}
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;
}
這個程式也實際測試過,可以執行,希望這兩個程式能對大家有幫助,謝謝觀看,有問題請留言。
轉:https://blog.csdn.net/lubilie969/article/details/78438613