gpt4 book ai didi

c - NI USB 6211 读取模拟电压输入

转载 作者:行者123 更新时间:2023-11-30 15:46:37 26 4
gpt4 key购买 nike

我正在尝试通过 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() 中的第二个和第七个参数。第二个参数告诉您要为每个 channel 采集多少个样本。 Sevent 参数 (&read) 告诉它在哪里存储每个 channel 实际采集的样本数的结果。所以你要了 1000,得到了 1000。

此时您的程序并未打印出已读入的数据。调用DAQmxReadAnalogF64() 执行数据采集并将其存储在第五个参数中指定的数组中(在您的案例数据)。

在调用之后,您可以使用类似以下内容打印出电压:

for (int i = 0; i < read; i++)
{
printf("Data point %d has value %f\n",i, data[i]);
}

尽管这显然会打印出所有 1000 个值,但这可能不是您想要的。

如果您要为 NI 库编写代码,那么您应该查看 NI-DAQmx C 引用帮助以获取函数及其参数的说明。他们有很多手册,很容易错过那一本。这些示例通常也很容易适应。

关于c - NI USB 6211 读取模拟电压输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18229196/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com