Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python检测和比较Beaglebone中的GPIO事件_Python_Multithreading_Beagleboneblack - Fatal编程技术网

使用Python检测和比较Beaglebone中的GPIO事件

使用Python检测和比较Beaglebone中的GPIO事件,python,multithreading,beagleboneblack,Python,Multithreading,Beagleboneblack,我试图在Beaglebone Black中的两个不同GPIO中检测两个事件,然后确定哪一个先发生。我使用Adafrubbio.GPIO来编写用Python编写的代码。它不能正常工作,也不知道为什么。代码如下: import sys import thread import time from datetime import datetime import bitarray import Adafruit_BBIO.GPIO as GPIO gpio_state = [0, 0] gpio_ti

我试图在Beaglebone Black中的两个不同GPIO中检测两个事件,然后确定哪一个先发生。我使用Adafrubbio.GPIO来编写用Python编写的代码。它不能正常工作,也不知道为什么。代码如下:

import sys
import thread
import time
from datetime import datetime
import bitarray
import Adafruit_BBIO.GPIO as GPIO

gpio_state = [0, 0]
gpio_time = [0, 0]
ir_recv = ['GPIO0_26', 'GPIO1_12']

def checkEvent(index):
    while True:
        if GPIO.event_detected(ir_recv[index]): 
            if (gpio_state[index] == 0):
                gpio_state[index] = 1
                gpio_time[index] = datetime.now()
                print ir_recv[index]
                time.sleep(5) # time to avoid rebounces

for gpio in ir_recv:
    GPIO.setup(gpio, GPIO.IN)
    GPIO.add_event_detect(gpio, GPIO.RISING)

try:
    thread.start_new_thread(checkEvent, (0, ) )
    thread.start_new_thread(checkEvent, (1, ) )
except:
    print "Error: unable to start thread"

while True:
    if (gpio_state[0] == 1) and (gpio_state[1] == 1):
        if gpio_time[0] > gpio_time[1]:
            print "1"
        if gpio_time[0] < gpio_time[1]:
            print "2"
        if gpio_time[0] == gpio_time[1]:
            print "???"
        gpio_state[0] = 0
        gpio_state[1] = 0
        gpio_time[0] = 0
        gpio_time[1] = 0
导入系统 导入线程 导入时间 从日期时间导入日期时间 导入位数组 将Adafruitbbio.GPIO导入为GPIO gpio_状态=[0,0] gpio_时间=[0,0] ir_recv=['GPIO0_26','GPIO1_12'] def checkEvent(索引): 尽管如此: 如果检测到GPIO.event(红外接收[索引]): 如果(gpio_状态[索引]==0): gpio_状态[索引]=1 gpio_时间[index]=datetime.now() 打印ir_recv[索引] 时间。睡觉(5)#是时候避免再次约会了 对于ir_recv中的gpio: GPIO.setup(GPIO,GPIO.IN) GPIO.add\u事件检测(GPIO,GPIO.RISING) 尝试: 线程。启动新线程(检查事件,(0,)) 线程。启动新线程(checkEvent,(1,)) 除: 打印“错误:无法启动线程” 尽管如此: 如果(gpio_状态[0]==1)和(gpio_状态[1]==1): 如果gpio_时间[0]>gpio_时间[1]: 打印“1” 如果gpio_时间[0] 我没有得到任何错误。主要问题是没有正确比较事件,例如,尽管GPIO0_26中的事件比GPIO1_12中的事件先发生(即gpio_时间[0]小于gpio_时间[1]),但最后一个While循环中的输出不会打印出“2”。有时,代码会从线程打印两次GPIO引脚

提前感谢您提出的任何解决方案建议。

我建议您为此使用(当然,我是作者)。它有一个基于的中断API(用于内核级中断信令),这将大大简化这一过程。像这样的东西应该可以做到(我还没有测试过):

您应该确保您的PyBBIO是最新的:

# pip install -U PyBBIO

你有什么错误吗。或未预测预期结果,如果是,请提供输入和预期结果output@sundarnatarajССааа:添加了我遇到的问题,如果没有GPIO数据,则两个线程都处于快速忙等待循环中。考虑添加<代码>时间。睡眠(0.01),或者找到在代码> GPIO.EngEnTebug()/代码>上阻塞的方法,考虑使用内核创建(时间戳)GPIO输入事件的能力。看见
# pip install -U PyBBIO