【问题标题】:asyncio pyserial - how to determine if read buffer is emptyasyncio pyserial - 如何确定读取缓冲区是否为空
【发布时间】:2023-02-02 04:52:41
【问题描述】:

我正在使用 asyncio 并尝试使用使用 asyncio.StreamReader 的 pyserial-asyncio 从串行端口异步读取。在同步 pyserial 中,可以使用客户端的 in_waiting getter 来确定读取缓冲区中是否有任何内容。

asyncio.StreamReader 有 at_eof() 成员,但这依赖于用户断言 feed_eof()

其他人有 suggested 只是做 await asyncio.StreamReader.read() 并检查空字节,但至少在 pyserial-asyncio 的情况下,这永远不会返回空字节 - 它只是无限期地等待(当实际收到一些字节时返回)

我可以使用 asyncio.wait_for() 设置超时并将其用作缓冲区为空的线索,但这显然不是最佳选择。

如何有效地检查空的读取缓冲区?

【问题讨论】:

    标签: python stream python-asyncio pyserial


    【解决方案1】:

    尝试直接访问缓冲区asyncio.StreamReader._buffer。空缓冲区检查是 lake len(stdout._buffer)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 2011-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多