【问题标题】:Reading a Serial Port with Python. How does the buffers work?使用 Python 读取串行端口。缓冲区如何工作?
【发布时间】:2011-03-10 13:08:27
【问题描述】:

我只是想知道缓冲区如何在 com 端口上工作。下面的代码是我如何读取 com 端口的片段。我想知道通过serial_connection.close()serial_connection.open() 是否会丢失任何数据,还是会保留在缓冲区中?你可能会问我为什么要关闭和打开 comport。原因是它实际上是一个虚拟端口,当我保持连接一段时间后数据停止传输时出于什么原因......

import serial

serial_connection = serial.Serial(
        port = self.SERIAL_PORT,
        baudrate = self.BAUD_RATE,
        timeout = 10
        )

while true:

    serial_connection.close()
    serial_connection.open()
    line = serial_connection.readline()
    print line

【问题讨论】:

    标签: python serial-port buffer


    【解决方案1】:

    PySerial 有一个单独的线程,用于监听数据以确保不会丢失任何内容。但是,操作系统本身并不缓冲数据。在关闭端口和再次打开端口之间的短暂时间内,您可能会丢失一些数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 2019-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-11
      相关资源
      最近更新 更多