简体   繁体   English

使用PySerial从USB Weatherboard获取数据

[英]get data from usb weatherboard using PySerial

I am using USB Weatherboard V3 right now. 我现在正在使用USB Weatherboard V3。

The data is sent every 1 seconds continously from /dev/ttyUSB* ... I have tried to get the data with PySerial but I still failed. 数据每隔1秒从/ dev / ttyUSB *连续发送一次...我尝试使用PySerial获取数据,但仍然失败。

I just can get "RESET" in the output console. 我只能在输出控制台中得到“ RESET”。 I can't get the data. 我无法获取数据。

What I want is output like this : 我想要的是这样的输出:

SHT15 temperature: 2.5          75.1 deg F          
SHT15 humidity:                 65%                 
SHT15 dewpoint:                 62.7 deg F 
BMP085 pressure: 2 2011, 10:05:235.967 in Hg    FAIL
BMP085 temperature:             75.3 deg F          
TEMT6000 light:                 0.1%                
Weather meters wind speed: speci0.0 MPH         FAIL 
Weather meters wind direction:  -1 degrees      FAIL 
Weather meters rainfall:        0.00 inches     FAIL 
External power:                 0.00 Volts      FAIL 

^ it come from minicom (unix serial program). ^它来自minicom(unix串行程序)。

Can someone help me ? 有人能帮我吗 ?

Btw, this is my code currently : 顺便说一句,这是我目前的代码:

import serial;
import io;
import time;
import os;

# Weather board script #    

if __name__ == '__main__' :
    try :
        print '===================================\n'
        print 'USB Weatherboard V3 - Python Script'
        print 'Connection datasheet : '
        print '(+) Port : /dev/ttyUSB0'
        print '(+) Baud rate : 9600'
        print '(+) Type : 8N1'
        print '===================================\n'
        ser = serial.Serial(port='/dev/ttyUSB0', baudrate=9600, timeout=1, xonxoff=False, rtscts=False, dsrdtr=True)

        while True :
            arr = ser.readlines()
            for strarr in arr :
                s = strarr.decode('cp1252').replace('\00','')
                #s2 = s.encode('ascii');
                s2 = s
                if s2[1:3] != '[H' :
                    print s2
                    pass
                pass
            pass
    except :
        print 'Program exit !'
        pass
    finally :
        ser.close()

    pass

Please help me to correct it or if someone who has code it before me, please share with me :) 请帮助我纠正它,或者如果有人在我之前有代码,请与我分享:)

Weatherboard v3 datasheet : http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/Weather/USB_Weather_Board_V3_datasheet_110615.pdf Weatherboard v3数据表: http : //dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/Weather/USB_Weather_Board_V3_datasheet_110615.pdf

Weatherboard v3 product : https://www.sparkfun.com/products/10586 Weatherboard v3产品: https//www.sparkfun.com/products/10586

arr = ser.readlines()

reads all lines from the serial device, and only then returns. 从串行设备读取所有行,然后返回。 Since your device is sending data ad infinitum, you should just iterate over it to get the lines while they're written, like this: 由于您的设备无限发送数据,因此您应该对其进行迭代以获取编写时的行,如下所示:

import serial
if __name__ == '__main__':
    with serial.Serial(port='/dev/ttyUSB0', baudrate=9600, timeout=1,
                       xonxoff=False, rtscts=False, dsrdtr=True) as s:
        for line in s:
            print(s)

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

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