是否可以在没有 GUI 或类的情况下使用 PyQt5 QtSerialPort? [英] Is it possible to use PyQt5 QtSerialPort without GUI's or classes?

查看:77
本文介绍了是否可以在没有 GUI 或类的情况下使用 PyQt5 QtSerialPort?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The point would be to have the functionality of reading only when there is something to be read, instead of using pyserial which doesn't have a special method for that. I guess this may go into a bigger question of whether signals and slots could be used without a GUI classes (that inherit from other objects). I could get the serial port to write, but not read, with

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 时读取了某些内容,也不会打印出任何内容.

Nothing prints out, even though something is read when using pyserial.

推荐答案

您不需要 GUI 即可使用 Qt.Qt 中有一个专用的 GUI 模块,什么不依赖它,不需要 GUI.

You do not need GUI to use Qt. There is a dedicated GUI module in Qt and what does not depend on it, doesn't need a GUI.

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

However, to use slots and signals you need to run Qt's event loop. The regular way would be to use a QCoreApplication.

app = QCoreApplication([])

# setup your serial port here

sys.exit(app.exec_())

这篇关于是否可以在没有 GUI 或类的情况下使用 PyQt5 QtSerialPort?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆