Connect to serial from a PyQt GUI

前端 未结 1 717
悲哀的现实
悲哀的现实 2020-11-29 11:27

I write a program to that send and recive data from serial, but I have a problem, I want to create a function \"connect()\" or a class, and when I press a button, the functi

相关标签:
1条回答
  • 2020-11-29 12:24

    Instead of using pySerial + thread it is better to use QSerialPort that is made to live with the Qt event-loop:

    from PyQt5 import QtCore, QtWidgets, QtSerialPort
    
    class Widget(QtWidgets.QWidget):
        def __init__(self, parent=None):
            super(Widget, self).__init__(parent)
            self.message_le = QtWidgets.QLineEdit()
            self.send_btn = QtWidgets.QPushButton(
                text="Send",
                clicked=self.send
            )
            self.output_te = QtWidgets.QTextEdit(readOnly=True)
            self.button = QtWidgets.QPushButton(
                text="Connect", 
                checkable=True,
                toggled=self.on_toggled
            )
            lay = QtWidgets.QVBoxLayout(self)
            hlay = QtWidgets.QHBoxLayout()
            hlay.addWidget(self.message_le)
            hlay.addWidget(self.send_btn)
            lay.addLayout(hlay)
            lay.addWidget(self.output_te)
            lay.addWidget(self.button)
    
            self.serial = QtSerialPort.QSerialPort(
                '/dev/tty.usbmodem14201',
                baudRate=QtSerialPort.QSerialPort.Baud9600,
                readyRead=self.receive
            )
    
        @QtCore.pyqtSlot()
        def receive(self):
            while self.serial.canReadLine():
                text = self.serial.readLine().data().decode()
                text = text.rstrip('\r\n')
                self.output_te.append(text)
    
        @QtCore.pyqtSlot()
        def send(self):
            self.serial.write(self.message_le.text().encode())
    
        @QtCore.pyqtSlot(bool)
        def on_toggled(self, checked):
            self.button.setText("Disconnect" if checked else "Connect")
            if checked:
                if not self.serial.isOpen():
                    if not self.serial.open(QtCore.QIODevice.ReadWrite):
                        self.button.setChecked(False)
            else:
                self.serial.close()
    
    if __name__ == '__main__':
        import sys
        app = QtWidgets.QApplication(sys.argv)
        w = Widget()
        w.show()
        sys.exit(app.exec_())
    
    0 讨论(0)
提交回复
热议问题