PiZero W连接到两个外围设备(GPIO和USB):如何同时连续从两个外围设备读取数据?

PiZero W连接到两个外围设备(GPIO和USB):如何同时连续从两个外围设备读取数据?,第1张

PiZero W连接到两个外围设备(GPIO和USB):如何同时连续从两个外围设备读取数据?

更新的答案

我为条形码阅读器添加了一些代码。我这样做是为了使条形码读取器花费可变的时间,最多花费5秒来读取数据,而流量计花费恒定的0.5秒,因此您可以看到不同的线程以彼此独立的速率进行进程。

#!/usr/bin/env python3from threading import Lockimport threadingimport timefrom random import seedfrom random import random# Dummy function to read SPI as I don't have anything attacheddef readSPI():    # Take 0.5s to read    time.sleep(0.5)    readSPI.static += 1    return readSPI.staticreadSPI.static=0class FlowMeter(threading.Thread):    def __init__(self):        super(FlowMeter, self).__init__()        # Create a mutex        self.mutex = Lock()        self.currentReading = 0    def run(self):        # Continuously read flowmeter and safely update self.currentReading        while True: value = readSPI() self.mutex.acquire() self.currentReading = value self.mutex.release()    def read(self):        # Main calls this to get latest reading, we just grab it from internal variable        self.mutex.acquire()        value = self.currentReading        self.mutex.release()        return value# Dummy function to read Barpre as I don't have anything attacheddef readBarpre():    # Take variable time, 0..5 seconds, to read    time.sleep(random()*5)    result = "BC" + str(int(random()*1000))    return resultclass BarpreReader(threading.Thread):    def __init__(self):        super(BarpreReader, self).__init__()        # Create a mutex        self.mutex = Lock()        self.currentReading = 0    def run(self):        # Continuously read barpre and safely update self.currentReading        while True: value = readBarpre() self.mutex.acquire() self.currentReading = value self.mutex.release()    def read(self):        # Main calls this to get latest reading, we just grab it from internal variable        self.mutex.acquire()        value = self.currentReading        self.mutex.release()        return valueif __name__ == '__main__':    # Generate repeatable random numbers    seed(42)    # Instantiate and start flow meter manager thread    fmThread = FlowMeter()    fmThread.daemon = True    fmThread.start()    # Instantiate and start barpre reader thread    bcThread = BarpreReader()    bcThread.daemon = True    bcThread.start()    # Now you can do other things in main, but always get access to latest readings    for i in range(20):        fmReading = fmThread.read()        bcReading = bcThread.read()        print(f"Main: i = {i} FlowMeter reading = {fmReading}, Barpre={bcReading}")        time.sleep(1)

样本输出

Main: i = 0 FlowMeter reading = 0, Barpre=0Main: i = 1 FlowMeter reading = 1, Barpre=0Main: i = 2 FlowMeter reading = 3, Barpre=0Main: i = 3 FlowMeter reading = 5, Barpre=0Main: i = 4 FlowMeter reading = 7, Barpre=BC25Main: i = 5 FlowMeter reading = 9, Barpre=BC223Main: i = 6 FlowMeter reading = 11, Barpre=BC223Main: i = 7 FlowMeter reading = 13, Barpre=BC223Main: i = 8 FlowMeter reading = 15, Barpre=BC223Main: i = 9 FlowMeter reading = 17, Barpre=BC676Main: i = 10 FlowMeter reading = 19, Barpre=BC676Main: i = 11 FlowMeter reading = 21, Barpre=BC676Main: i = 12 FlowMeter reading = 23, Barpre=BC676Main: i = 13 FlowMeter reading = 25, Barpre=BC86Main: i = 14 FlowMeter reading = 27, Barpre=BC86Main: i = 15 FlowMeter reading = 29, Barpre=BC29Main: i = 16 FlowMeter reading = 31, Barpre=BC505Main: i = 17 FlowMeter reading = 33, Barpre=BC198Main: i = 18 FlowMeter reading = 35, Barpre=BC198Main: i = 19 FlowMeter reading = 37, Barpre=BC198

原始答案

我建议您研究一下

systemd
systemctl
在每次系统启动时启动您的应用程序-这里的示例。

至于同时监视两件事,我建议您使用Python的 线程
模块。这是一个简单的示例,我创建了一个子类

threading
,通过不断读取该对象并将其当前值保存在主程序可以随时读取的变量中来管理您的流量计。您可以启动另一个类似的程序来管理您的条形码读取器,然后并行运行它们。我不想这样做,并且会使您的代码倍增。

#!/usr/bin/env python3from threading import Lockimport threadingimport time# Dummy function to read SPI as I don't have anything attacheddef readSPI():    readSPI.static += 1    return readSPI.staticreadSPI.static=0class FlowMeter(threading.Thread):    def __init__(self):        super(FlowMeter, self).__init__()        # Create a mutex        self.mutex = Lock()        self.currentReading = 0    def run(self):        # Continuously read flowmeter and safely update self.currentReading        while True: value = readSPI() self.mutex.acquire() self.currentReading = value self.mutex.release() time.sleep(0.01)    def read(self):        # Main calls this to get latest reading, we just grab it from internal variable        self.mutex.acquire()        value = self.currentReading        self.mutex.release()        return valueif __name__ == '__main__':    # Instantiate and start flow meter manager thread    fmThread = FlowMeter()    fmThread.start()    # Now you can do other things in main, but always get access to latest reading    for i in range(100000):        fmReading = fmThread.read()        print(f"Main: i = {i} FlowMeter reading = {fmReading}")        time.sleep(1)

您可以考虑使用

logging
来协调和统一调试和日志记录消息-参见此处。

您可以看一下

events
让其他线程知道某些事情达到临界水平时需要做的事情-
例如此处。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5649499.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存