简体   繁体   English

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

[英]How to get data output from UART Beaglebone using Python

i'm working on little project to make weather station and collect the data using Python programming. 我正在做一个小项目,使气象站和使用Python编程收集数据。 I'm using this weather station click here. 我正在使用此气象站, 请单击此处。

The problem is, that weather station is only providing sample code using C programming, i'm not good with that. 问题是,气象站仅使用C编程提供示例代码,我对此不太满意。 So i decide to using Python, but the data output format is like this 所以我决定使用Python,但是数据输出格式是这样的

c000s000g000t084r000p000h63b10040
c000s000g000t084r000p000h63b10040
c000s000g000t084r000p000h63b10040
c000s000g000t084r000p000h63b10040
c000s000g000t084r000p000h63b10040
c000s000g000t084r000p000h63b10040
c000s000g000t084r000p000h63b10040
c000s000g000t084r000p000h63b10040
c000s000g000t084r000p000h63b10040

I'm using minicom to see the output. 我正在使用minicom来查看输出。 I'm not understand how the code sample in their website works, so if you can give me explanation or some example how it works and convert it to Python programming, that will be great. 我不了解他们网站上的代码示例的工作方式,因此,如果您可以给我解释或提供一些示例示例,说明其如何工作并将其转换为Python编程,那就太好了。

This is their sample code written with C 这是他们用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("");
        }

Thank you. 谢谢。

You have to "decode" the data format as suggested in the manual (It outputs 37 bytes per second, including the end CR/LF): 您必须按照手册中的建议“解码”数据格式(每秒输出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

For example if you have the input line: 例如,如果您有输入行:

c000s000g000t084r000p000h63b10040

Then to get the air direction [degree] in c (The example above): 然后以c为单位获取风向[度](上面的示例):

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

And in Python: 在Python中:

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

You now need to pass threw all the 8 parts of the data structure and build a function that will extract the data and convert it to meaningful number if needed. 现在,您需要传递数据结构的所有8个部分,并构建一个函数来提取数据,并在需要时将其转换为有意义的数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM