2016-10-20 52 views
0

我正在開發一個小型項目來製作氣象站並使用Python編程收集數據。我正在使用這個氣象站click here.如何從Python Beaglebone使用Python獲取數據輸出

問題是,該氣象站只提供使用C編程的示例代碼,我不擅長這一點。 所以我決定使用Python,但數據輸出格式是這樣

c000s000g000t084r000p000h63b10040 
c000s000g000t084r000p000h63b10040 
c000s000g000t084r000p000h63b10040 
c000s000g000t084r000p000h63b10040 
c000s000g000t084r000p000h63b10040 
c000s000g000t084r000p000h63b10040 
c000s000g000t084r000p000h63b10040 
c000s000g000t084r000p000h63b10040 
c000s000g000t084r000p000h63b10040 

我使用小型機來查看輸出。我不明白他們的網站中的代碼示例是如何工作的,所以如果你可以給我解釋或示例它如何工作並將其轉換爲Python編程,那將非常棒。

這是他們的示例代碼用C編寫

 char     databuffer[35]; 
     double    temp; 

     void getBuffer()                 //Get weather status data 
     { 
      int index; 
      for (index = 0;index < 35;index ++) 
      { 
      if(Serial.available()) 
      { 
       databuffer[index] = Serial.read(); 
       if (databuffer[0] != 'c') 
       { 
       index = -1; 
       } 
      } 
      else 
      { 
       index --; 
      } 
      } 
     } 

     int transCharToInt(char *_buffer,int _start,int _stop)        //char to int) 
     { 
      int _index; 
      int result = 0; 
      int num = _stop - _start + 1; 
      int _temp[num]; 
      for (_index = _start;_index <= _stop;_index ++) 
      { 
      _temp[_index - _start] = _buffer[_index] - '0'; 
      result = 10*result + _temp[_index - _start]; 
      } 
      return result; 
     } 

     int WindDirection()                 //Wind Direction 
     { 
      return transCharToInt(databuffer,1,3); 
     } 

     float WindSpeedAverage()                //air Speed (1 minute) 
     { 
      temp = 0.44704 * transCharToInt(databuffer,5,7); 
      return temp; 
     } 

     float WindSpeedMax()                 //Max air speed (5 minutes) 
     { 
      temp = 0.44704 * transCharToInt(databuffer,9,11); 
      return temp; 
     } 

     float Temperature()                 //Temperature ("C") 
     { 
      temp = (transCharToInt(databuffer,13,15) - 32.00) * 5.00/9.00; 
      return temp; 
     } 

     float RainfallOneHour()                //Rainfall (1 hour) 
     { 
      temp = transCharToInt(databuffer,17,19) * 25.40 * 0.01; 
      return temp; 
     } 

     float RainfallOneDay()                //Rainfall (24 hours) 
     { 
      temp = transCharToInt(databuffer,21,23) * 25.40 * 0.01; 
      return temp; 
     } 

     int Humidity()                  //Humidity 
     { 
      return transCharToInt(databuffer,25,26); 
     } 

     float BarPressure()                 //Barometric Pressure 
     { 
      temp = transCharToInt(databuffer,28,32); 
      return temp/10.00; 
     } 

     void setup() 
     { 
      Serial.begin(9600); 
     } 
     void loop() 
     { 
      getBuffer();                  //Begin! 
      Serial.print("Wind Direction: "); 
      Serial.print(WindDirection()); 
      Serial.println(" "); 
      Serial.print("Average Wind Speed (One Minute): "); 
      Serial.print(WindSpeedAverage()); 
      Serial.println("m/s "); 
      Serial.print("Max Wind Speed (Five Minutes): "); 
      Serial.print(WindSpeedMax()); 
      Serial.println("m/s"); 
      Serial.print("Rain Fall (One Hour): "); 
      Serial.print(RainfallOneHour()); 
      Serial.println("mm "); 
      Serial.print("Rain Fall (24 Hour): "); 
      Serial.print(RainfallOneDay()); 
      Serial.println("mm"); 
      Serial.print("Temperature: "); 
      Serial.print(Temperature()); 
      Serial.println("C "); 
      Serial.print("Humidity: "); 
      Serial.print(Humidity()); 
      Serial.println("% "); 
      Serial.print("Barometric Pressure: "); 
      Serial.print(BarPressure()); 
      Serial.println("hPa"); 
      Serial.println(""); 
      Serial.println(""); 
     } 

謝謝。

回答

0

你必須「解碼」的數據格式作爲手冊(它輸出每秒37個字節,包括端CR/LF)中所建議:

c000:air direction, degree 
s000:air speed(1 minute), 0.1 miles per hour 
g000:air speed(5 minutes), 0.1 miles per hour 
t086:temperature, Fahrenheit 
r000:rainfall(1 hour), 0.01 inches 
p000:rainfall(24 hours), 0.01 inches 
h53:humidity,% (00%= 100) 
b10020:atmosphere,0.1 hpa 

例如,如果你有輸入線:

c000s000g000t084r000p000h63b10040 

然後,以獲得空氣方向[度]在C(上面的例子):

int WindDirection() //Wind Direction 
    { 
     return transCharToInt(databuffer,1,3); 
    } 

而且在Python :

def win_direction(): 
    return int(dataBuffer[1,3]) 

現在,您需要通過把所有的8份的數據結構,並建立一個功能,將提取數據,並在需要時轉換爲有意義的數字。

+0

很好,謝謝你的回覆,但是你能給出更多使用基於示例代碼的Python例子,如果你不介意的話。 我還是很困惑, –

+0

'TypeError:字符串索引必須是整數,而不是元組',這是我得到的。 –

+0

您需要分享您的編碼嘗試,您需要了解的基本知識是您有一個字符串,並且您需要以不同的方式分割每個分割結果。 –