简体   繁体   English

NI USB 6211读取模拟电压输入

[英]NI USB 6211 Reading Analog Voltage Input

I'm trying to read a voltage input into my NI USB-6211 via a C program. 我试图通过C程序读取输入到我的NI USB-6211的电压。 To that end, I tried using some of the example programs that came with the installed program, to no avail. 为此,我尝试使用已安装程序随附的一些示例程序,但无济于事。 I've looked at the documentation, but to be honest it doesn't quite help, at all. 我看过文档,但是说实话,它根本没有帮助。

This the code that I have adapted. 这是我改编的代码。 (It has some error checking in and also asks for an input...) (它有一些错误检查,也要求输入...)

/*********************************************************************
*
* 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;
}

All the code is doing at this moment is printing out the number 1000 how many times I ask it too. 此刻所有代码正在做的事情就是将数字1000打印出来,我也问过几次。 I'm pretty sure that comes from this code: float64 data[1000]; 我很确定这是从以下代码得出的: float64 data[1000]; . Does anyone have any knowledge on how to get a direct voltage read? 有谁知道如何获得直流电压读数吗? Even if it is just a long string of numbers which haven't been formatted (I can figure that out). 即使只是一长串未格式化的数字(我能弄清楚)。

Thanks 谢谢

The number 1000 that is displayed comes from the second and seventh parameters in your call to DAQmxReadAnalogF64() . 显示的数字1000来自调用DAQmxReadAnalogF64()的第二个和第七个参数。 The second parameter tells how many samples for each channel you want to take. 第二个参数告诉您每个通道要采集多少个样本。 The sevent parameter ( &read ) tells it where to store the result of how many samples per channel were actually taken. sevent参数( &read )告诉它在哪里存储每个通道实际获取了多少个样本的结果。 So you asked for 1000 and got 1000. 因此,您要价1000,而得到1000。

At the minute your program is not printing out the data that has been read in. The call to DAQmxReadAnalogF64() performs the data acquisition and stores it in the array specified in the fifth parameter (in your case data ). 在您的程序无法立即打印出已读入的数据的那DAQmxReadAnalogF64() 。对DAQmxReadAnalogF64()的调用执行数据获取,并将其存储在第五个参数指定的数组中(在您的情况下为data )。

After that call you can print out your voltages using something like: 致电之后,您可以使用以下方法打印出电压:

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

Although that would obviously print out all 1000 values which is probably not what you want. 尽管显然可以打印出所有1000个值,但这可能不是您想要的。

If you are going to code for the NI libraries then you should look at the NI-DAQmx C Reference Help for an explanation of the functions and their parameters. 如果要为NI库编写代码,则应查看《 NI-DAQmx C参考帮助》以获取有关功能及其参数的说明。 They have a lot of manuals and it's easy to miss that one. 他们有很多手册,很容易错过。 The examples are usually pretty straightforward to adapt too. 这些示例通常也很容易适应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM