• 用C语言给NI数据采集卡编程序实现多路数据的同时采集


      因为写的上一篇NI数据采集卡的程序有人留言说想要实现多路数据的同时采集,我没有及时回复,深感抱歉,在此写一篇关于NI数据采集卡的多路数据同时采集的程序

      第一个程序实现的功能:六路数据同时采集,采集有限个数据,并且保存到txt文档中。采用的是参考单端接地的方式,参考单端接地指的是我们测量的每一路的值是该路相对AI GND的电压值。对参考单端接地不了解的话可以看NI-DAQmx C Reference help


      不多说,上程序。

     1 #include <stdio.h>
     2 #include "NIDAQmx.h"
     3 #pragma comment(lib,"NIDAQmx.lib")
     4 #include<iostream>
     5 using namespace std;
     6  
     7 #define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else
     8  
     9 int main(void)
    10 {
    11     int32       error=0;
    12     int32        per_num;
    13     float64     data[6000];
    14     TaskHandle taskHandle=0;
    15     char        errBuff[2048]={''};
    16     DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
    17     DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,"Dev2/ai0:5","",DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,NULL));
    18     DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",20000,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000));
    19  
    20     DAQmxErrChk (DAQmxSetAITermCfg(taskHandle,"Dev2/ai0:5",DAQmx_Val_RSE));
    21     //DAQmxErrChk (DAQmxRegisterEveryNSamplesEvent(taskHandle,DAQmx_Val_Acquired_Into_Buffer,PER_NUM0,0,EveryNCallback,this));
    22     //DAQmxErrChk (DAQmxRegisterDoneEvent(taskHandle,0,DoneCallback,NULL));
    23  
    24     /*********************************************/
    25     // DAQmx Start Code
    26     /*********************************************/
    27     DAQmxErrChk (DAQmxStartTask(taskHandle));
    28     DAQmxErrChk(DAQmxReadAnalogF64(taskHandle,1000,10.0,DAQmx_Val_GroupByScanNumber,data,6000,&per_num,NULL));
    29     FILE *fp1=fopen("data1.txt","w+");
    30  
    31     for(int l=0;l<6000;l++)
    32     {
    33         fprintf(fp1,"%f",data[l]);
    34         fputc('
    ',fp1);
    35     }
    36     fclose(fp1);
    37 Error:
    38     if( DAQmxFailed(error) )
    39         DAQmxGetExtendedErrorInfo(errBuff,2048);
    40     if( taskHandle!=0 ) {
    41         /*********************************************/
    42         // DAQmx Stop Code
    43         /*********************************************/
    44         DAQmxStopTask(taskHandle);
    45         DAQmxClearTask(taskHandle);
    46     }
    47     if( DAQmxFailed(error) )
    48         printf("DAQmx Error: %s
    ",errBuff);
    49     printf("End of program, press Enter key to quit
    ");
    50     getchar();
    51     return 0;
    52 }

    这个程序实测过,可以使用,希望能够对大家有帮助。

    第二个程序实现的功能是对多路数据进行实时连续的采集与保存,采用的也是参考单端接地的方式。程序如下:

      1 #include <stdio.h>
      2 #include "NIDAQmx.h"
      3 #pragma comment(lib,"NIDAQmx.lib")
      4 #include<iostream>
      5 using namespace std;
      6  
      7 #define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else
      8  
      9 int32 CVICALLBACK EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData);
     10 int32 CVICALLBACK DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData);
     11  
     12 float64     data[6000];
     13  
     14 int main(void)
     15 {
     16     int32       error=0;
     17     TaskHandle  taskHandle=0;
     18     char        errBuff[2048]={''};
     19  
     20     /*********************************************/
     21     // DAQmx Configure Code
     22     /*********************************************/
     23     DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
     24     DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,"Dev2/ai0:5","",DAQmx_Val_Cfg_Default,-10.0,10.0,DAQmx_Val_Volts,NULL));
     25     DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",1000,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000));
     26  
     27     DAQmxErrChk (DAQmxSetAITermCfg(taskHandle,"Dev2/ai0:5",DAQmx_Val_RSE));
     28     DAQmxErrChk (DAQmxRegisterEveryNSamplesEvent(taskHandle,DAQmx_Val_Acquired_Into_Buffer,1000,0,EveryNCallback,NULL));
     29     DAQmxErrChk (DAQmxRegisterDoneEvent(taskHandle,0,DoneCallback,NULL));
     30  
     31     /*********************************************/
     32     // DAQmx Start Code
     33     /*********************************************/
     34     DAQmxErrChk (DAQmxStartTask(taskHandle));
     35  
     36     printf("Acquiring samples continuously. Press Enter to interrupt
    ");
     37     getchar();
     38 Error:
     39     if( DAQmxFailed(error) )
     40         DAQmxGetExtendedErrorInfo(errBuff,2048);
     41     if( taskHandle!=0 ) {
     42         /*********************************************/
     43         // DAQmx Stop Code
     44         /*********************************************/
     45         DAQmxStopTask(taskHandle);
     46         DAQmxClearTask(taskHandle);
     47     }
     48     if( DAQmxFailed(error) )
     49         printf("DAQmx Error: %s
    ",errBuff);
     50     printf("End of program, press Enter key to quit
    ");
     51     getchar();
     52     return 0;
     53 }
     54  
     55 int32 CVICALLBACK EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void *callbackData)
     56 {
     57     int32       error=0;
     58     char        errBuff[2048]={''};
     59     static int  totalRead=0;
     60     int32       read=0;
     61     float64        dddd=0.001;
     62     //float64     data[6000],last=0;
     63  
     64     /*********************************************/
     65     // DAQmx Read Code
     66     /*********************************************/
     67     DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,1000,10.0,DAQmx_Val_GroupByScanNumber,data,6000,&read,NULL));
     68     if( read>0 ) {
     69         for(int i=0;i<6000;i=i+6)
     70         {
     71             cout<<data[i]<<endl;
     72         }
     73         FILE *fp2=fopen("data2.txt","w+");
     74         for(int l=0;l<6000;l++)
     75          {
     76              fprintf(fp2,"%f",data[l]);
     77              fputc(',',fp2);
     78              fprintf(fp2,"%f",dddd*l);
     79              fputc('
    ',fp2);
     80          }
     81         fclose(fp2);
     82     }
     83  
     84 Error:
     85     if( DAQmxFailed(error) ) {
     86         DAQmxGetExtendedErrorInfo(errBuff,2048);
     87         /*********************************************/
     88         // DAQmx Stop Code
     89         /*********************************************/
     90         DAQmxStopTask(taskHandle);
     91         DAQmxClearTask(taskHandle);
     92         printf("DAQmx Error: %s
    ",errBuff);
     93     }
     94     return 0;
     95 }
     96  
     97 int32 CVICALLBACK DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData)
     98 {
     99     int32   error=0;
    100     char    errBuff[2048]={''};
    101  
    102     // Check to see if an error stopped the task.
    103     DAQmxErrChk (status);
    104  
    105 Error:
    106     if( DAQmxFailed(error) ) {
    107         DAQmxGetExtendedErrorInfo(errBuff,2048);
    108         DAQmxClearTask(taskHandle);
    109         printf("DAQmx Error: %s
    ",errBuff);
    110     }
    111     return 0;
    112 }

    这个程序也实际测试过,可以运行,希望这两个程序能对大家有帮助,谢谢观看,有问题请留言。

  • 相关阅读:
    PHP 开发 APP 接口 学习笔记与总结
    Java实现 LeetCode 43 字符串相乘
    Java实现 LeetCode 43 字符串相乘
    Java实现 LeetCode 43 字符串相乘
    Java实现 LeetCode 42 接雨水
    Java实现 LeetCode 42 接雨水
    Java实现 LeetCode 42 接雨水
    Java实现 LeetCode 41 缺失的第一个正数
    Java实现 LeetCode 41 缺失的第一个正数
    Java实现 LeetCode 41 缺失的第一个正数
  • 原文地址:https://www.cnblogs.com/ybqjymy/p/14373386.html
Copyright © 2020-2023  润新知