Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PyQt小部件中的Arduino接口_Qt_Python 3.x_Arduino_Pyqt - Fatal编程技术网

PyQt小部件中的Arduino接口

PyQt小部件中的Arduino接口,qt,python-3.x,arduino,pyqt,Qt,Python 3.x,Arduino,Pyqt,我有一个PyQt程序,它有两个小部件。想法是与我的Arduino接口,并在程序中显示Arduinos信息。我真的不能附上我的整个计划,但我会给重点 Arduino通过ser.write串行接收命令,并使用ser.read() 因此,一个从Arduino持续读取信息的简单函数是 while True: ser.write(command.encode() time.sleep(.1) resp=ser.read() data=struct.unpack('<b

我有一个PyQt程序,它有两个小部件。想法是与我的Arduino接口,并在程序中显示Arduinos信息。我真的不能附上我的整个计划,但我会给重点

Arduino通过
ser.write
串行接收命令,并使用
ser.read()
因此,一个从Arduino持续读取信息的简单函数是

while True:
    ser.write(command.encode()
    time.sleep(.1)
    resp=ser.read()
    data=struct.unpack('<b',resp)

有人能解释为什么会发生这种情况,或者解释一下如何将更简单的
while
循环合并到Qt小部件中的解决方案吗?非常感谢

您需要存储对
QThread
的引用,这样它就不会被垃圾收集。像这样:

def start_cmd(self):
    self.downloader = CmdThread()
    self.downloader.start()
但是,请注意,再次单击按钮将用新的引用替换原始引用。因此,第一个线程可能会被垃圾回收(如果它完成了,这可能没问题)。您可能希望最终考虑一个更复杂的体系结构,其中总是有一个线程运行,并通过Qt信号/时隙从主线程向ARDUINO线程发送命令。 另一方面,我假设在某个时刻,您将使用arduino的结果来更新小部件。请确保不要直接从线程访问小部件(Qt GUI对象应该只从主线程访问)。相反,您需要从连接到主线程中插槽的线程发出信号(带有包含数据的参数)。然后,此插槽可以安全地访问GUI对象

显示了一个更复杂的示例,其中主线程和工作线程之间有双向通信(虽然没有随信号发射一起发送数据,但这是一个相当小的更改)

def start_cmd(self):
    self.downloader = CmdThread()
    self.downloader.start()