gpt4 book ai didi

python - Twisted 的 Serialport 和消失的串口设备

转载 作者:行者123 更新时间:2023-11-28 17:54:08 25 4
gpt4 key购买 nike

我正在使用 twisted.internet.serialport 让我的程序持续连接到串行端口上的设备。

不幸的是,我的串行端口只是一个 USB 设备,这意味着它可以随时被操作系统断开连接或重置(端口 2 被集线器禁用(EMI?),重新启用...)。我看到 pyserial 已经为此提供了几周的支持并引发了 SerialException。

我想做的是尝试重新连接到每隔几秒就消失的串口。

那么,有什么方法可以让 twisted 通知我断开连接吗?还是我应该继续为 pyserial 编写线程包装器?

谢谢

最佳答案

似乎分支版本中唯一相关的变化是在协议(protocol)中调用 connectionLost()。

直到它固定在后备箱中,我使用:

class fixedSerialPort(SerialPort):
def connectionLost(self, reason):
SerialPort.connectionLost(self, reason)
self.protocol.connectionLost(reason)

我用 Twisted 10.1(在 ubuntu 上)和 8.1(在我信任的 debian 上)测试了它。两者都很好。不过不知道其他操作系统。

关于python - Twisted 的 Serialport 和消失的串口设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3678661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com