0

关键是只有在有要阅读的内容时才具有阅读功能,而不是使用没有特殊方法的 pyserial 。我想这可能会涉及一个更大的问题,即信号和插槽是否可以在没有 GUI 类(从其他对象继承)的情况下使用。我可以让串口写入,但不能读取,

from PyQt5 import QtCore, QtSerialPort
serial_port = QtSerialPort.QSerialPort('COM3')
serial_port.open(QtCore.QIODevice.ReadWrite)
serial_port.write(bytes([255]))

def handle_ready_read():
    while serial_port.canReadLine():
        print(serial_port.readAll())
        print('here')
        serial_port.close()

serial_port.readyRead.connect(handle_ready_read)

即使在使用 pyserial 时读取了某些内容,也没有打印出来。

4

1 回答 1

0

你不需要 GUI 来使用 Qt。Qt 中有一个专用的GUI 模块,不依赖于它的模块也不需要 GUI。

但是,要使用槽和信号,您需要运行 Qt 的事件循环。常规方法是使用QCoreApplication.

app = QCoreApplication([])

# setup your serial port here

sys.exit(app.exec_())
于 2021-03-10T17:10:54.703 回答