定期轮询 Raspberry Pi 上的端口或硬件 IO 点 [英] periodic polling of port or hardware IO point on Raspberry Pi

查看:43
本文介绍了定期轮询 Raspberry Pi 上的端口或硬件 IO 点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 Qt5 和 Python 开发应用程序时,您通常是事件驱动的.没有汗水,工作就像一个魅力.但是,在某些情况下,您需要轮询某些硬件 GPIO(即按下按钮)的状态,或从串行端口或 gpsd 守护程序等获取一些信息.

In developing an application using Qt5 with Python, you are generally event driven. No sweat, works like a charm. However, there are instances when you need to poll the status of some hardware GPIO (i.e. a button push), or get some information from a serial port, or something like a gpsd daemon.

处理这个问题的首选方法是什么?通过 QTimer,比如说,每 50 毫秒运行一次?还是有其他我没有找到的方法?在 GPIO pi 上设置触发器是否更好(https://www.ics.com/blog/control-raspberry-pi-gpio-pins-python) 还是与 Qt5 Gui 有冲突?

What is the preferred way to handle this? Via a QTimer, say, running every 50 msec? Or is there some other method I haven't found? Is it better to set up a trigger on a GPIO pi (https://www.ics.com/blog/control-raspberry-pi-gpio-pins-python) or is there any conflict with the Qt5 Gui?

基础文档看起来并不可怕,我当然可以参考一些示例,但不知道是否有更好/更规范/更 Pythonic 的方法.

Basic documentation doesn't look horrible, and I can follow some examples, of course, but didn't know if there was a better/canonical/more Pythonic method.

https://doc.qt.io/qtforpython/PySide2/QtCore/QTimer.html

https://python-catalin.blogspot.com/2019/08/python-qt5-qtimer-class.html

推荐答案

我不认为有 pythonic 解决方案,不是因为你不能使用 python,而是因为 python 与话题.而且也没有规范的解决方案,一切都取决于应用程序.

I don't think there is a pythonic solution, not because you can't use python but because python is not relevant to the topic. And there is no canonical solution either, everything will depend on the application.

根据我的经验,我发现重用处理 Rpi.GPIO 或 gpiozero 等 GPIO 的库要容易得多.这些库具有创建线程的策略,在其中监视引脚的状态,因此您不能直接使用回调来更新 GUI,但您必须实现包装器(请参阅 这个例如).

From my experience I have found it much easier to reuse libraries that handle GPIOs like Rpi.GPIO or gpiozero. These libraries have as a strategy to create threads where the state of the pins is monitored, so you cannot use the callbacks directly to update the GUI but you must implement wrapper(see this for example).

简单的例子:

import sys

from gpiozero import Button

from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QUrl
from PyQt5.QtWidgets import QMainWindow, QApplication


class ButtonManager(QObject):
    pressed = pyqtSignal()

    def __init__(self, parent=None):
        super(ButtonManager, self).__init__(parent)
        self._button = Button(20)
        self._button.when_pressed = self._on_when_pressed

    def _on_when_pressed(self):
        self.pressed.emit()


class MainWindow(QMainWindow):
    @pyqtSlot()
    def on_pressed(self):
        print("pressed")


def main():

    app = QApplication(sys.argv)
    w = MainWindow()

    button_manager = ButtonManager()
    button_manager.pressed.connect(w.on_pressed)

    w.show()

    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

如果您打算使用串行端口,那么最好的选择是使用 Qt 串行端口,因为与 pyserial 不同,它不需要使用线程,但通知是通过信号(参见 这个例如)

If you are going to use serial ports then the best option is to use Qt Serial Port since unlike pyserial it is not necessary to use threads but the notification is through signals(See this for example)

使用 QTimer 是另一种选择.

Using a QTimer is another option.

这篇关于定期轮询 Raspberry Pi 上的端口或硬件 IO 点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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