访问外部生成的数组中的值

时间:2016-04-20 18:46:23

标签: c nidaqmx daq-mx

我正在读取电压读数并将其转换为压力读数。这是我对C的第一次真实体验,所以我的代码很乱,但到目前为止工作正常。我面临的问题是让程序计算读数的数量(存储在数组数据[i]中),它们介于最小和最大期望值之间。

这是代码,Sum2和count是给我带来麻烦的区域。 Sum2正在添加999个值而不是过滤,并且当它应该接近500

时,计数总是导致998

编辑:

数据[i]中的读数是电压,我正在努力工作。我的校准曲线是P =(V-2.9674)/。404

    //Voltage readings for NI USB-6009 built from the ground up

#include "stdafx.h"
#include "stdio.h"
#include "NIDAQmx.h"
#include "math.h"
#include "tdmwriter.h"
#include "fundtypes.h"
#include "platdefines.h"



#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else

int main(void)
{
    int32       error = 0;
    TaskHandle  taskHandle = 0;
    int32       read;
    float64     data[1000], Sum=0, Average, Variance, Deviation=0, std_dev, min=0, max=0, num=0, avg=0, minP=0, maxP=0, avgP=0, avgP2=0, minP2=0, maxP2=0, minV=0, maxV=0, avgV=0, Sum2 = 0;
    char        errBuff[2048] = { '\0' };
    int         i, count = 0;

    /*********************************************/
    // DAQmx Configure Code
    /*********************************************/
    DAQmxErrChk(DAQmxCreateTask("Pressure Voltage\n", &taskHandle));
    DAQmxErrChk(DAQmxCreateAIVoltageChan(taskHandle, "Dev1/ai0", "", DAQmx_Val_Cfg_Default, -10.0, 10.0, DAQmx_Val_Volts, NULL));
    DAQmxErrChk(DAQmxCfgSampClkTiming(taskHandle, "", 100.0, DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, 1000));

    /*********************************************/
    // DAQmx TDMS Configure Code
    /*********************************************/
    DAQmxErrChk(DAQmxConfigureLogging(taskHandle, "C:\\TestData\\LogFile.tdms", DAQmx_Val_LogAndRead, "ECS Test Data", DAQmx_Val_OpenOrCreate));
    //DAQmxWriteAnalogF64(taskHandle,1000, 1 /*autoStart*/, -1 /*timeout*/, DAQmx_Val_GroupByScanNumber, data, 1000, NULL);

    /*********************************************/
    // DAQmx Start Code
    /*********************************************/
    DAQmxErrChk(DAQmxStartTask(taskHandle));
    printf("Voltage due to Pressure:\n");
    printf("\n");
    printf("Recording Data...\n");
    DAQmxErrChk(DAQmxWaitUntilTaskDone(taskHandle, 10.0));


    /*********************************************/
    // DAQmx Read Code
    /*********************************************/
    DAQmxErrChk(DAQmxReadAnalogF64(taskHandle, 1000, 10.0, DAQmx_Val_GroupByChannel, data, 1000, &read, NULL));

    printf("Acquired %d points\n", (int)read);


    /*********************************************/
    //Display Values
    /*********************************************/
    printf("\n");
    printf("values:\n");
    printf("Voltage 1= %f\n",data[10]);
    printf("Voltage 2= %f\n", data[100]);
    printf("Voltage 3= %f\n", data[200]);
    printf("Voltage 4= %f\n", data[300]);
    printf("Voltage 5= %f\n", data[400]);
    printf("Voltage 6= %f\n", data[500]);
    printf("Voltage 7= %f\n", data[600]);
    printf("Voltage 8= %f\n", data[700]);
    printf("Voltage 9= %f\n", data[800]);
    printf("Voltage 10= %f\n", data[900]);
    printf("\n");

    //for (int i = 1; i < 1000; i++) {
    //  printf("Voltage %i= %f\n",i, data[i]);
    //}
    //printf("\n");

    /*********************************************/
    //Average Values
    /*********************************************/
    for (i = 1; i < 999; ++i) {
        Sum = Sum + data[i];
    }

    Average = (Sum / 999);
    printf("Average= %f\n", Average);


    /*********************************************/
    //Standard Deviation
    /*********************************************/
    for (i = 1; i < 999; ++i) {
        Deviation = Deviation + pow((Average - data[i]), 2);
    }

    Variance = Deviation / 999;
    printf("Variance= %f\n", Variance);

    std_dev = sqrt(Variance);
    printf("Standard Deviation= %f\n", std_dev);

    printf("\n");


    /*********************************************/
    //Min and Max Values
    /*********************************************/

    {
        max = fmax(data[2], data[999]);
    }
    {
        min = fmin(data[2], data[999]);
    }

    printf("Min: %f\n", min);
    printf("Max: %f\n", max);
    printf("Log File located in C:\\TestData. Please rename LogFile.tdms after testing\n");
    printf("\n");


    /*********************************************/
    //Convert to Pressure Readings
    /*********************************************/
    printf("Pressure Readings (inches H2O):\n");
    {
        minP = (min - 2.9674) / .404;
        maxP = (max - 2.9674) / .404;
        avgP = (Average - 2.9674) / .404;
    }
    printf("Min Pressure: %f\n", minP);
    printf("Max Pressure: %f\n", maxP);
    printf("Average Pressure: %f\n", avgP);
    printf("\n");


    /*********************************************/
    //New Voltage and Pressure Averages
    /*********************************************/
    {//target min and max pressure
        minP2 = avgP - (avgP / 10);
        maxP2 = avgP + (avgP / 10);
    }
    {//target min and max voltage
        minV = (minP2*.404) + 2.9674;
        maxV = (maxP2*.404) + 2.9674;
    }
    {//Sum of values in desired range
        for (i = 1; i < 999; ++i) {
            if (minV < data[i] && data[i] < maxV); { Sum2 = Sum2 + data[i]; }
    }
    }
    {//Number of values in desired range
        for (i = 1; i < 999; i++)
        {
            if (minV < data[i] && data[i] < maxV);
            {
                count++;
            }
        }
    }
    {//New average voltage
        avgV = Sum2 / count;
    }
    {//New average pressure
        avgP2 = ((avgV - 2.9674) / .404);
    }
    printf("Adjusted Values:\n");
    printf("Min P2= %f\n", minP2);
    printf("Max P2= %f\n", maxP2);
    printf("Min Voltage= %f\n", minV);
    printf("Max Voltage= %f\n", maxV);
    printf("Sum Voltage= %f\n", Sum2);
    printf("Count= %d\n", count);
    printf("AvgV= %f\n", avgV);
    printf("AvgP= %f\n", avgP2);


Error:
    if (DAQmxFailed(error))
        DAQmxGetExtendedErrorInfo(errBuff, 2048);
    if (taskHandle != 0) 
    {
        /*********************************************/
        // DAQmx Stop Code
        /*********************************************/

        DAQmxStopTask(taskHandle);
        DAQmxClearTask(taskHandle);
    }

    if (DAQmxFailed(error))
        printf("DAQmx Error: %s\n", errBuff);
    printf("End of program, press Enter key to quit...\n");
    getchar();
    return 0;
}

2 个答案:

答案 0 :(得分:0)

C是基于零的索引,因此您每次都跳过第一个值,这可能会或可能不会给出错误值。额外的括号导致您错过正确设置条件。我标记了坏;有评论所以你可以看到它。

sum2 = 0; // not needed since initialized above
count = 0; // not needed since initialized above
for (i = 0; i < 999; ++i) 
{
    if ((minV < data[i]) && (data[i] < maxV)) // ; messed up and forced code to execute
    // Extra parens are just for readability
    {
        //Sum of values in desired range
        Sum2 += data[i];
        //Number of values in desired range
        count++;
    }
}

答案 1 :(得分:0)

在以下行之后您有错误的;

if (minV < data[i] && data[i] < maxV);

if (minV < data[i] && data[i] < maxV);

这些会导致始终执行以下代码块,因为;会创建一个&#34; do-nothing&#34;代码块。

编辑:您是否对minVmaxV进行了错误的计算?他们与压力有什么关系?

minV = (minP2*.404) + 2.9674;
maxV = (maxP2*.404) + 2.9674;

也许这会使数据超出其范围,因为循环无法捕获任何值。