【问题标题】:How to skip first row when writing data from serial port?从串口写入数据时如何跳过第一行?
【发布时间】:2019-08-21 14:41:39
【问题描述】:

我的 Python 脚本将串行数据写入现有的 CSV 文件。我试图跳过作者的第一行,因为最初总是打印一些随机的垃圾字符。有什么简单的替代方法吗?

import serial
import sys
import os

def writer():
try:
    ser = serial.Serial('COM10', baudrate=9600)
    ser.flushInput()

    while True:

        ser_bytes = ser.readline()
        print(ser_bytes) 
        file = open("A.csv", "a")
        file.write(str(ser_bytes))
        file.close()          

    ser.close
except:
    print("Unexpected error:", sys.exc_info()[0])
writer()

我想将串行数据附加到我现有的 CSV 文件中没有初始垃圾字符。

【问题讨论】:

    标签: python arduino pyserial


    【解决方案1】:

    将 pyserial 版本降级到 2.5 可能会解决问题:

    pip uninstall pyserial
    pip install "pyserial==2.5"
    

    【讨论】:

    • 感谢您的回复!无论如何,我解决了我的问题,我只添加了一些带有布尔值的变量,并在内部使用 if else 条件来读取第一行。
    猜你喜欢
    • 2019-01-01
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多