1

I pass a variable with a value of millivolts acquired by a microprocessor connected via serial port, to a class in my app. The class has to compare the received value "milliV" to many values corresponding to 10 °C increments. I have to do this for accuracy of the output because the output of a thermocouple is not linear. When the value of "milliV is lower than a specific value, then an integration calculation is made between the top and bottom closer values and returns a new value "_tempEx".

This class is working and returns the correct value but I think that beside if/if else and switch, there should be better way to achieve the same result. I have not coded the entire class which should contain approx. 150 comparison values in the range I need to cover. Moreover, having to do the same for a thermocouple type K, I would end up with hundreds of strings to compare.

Is there a better way to achieve the same result of the method in this class?

 public  class Termocoppia
{ 
    //milliV@temperature for thermocouple type "J"
    double t10=1.019, t20=1.537, t30=2.059, t40=2.585, t50=3.116;
    double t60=3.650, t70=4.187, t80=4.726, t90=5.269, t100=5.814;
    double t110=6.360, t120=6.909, t130=7.459, t140=8.010, t150=8.562;
    double t160=9.115, t170=9.669, t180=10.224, t190=10.779, t200=11.334;
    double t210=11.889, t220=12.445, t230=13.000, t240=13.555, t250=14.110;
    double t260=14.665, t270=15.219, t280=15.773, t290=16.327, t300=16.881;

    //Temperature References
    double d10 = 10.00, d20 = 20.00, d30 = 30, d40 = 40, d50 = 50;
    double d60 = 60, d70 = 70, d80 = 80, d90 = 90, d100 = 100;
    double d110 = 110, d120 = 120, d130 = 130, d140 = 140, d150 = 150;
    double d160 = 160, d170 = 170, d180 = 180, d190 = 190, d200 = 200;
    double d210=210, d220=220, d230=230, d240=240, d250=250, d260=260;
    double d270=270, d280=280, d290=290, d300=300;


    // find the highest value and the bottom one to integrate in between withthe  received milliV
    // returns the value with _tempEx
    public double Calcola(double milliV,  double _tempEx)
    {
        if (milliV <= t10)
        {
            _tempEx = d10;
        }

        else if (milliV <= t20)
        {
            _tempEx = d20 - ((t20 - milliV) / ((t20 - t10) / 10));//Questa formula è corretta
        }

        else if (milliV <= t30)
        {
            _tempEx = d30 - ((t30 - milliV) / ((t30 - t20) / 10));
        }

        else if (milliV <= t40)
        {
            _tempEx = d40 - ((t40 - milliV) / ((t40 - t30) / 10));
        }

        else if (milliV <= t50)
        {
            _tempEx = d50 - ((t50 - milliV) / ((t50 - t40) / 10));
        }
        ...........
        ...........
        else if (milliV <= t300)
        {
            _tempEx = d300 - ((t300 - milliV) / ((t300 - t290) / 10));
        }

        else
        {

        }

     return _tempEx;

 } 

I would appreciate answers with sample codes and/or pointing to usable references.

4

1 回答 1

1

正如已经指出的那样,您可以使用数组:

class Termocoppia
{
    // be sure to add all of your values here...
    double[] t = { 1.019, 1.537, 2.059, ... };
    double[] d = { 10, 20, 30, ... };

    public double Calcola(double milliV, double _tempEx)
    {
        if (milliV <= t[0])
        {
            // handle first case
            _tempEx = d[0];
        }
        else
        {
            bool success = false;
            int count = t.Length;
            // loop through all t values, test, and then calculate
            for (int idx = 1; idx < count; idx++)
            {
                if (milliV <= t[idx])
                {
                    _tempEx = d[idx] - 
                        ((t[idx] - milliV) / ((t[idx] - t[idx - 1]) / 10));
                    success = true;
                    break;
                }
            }

            if (success == false)
                throw new Exception("Unable to calculate _tempEX");
        }

        return _tempEx;
    }
}

t和值将d值存储在数组中。然后代码循环遍历t数组并检查条件 ( milliV <= t[idx])。如果这是真的,那么它使用对应的d[idx]t[idx-1]值来计算结果。

于 2013-05-11T17:59:24.697 回答