如何将串行数据读取到我的Python Gui App

时间:2019-05-03 06:32:33

标签: python multithreading serial-port pyqt5

我尝试将串行数据读取到我的GUI应用程序,当我读取数据以控制台它正确运行时,我使用qt设计器,但是当我制作一个gui应用程序来处理gui应用程序冻结的事件时

这是我的代码:

SERIAL_RATE = 9600


class AppWindow(QDialog):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.show()
        self.scan_port()
        self.timeout = 0
        self.stopevent=threading.Event()
        self.make_signal_slot()

def make_signal_slot(self):
    self.ui.btnConnect.clicked.connect(self.readSerial)

def scan_port(self):
    comlist = serial.tools.list_ports.comports()
    _port = []
    for element in comlist:
        _port.append(element.device)
    self.ui.cmbPort.addItems(_port)
    return _port

def connect(self):
    self.serialthread = threading.Thread(group=None,    target=self.readSerial)
    self.stopevent.clear()
    self.serialthread.start()

@pyqtSlot()
def readSerial(self):
    SERIAL_PORT = self.ui.cmbPort.currentText()
    ser = serial.Serial(SERIAL_PORT, SERIAL_RATE)
    while ser.isOpen():
          s = ser.read(14)
          time.sleep(1)
          hex_string = binascii.hexlify(s).decode('utf-8')
          valx = float(hex_string[10:14]) / 100
          self.ui.txtRaw.setPlainText(str(hex_string))
          self.ui.lbDepth.setText(str('%05.2F' %valx))

    time.sleep(1)
    self.close()

这是我的Gui应用

enter image description here

0 个答案:

没有答案