简体   繁体   中英

get data from usb weatherboard using PySerial

I am using USB Weatherboard V3 right now.

The data is sent every 1 seconds continously from /dev/ttyUSB* ... I have tried to get the data with PySerial but I still failed.

I just can get "RESET" in the output console. 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).

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 product : 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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