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("");
}
謝謝。
很好,謝謝你的回覆,但是你能給出更多使用基於示例代碼的Python例子,如果你不介意的話。 我還是很困惑, –
'TypeError:字符串索引必須是整數,而不是元組',這是我得到的。 –
您需要分享您的編碼嘗試,您需要了解的基本知識是您有一個字符串,並且您需要以不同的方式分割每個分割結果。 –