PySerial无法在PyQt5 GUI上从Arduino读取数据

2024-03-28 16:32:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个GUI应用程序,以便使用PyQt5和PySerial向Arduino发送和接收数据。但是,它不能向Arduino写入任何数据。你知道吗

我已经尝试在Python控制台上导入库,并尝试发送/接收数据,但成功了。你知道吗

    def connectArduino(self):
        self.port = self.serialList[self.serialBox.currentIndex()]
        if len(self.port) < 2:
            errorWindow = QMessageBox()
            errorWindow.critical(self, "Error", "Arduino not selected.")
        else:
            ex.serial = SerialInterface(9600, self.port)
            ex.serial.serial.flush()
            ex.serial.write("id")
            time.sleep(5)
            teststr = ex.serial.read()
            print("Output: " + teststr)
            if teststr == "OK":
                self.serialButton.setText("Connected")
                self.serialButton.setDisabled(True)
                self.serialBox.setDisabled(True)
                self.serialButton.setStyleSheet("background-color: rgb(0, 255, 0);")
            else:
                errorWindow = QMessageBox()
                errorWindow.critical(self, "Error", "Selected serial port is not valid.")
                ex.serial.close()

编辑:我在Arduino上添加了一个LED,当它接收到数据时,我将其设置为闪烁。当从控制台触发时,LED闪烁,但是当从GUI调用函数时,没有区别。你知道吗


Tags: 数据selfifportserialguiarduinoex