如何使用Python从UART Beaglebone获取数据输出

时间:2016-10-20 07:42:00

标签: python c++ beagleboneblack uart

我正在开展一个小项目来制作气象站并使用Python编程收集数据。我正在使用这个气象站click here.

问题是,气象站只提供使用C编程的示例代码,我并不擅长。 所以我决定使用Python,但数据输出格式是这样的

c000s000g000t084r000p000h63b10040
c000s000g000t084r000p000h63b10040
c000s000g000t084r000p000h63b10040
c000s000g000t084r000p000h63b10040
c000s000g000t084r000p000h63b10040
c000s000g000t084r000p000h63b10040
c000s000g000t084r000p000h63b10040
c000s000g000t084r000p000h63b10040
c000s000g000t084r000p000h63b10040

我正在使用minicom来查看输出。我不明白他们网站上的代码示例是如何工作的,所以如果你能给我解释或者一些例子如何工作并将其转换为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("");
        }

谢谢。

1 个答案:

答案 0 :(得分: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个部分并构建一个函数,该函数将提取数据并在需要时将其转换为有意义的数字。