Question

I have a serial connection that acquires data from a micro connected to a J thermocouple. The microprocessor sends a numeric value from 0 to 1023 proportional to the measured millivolts. The data from serial are stored in a variable "Thm1" which is continuously updated. My target is to calculate the temperature reconverting the received data and show the same in a textbox. The output of the thermocouple is not linear and therefore I cannot use an equation, I should read the data from a table that gives the millivolt/temperture in steps of 5 degrees and integrate the received value between the two closest values.

Lets assume that 1023 correspond to 16,881 mV. I have therefore 1023 point each one is 0.01650 mV. I receive from serial 800 which correspond to 0,016550 x 800 = 13,2012 mV. Looking at this table pyromation.com/downloads/data/emfj_c.pdf , first coloumn on the left, the value is between 240 and 250 degree C. I can make a linear integration between those two point. But, how can I found those two points? Is there a better way than using a long series of if and if else?

Please give examples.

Was it helpful?

Solution

you can do a linear extrapolation like :-

public static decimal ExtrapolateFrom(int f, int s, decimal f1, decimal s2, int value)
{            
    return (s2-f1)/((s-(decimal)f)/(value-(decimal)f))+f1;         
}

public static decimal ExtrapolateFrom(List<Tuple<int, decimal>> table, int value)
{
    if(table.Count < 2) throw  new Exception("Not enough values to extrapolate from");
    var result = table.Select((x, i) => new { x, i }).Where(x => x.x.Item1 >= value).Select(x => x.i).ToList();
    var index = result.Any()? result.First() : table.Count-1;
    if (index < 1) index = 1;    
    return ExtrapolateFrom(table[index - 1].Item1, table[index].Item1, table[index - 1].Item2,table[index].Item2, value);
}


private static void Main(string[] args)
{
    var table = new List<Tuple<int, decimal>> ()
        {
            new Tuple<int, decimal>(0, 0.0M),
            new Tuple<int, decimal>(100, 5.0M),
            new Tuple<int, decimal>(200, 6.0M),
            new Tuple<int, decimal>(300, 9.0M),
            new Tuple<int, decimal>(500, 11.0M),
        };


    Console.WriteLine(ExtrapolateFrom(table, 50));
    Console.WriteLine(ExtrapolateFrom(table, 400));
    Console.WriteLine(ExtrapolateFrom(table, 600));
}

The ExtrapolateFrom that takes a table does :-

  • checks to make sure theres at least 2 cutoffs extrapolate from
  • finds the first cutoff in the table that is greater than the value you are wanting to convert
  • checks if we have a value greater than the the table specifies, in which case use the last two cutoffs
  • if we have a value less than the table specifies, in which case use the first two cutoffs
  • uses the two table points to do a linear extrapolation.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top