2013-05-11 35 views
1

我將通過串口連接的微處理器獲取的毫伏數變量傳遞給我的應用程序中的類。 該課程必須將收到的值「milliV」與許多值相比較,以10°C爲增量進行比較。因爲熱電偶的輸出不是線性的,所以我必須這樣做才能保證輸出的準確性。當「milliV」的值小於特定值時,則對頂部和底部較近的值進行積分計算並返回一個新值「_tempEx」。將變量與數百個值進行比較

該類正在工作並返回正確的值,但I認爲除了if/if else和switch之外,還應該有更好的方法來達到同樣的結果,我沒有編碼整個類,它應該包含我需要覆蓋的大約150個比較值 此外,必須做的對於K型熱電偶一樣,我最終會與上百串的比較。

有沒有更好的方式來實現這個類中的方法相同的結果呢?

public class Termocoppia 
{ 
    //[email protected] 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; 

} 

我將不勝感激示例代碼的答案和/或指向可用的參考。

+5

這聽起來像是你真的想要一個數組或一個列表...... – 2013-05-11 17:26:25

+0

或者一個'Dictionary'如果溫度和熱電偶沒有在連續範圍內映射。 – 2013-05-11 17:46:40

回答

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

td值存儲在一個數組中的值。該代碼然後通過t陣列循環並檢查條件(milliV <= t[idx])。如果這是真的,則它使用相應的d[idx]t[idx-1]值來計算結果。

+0

非常感謝,非常感謝。有沒有一種方法可以將這些值從項目資源中複製的Excel表格中拷入數組中?或類似的東西。 – FeliceM 2013-05-11 18:22:12

+0

@ FeliceM - 是的,這是可能的。但是,閱讀CSV或文本文件會更容易。谷歌應該返回大量的結果。這可能有所幫助:http://stackoverflow.com/questions/9777206/c-sharp-read-txt-file-and-store-the-data-in-formatted-array – 2013-05-11 18:48:57

+0

是的,你是對的,谷歌永遠是你最好的朋友。感謝您的支持。 – FeliceM 2013-05-11 18:55:56

相關問題