PyQt5 - 自动化串行模块 [英] PyQt5 - Automate Serial module

查看:34
本文介绍了PyQt5 - 自动化串行模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在不单击按钮的情况下自动进行串行连接.当gui加载时,串行应该被立即读取并每隔一段时间刷新,无需使用鼠标触发任何按钮(即自动读取和更新).

I am trying to automate serial connection without clicking on button. When the gui load, serial should be read immediately and refreshed at interval without using the mouse to trigger any button (that is auto read in and updated).

这怎么可能?请参阅下面的脚本和 GUI..

How possible is this? See script and GUI below..

import schedule
import serial
import sys
from PyQt5 import uic, QtWidgets


qtCreatorFile = "gui.ui" 

Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)

estudiantes = ['   ','    ','     ','    '] 

ser = serial.Serial('COM9', baudrate=9600, timeout=1)

class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.Boton.clicked.connect(self.ingresos)
        #self.ingresos()

    def ingresos(self):

        dato = ser.readline().decode('ascii')

        #datos = (self.Box4.toPlainText())
        #dato = str(datos)
        estudiantes.insert(0,dato)
        estudiantes.pop()

        self.Box1.setText(estudiantes[0])
        self.Box2.setText(estudiantes[1])
        self.Box3.setText(estudiantes[2])
        self.Box4.setText(estudiantes[3])
        #self.Box1.setText("estudiantes")

# =============================================
        def job():
            # print("I'm working...")
            dato = ser.readline().decode('ascii')

            #datos = (self.Box4.toPlainText())
            #dato = str(datos)
            estudiantes.insert(0,dato)
            estudiantes.pop()

            self.Box1.setText(estudiantes[0])
            self.Box2.setText(estudiantes[1])
            self.Box3.setText(estudiantes[2])
            self.Box4.setText(estudiantes[3])
            #self.Box1.setText("estudiantes")

        # .......................    

        schedule.every(0.05).minutes.do(job)

        while True:
            schedule.run_pending()
            time.sleep(1)        

# .... Time definition ....
# 0.05 = 05 Seconds
# 0.50 = 50 Seconds
# 5.00 = 05 Minutes

# =============================================




if __name__ == "__main__":
    app =  QtWidgets.QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_()) 

PyQt5 GUI .ui 文件位于这里....

The PyQt5 GUI .ui file is HERE....

推荐答案

Qt 中的一个基本规则是 GUI 不应从主线程以外的线程更新,这称为 GUI 线程.有几种选择,例如通过信号将数据发送到主线程,或者使用 QRunnableQThreadPool 如下所示:

A basic rule in Qt is that the GUI should not be updated from a thread other than the main one, this is called the GUI thread. There are several options such as sending the data through signals to the main thread, or using a QRunnable with QThreadPool as shown below:

代码:

qtCreatorFile = "gui.ui" 
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
estudiantes = ['   ','    ','     ','    '] 

class SerialRunnable(QtCore.QRunnable):
    def __init__(self, w):
        QtCore.QRunnable.__init__(self)
        self.w = w
        self.ser = serial.Serial('COM9', baudrate=9600, timeout=1)

    def run(self):
        while True:
            dato = self.ser.readline().decode('ascii')
            if dato != "":
                QtCore.QMetaObject.invokeMethod(self.w, "setValues",
                                     QtCore.Qt.QueuedConnection,
                                     QtCore.Q_ARG(str, dato))
            QtCore.QThread.msleep(10)


class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.setupUi(self)
        runnable = SerialRunnable(self)
        QtCore.QThreadPool.globalInstance().start(runnable)

    @QtCore.pyqtSlot(str)
    def setValues(self, dato):
        estudiantes.insert(0,dato)
        estudiantes.pop()
        self.Box1.setText(estudiantes[0])
        self.Box2.setText(estudiantes[1])
        self.Box3.setText(estudiantes[2])
        self.Box4.setText(estudiantes[3])

这篇关于PyQt5 - 自动化串行模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆