【发布时间】:2021-04-29 09:47:01
【问题描述】:
我有一个 PyQt5 应用程序,其中一个按钮触发与串行设备的通信。在应用程序运行时,它还会从相机中获取图像。但是,当串行通信忙于读/写时,GUI 不会更新,也不会显示来自相机的图像。
我试图通过 3 个单独的线程来解决这个问题 - 1:GUI,2:串行通信,3:图像抓取。它们之间的通信由 Signals 完成。不幸的是,当我通知第二个线程进行通信时,第一个线程(GUI)没有更新。
布局基本上是这样的:
Thread1 = GUI:
signal to Thread2, when serial comm requested
slot for Thread3, for image data grabbed from device
Thread2 = Serial comm:
slot for Thread1, for data to be send via serial port
Thread3 = Image grab:
signal to Thread1, when new image data is available
所以,当我需要通过串口发送一些东西时,Thread1 会向 Thread2 发出一个信号,然后应该继续执行它的消息循环,例如对来自 Thread3 的信号做出反应并绘制新图像。到 Thread2 的信号似乎被阻塞了,直到一切都在串行通信线程中完成。
Thread2 中的槽是这样的:
@pyqtSlot(int, int, int)
def motor_move(self, motor, direction, steps):
"""
Move motor one step in given direction.
Parameters
----------
motor : int
Motor index.
direction : int
Direction.
Returns
-------
None.
"""
if self._motor.serial_port:
self._motor.motor_move(motor, steps, direction) # here the serial communication happens
现在问题: 串口忙时如何解除对 GUI 的阻塞? 我可以发送一些表明信号已被处理的返回值吗?
【问题讨论】: