我正在尝试创建一个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上我将其设置为在接收数据时闪烁。从控制台触发时,LED闪烁,但是从GUI调用该功能时,没有区别。