如何解决扭曲中对多个反应器的需求
我正在使用 qt4reactor 在 Linux 上运行 Qt 应用程序 应用程序在串行端口上发送和接收字节。 这在带有 QtReactor 的 Linux 上运行得很好,
但是当我将应用程序移植到 Windows 时,我遇到了问题。 在 Windows 上,我使用 _win32SerialPort 中的 SerialPort 类。 _win32SerialPort 中的文档字符串非常清楚:
Requires PySerial and win32all, and needs to be used with win32eventreactor.
我假设需要使用 win32eventreactor 是因为 addReader、addWriter 方法是为 Windows 编写的。
使用 QtReactor 时,一旦在传输上调用 LoseConnection,就会调用 twinsed.internet.abstract 中的 LoseConnection,最终调用 qt4reactor addWriter 方法(以刷新输出)。
然后创建一个 qt4reactor.TwistedSocketNotifier 尝试为 select() 获取文件描述符编号。 Abstract.fileno 方法不会被 _win32SerialPort 覆盖,因此总是返回 -1,并且我得到了一个
QSocketNotifier: Invalid Socket specified
I've see much posts about multiplereactor not allowed intwisted,但是我认为我在这里假设我需要 QtReactor 是正确的用于 Windows 串行端口的 Qt 应用程序和 win32eventreactor。
或者我可以使用其他一些解决方法吗?
注 1:在 Windows 上使用 QtReactor 时,串口工作正常,即可以发送和接收数据。只有当我关闭应用程序时,我才会收到“指定的套接字无效”
注2:现在我找到了解决方法。我使用 QtReactor,但是当关闭我的应用程序时,我会执行
serial.connectionLost(failure.Failure(Exception))
以下操作:serial 是 _win32serialport.SerialPort 的实例。
这样,abstract.loseConnection 永远不会被调用,这意味着 QtReactor addWriter 永远不会被调用来刷新输出。我怀疑最好的解决方案包括调用 LoseConnection 并正确刷新输出。
I am running a Qt application on Linux using the qt4reactor
The application sends and receives bytes on the serial port.
This works very well on Linux with the QtReactor
However when I port the application to windows then I have a problem.
On windows I use the SerialPort class from _win32SerialPort.
The doc string in _win32SerialPort is quite clear:
Requires PySerial and win32all, and needs to be used with win32eventreactor.
I assume the need to use win32eventreactor is because the addReader, addWriter methods are written for windows.
When the QtReactor is used, as soon as loseConnection is called on the transport, this calls loseConnection in twisted.internet.abstract which eventually calls the qt4reactor addWriter method (to flush the output).
This then creates a qt4reactor.TwistedSocketNotifier which tries to get a file descriptor number for select(). The abstract.fileno method is not overwritten by _win32SerialPort, so -1 is always returned and I get a
QSocketNotifier: Invalid Socket specified
I've seen many posts about multiple reactors not allowed in twisted, however I think I am correct here to assume that I need QtReactor for the Qt application and the win32eventreactor for the windows serial port.
Or is there some other workaround I can use ?
NOTE 1: when using QtReactor on windows, the serial ports work fine i.e. they can send and receive data. It is only when I close the application that I get "Invalid Socket specified"
Note 2: Now I found a workaround. I use the QtReactor, but when closing my application I do
serial.connectionLost(failure.Failure(Exception))
where serial is an instance of _win32serialport.SerialPort
This way abstract.loseConnection is never called which means that QtReactor addWriter is never called to flush the output. I suspect though that the best solution involves calling loseConnection and getting the output flushed properly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最近在 Windows 上的 selectreactor、iocpreactor 和 gtkreactor 添加了支持的串行端口。我希望这种方法也可以扩展到 Qt 反应器中支持它们。
I recently added serial port supported to selectreactor, iocpreactor, and gtkreactor on Windows. This approach can be extended to support them in the Qt reactor as well, I expect.
在Win32反应器的空闲事件中调用Qt消息检查/处理函数。
Call the Qt message checking/handling functions in the idle event of the Win32 reactor.