簡體   English   中英

Python串行不讀一行

[英]Python Serial not reading on one line

我正在嘗試從EAS解碼器讀取串行數據,並使用python將其記錄到文本文件中。 當我使用商業串行記錄器時,這是我得到的:

1A Broadcast station or cable system has issued a Tornado Warning for all of 
 Iowa beginning at 7:54 pm and ending at 8:54 pm (MEWDOG)

運行文件時的輸出如下(我沒有包括整個輸出,但是您知道了):

輸出圖像

這是我的下面的代碼。 它只將第一個字符寫入文本文件,但我需要將其全部放在文本文件輸出的一行上。

import serial
import os
ser = serial.Serial()
ser.baudrate = 9600
ser.port = 'COM2'
ser.open()
def write_txt(EAS):
    import os.path
    fileName = "eas.txt"
    pathOfTxt = os.path.abspath(os.path.join(fileName))
    with open(pathOfTxt, 'w') as f:
        f.write(str(EAS)+'\n')
    return()

while True:
    message = ser.read()
    write_txt(message)
    print(message)

謝謝!

f.write(str(EAS)+'\n')

這就是問題。 您要在EAS之后添加換行符。 更改為:

f.write(str(EAS))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM