[英]NI USB 6211 Reading Analog Voltage Input
我試圖通過C程序讀取輸入到我的NI USB-6211的電壓。 為此,我嘗試使用已安裝程序隨附的一些示例程序,但無濟於事。 我看過文檔,但是說實話,它根本沒有幫助。
這是我改編的代碼。 (它有一些錯誤檢查,也要求輸入...)
/*********************************************************************
*
* ANSI C Example program:
* Acq-IntClk.c
*
* Example Category:
* AI
*
* Description:
* This example demonstrates how to acquire a finite amount of data
* using the DAQ device's internal clock.
*
* Instructions for Running:
* 1. Select the physical channel to correspond to where your
* signal is input on the DAQ device.
* 2. Enter the minimum and maximum voltages.
* Note: For better accuracy try to match the input range to the
* expected voltage level of the measured signal.
* 3. Select the number of samples to acquire.
* 4. Set the rate of the acquisition.
* Note: The rate should be AT LEAST twice as fast as the maximum
* frequency component of the signal being acquired.
*
* Steps:
* 1. Create a task.
* 2. Create an analog input voltage channel.
* 3. Set the rate for the sample clock. Additionally, define the
* sample mode to be finite and set the number of samples to be
* acquired per channel.
* 4. Call the Start function to start the acquisition.
* 5. Read all of the waveform data.
* 6. Call the Clear Task function to clear the task.
* 7. Display an error if any.
*
* I/O Connections Overview:
* Make sure your signal input terminal matches the Physical
* Channel I/O Control. For further connection information, refer
* to your hardware reference manual.
*
*********************************************************************/
#include <stdio.h>
#include <NIDAQmx.h>
#include <string.h>
#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else
int main(void)
{
int32 error=0;
int32 amount;
int32 i;
TaskHandle taskHandle=0;
int32 read;
float64 data[1000];
char errBuff[2048]={'\0'};
char c = 64;
/*********************************************/
// DAQmx Configure Code
/*********************************************/
printf("Please enter the amount of voltage checks you wish to run.\n");
//scanf("%d", &amount);
while(scanf("%d%c", &amount, &c) !=2)
{
getchar();
puts("Please enter a number.");
}
for (i = 0; i < amount; i++)
{
DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai0","",DAQmx_Val_Cfg_Default,1.0,10.0,DAQmx_Val_Volts,NULL));
DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",10000.0,DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,1000));
/*********************************************/
// DAQmx Start Code
/*********************************************/
DAQmxErrChk (DAQmxStartTask(taskHandle));
/*********************************************/
// DAQmx Read Code
/*********************************************/
DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,1000,10.0,DAQmx_Val_GroupByChannel,data,1000,&read,NULL));
printf("Acquired %d points\n",read);
Error:
if( DAQmxFailed(error) )
DAQmxGetExtendedErrorInfo(errBuff,2048);
if( taskHandle!=0 )
{
/*********************************************/
// DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
printf("Updating... ");
}
}
if(i=amount)
{
printf("End of Program, press the Enter key to quit\n");
getchar();
if( DAQmxFailed(error) )
printf("DAQmx Error: %s\n",errBuff);
}
return 0;
}
此刻所有代碼正在做的事情就是將數字1000打印出來,我也問過幾次。 我很確定這是從以下代碼得出的: float64 data[1000];
。 有誰知道如何獲得直流電壓讀數嗎? 即使只是一長串未格式化的數字(我能弄清楚)。
謝謝
顯示的數字1000
來自調用DAQmxReadAnalogF64()
的第二個和第七個參數。 第二個參數告訴您每個通道要采集多少個樣本。 sevent參數( &read
)告訴它在哪里存儲每個通道實際獲取了多少個樣本的結果。 因此,您要價1000,而得到1000。
在您的程序無法立即打印出已讀入的數據的那DAQmxReadAnalogF64()
。對DAQmxReadAnalogF64()
的調用執行數據獲取,並將其存儲在第五個參數指定的數組中(在您的情況下為data
)。
致電之后,您可以使用以下方法打印出電壓:
for (int i = 0; i < read; i++)
{
printf("Data point %d has value %f\n",i, data[i]);
}
盡管顯然可以打印出所有1000個值,但這可能不是您想要的。
如果要為NI庫編寫代碼,則應查看《 NI-DAQmx C參考幫助》以獲取有關功能及其參數的說明。 他們有很多手冊,很容易錯過。 這些示例通常也很容易適應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.