我正在使用PyQT5编写简单的应用程序以从串行端口读取数据,但是从串行端口接收数据时不会发出readyRead
。
如果应用程序被waitForReadyRead()
阻止,则在收到数据时会调用dataReady()
。
在Windows 10中使用Python 3.7.4,PyQt5 5.13.1
再现问题的最小代码:
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget
from PyQt5.QtWidgets import QMainWindow, QLabel
from PyQt5.QtCore import Qt, QIODevice
from PyQt5.QtSerialPort import QSerialPort
import sys
class ExampleGUI(QMainWindow):
def __init__(self):
super().__init__()
#self.setGeometry(50,50,500,300)
self.setWindowTitle("Example")
# Start mainLayout
self.mainLayout = QVBoxLayout()
serialLabel = QLabel("Example program")
self.mainLayout.addWidget(serialLabel)
widget = QWidget()
widget.setLayout(self.mainLayout)
self.setCentralWidget(widget)
self.serPort = QSerialPort()
self.serPort.readyRead.connect(self.dataReady)
self.serPort.setPortName("COM4")
self.serPort.setBaudRate(9600)
self.serPort.open(QIODevice.ReadWrite)
self.serPort.writeData("Hi".encode())
# self.serPort.waitForReadyRead()
def dataReady(self):
print(bytes(self.serPort.readAll()))
if __name__ == '__main__':
app = QApplication([])
gui = ExampleGUI()
gui.show()
app.exec_()