python串口读取,然后覆盖到.txt文件

时间:2014-08-21 11:59:54

标签: python serial-port

半新手在这里工作,但显然我打破了它,我错过了什么?现在没有任何事情发生,而昨天串行数据将回显到屏幕并保存到文件中。我尝试了sudo cat / dev / ttyUSB0,但没有显示任何内容,但如果我将USB线连接到Windows框,数据就在那里。提前thx!

#!/usr/bin/python
# get lines of text from serial port, save them to a file

from __future__ import print_function
import serial, io

    addr  = '/dev/ttyUSB0'  # serial port to read data from
    baud  = 9600            # baud rate for serial port
    fname = '/home/pi/scripts/radar/radarout.txt'   # log file to save data in
    fmode = 'w'             # log file mode = append

    with serial.Serial(addr,baud) as pt, open(fname,fmode) as outf:
    spb = io.TextIOWrapper(io.BufferedRWPair(pt,pt,1),
        encoding='ascii', errors='ignore', newline='\r',line_buffering=True)
    spb.readline()  # throw away first line; likely to start mid-sentence (incomplete)
    while (1):
        x = spb.readline()  # read one line of text from serial port
        print (x,end='')    # echo line of text on-screen
        outf.write(x)       # write line of text to file
        outf.flush()        # make sure it actually gets written out

0 个答案:

没有答案