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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/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 线程类来控制Raspberry Pi GPIO_Python_Multithreading_Raspberry Pi_Gpio - Fatal编程技术网

Python 线程类来控制Raspberry Pi GPIO

Python 线程类来控制Raspberry Pi GPIO,python,multithreading,raspberry-pi,gpio,Python,Multithreading,Raspberry Pi,Gpio,我用我的树莓Pi控制一个继电器,作为我正在建造的浇水系统的一部分。我终于有了线程类设置,可以基于一个简单的SQL查询创建多个线程。以下代码用于迭代记录并创建每个线程(传递GPIO引脚号和保持继电器打开的延迟时间): 下面是我创建的用于处理每个线程的类(整个类用于为每个GPIO引脚创建线程): 问题是我得到了一个运行时错误:当我已经将GPIO引脚的输出设置为true时,GPIO通道还没有设置为输出错误。我甚至在类的末尾执行GPIO.cleanup(),以确保延迟结束后不会引起任何问题。它似乎并不总

我用我的树莓Pi控制一个继电器,作为我正在建造的浇水系统的一部分。我终于有了线程类设置,可以基于一个简单的SQL查询创建多个线程。以下代码用于迭代记录并创建每个线程(传递GPIO引脚号和保持继电器打开的延迟时间):

下面是我创建的用于处理每个线程的类(整个类用于为每个GPIO引脚创建线程):

问题是我得到了一个
运行时错误:当我已经将GPIO引脚的输出设置为true时,GPIO通道还没有设置为输出
错误。我甚至在类的末尾执行
GPIO.cleanup()
,以确保延迟结束后不会引起任何问题。它似乎并不总是为我设置的每个GPIO引脚都这样做,但它总是至少为2

我认为这可能是我创建类实例的方式的一个问题,尽管当我删除GPIO代码时,它可以正常工作(例如,根据需要使用多个线程进行睡眠)

这里有什么简单的东西我遗漏了吗?如果有,请提前道歉!我对这件事还不太熟悉


干杯

您有一个输入错误
GPIO.output(deviceGPIO,GPIO.HIGH)
->
GPIO.output(self.deviceGPIO,GPIO.HIGH)
谢谢您指出了这个类型。。。不幸的是,基本上相同的问题仍在发生。我得到一个
GPIO.output(self.deviceGPIO,GPIO.LOW)运行时错误:每次运行脚本时,GPIO通道都没有设置为输出
错误。。。我的代码还有其他明显的问题吗?经过进一步的尝试和错误,我发现
GPIO.cleanup()
命令正在将GPIO引脚重置为输入。我已经删除了,我的脚本现在似乎工作正常。至少GPIO引脚现在没有显示“无输出错误”。。。
with con:

con.row_factory = lite.Row       
cur = con.cursor() 
cur.execute(joinProgramZoneQuery)
rows = cur.fetchall()        

for row in rows: 

    duration = int(row["programDuration"]) 
    deviceGPIO = int(row["deviceGPIO"])         

    t = MyThreadWithArgs(deviceGPIO=deviceGPIO, duration=duration)
    t.start()
# use P1 header pin numbering convention
GPIO.setmode(GPIO.BOARD)

class MyThreadWithArgs(threading.Thread):
    def __init__(self, deviceGPIO="default value", duration="default value", **kwargs):
        threading.Thread.__init__(self, **kwargs)

        self.deviceGPIO = deviceGPIO
        self.duration = duration       

    def run(self):

        logging.debug('Current GPIO pin: %s', self.deviceGPIO)       

        #SETUP THE PIN FOR OUTPUT AND DEFAULT TO LOW OUTPUT
        GPIO.setup(self.deviceGPIO, GPIO.OUT, initial=GPIO.LOW)
        #output the GPIO pin to HIGH                
        GPIO.output(deviceGPIO, GPIO.HIGH) 
        #Pause for specified duration        
        time.sleep(self.duration) 
        #SET THE GPIO OUTPUT BACK TO LOW AGAIN AFTER THE SLEEP
        GPIO.output(self.deviceGPIO, GPIO.LOW)
        #clean up all GPIO pins after the script has run..
        GPIO.cleanup()            

        logging.debug('ending')