NI USB 6211 Reading Analog Voltage Input

2k views Asked by At

I'm trying to read a voltage input into my NI USB-6211 via a C program. 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. I'm pretty sure that comes from this code: 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

1

There are 1 answers

2
tinman On BEST ANSWER

The number 1000 that is displayed comes from the second and seventh parameters in your call to 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. So you asked for 1000 and got 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).

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.

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. They have a lot of manuals and it's easy to miss that one. The examples are usually pretty straightforward to adapt too.