简体   繁体   English

使用GPIO.setup和GPIO.cleanup的RuntimeWarning不能与KeyboardInterrupt一起使用

[英]RuntimeWarnings with GPIO.setup and GPIO.cleanup not work with KeyboardInterrupt

I have a problem with my code working with raspberry pi. 我的代码使用raspberry pi时遇到问题。 I just started with python so i need some help. 我刚开始使用python,所以我需要一些帮助。

This is the code: 这是代码:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

led1=22
led2=17

GPIO.setup(led1, GPIO.OUT)
GPIO.setup(led2, GPIO.OUT)

def blink():
    GPIO.output(led1, 1)
    time.sleep(1)
    GPIO.output(led1, 0)

    GPIO.output(led2, 1)
    time.sleep(1)
    GPIO.output(led2, 0)

while(blink):
    blink()

try:
    main()
except KeyboardInterrupt:
    GPIO.cleanup()

when I run this error appear in the console: 当我运行此错误时出现在控制台中:

RuntimeWarning: This channel is already in use, continuing anyway. RuntimeWarning:此频道已在使用中,无论如何都在继续。 Use GPIO.setwarnings(False) to disable warnings. 使用GPIO.setwarnings(False)禁用警告。 GPIO.setup(led1, GPIO.OUT) and: GPIO.setup(led1,GPIO.OUT)和:

RuntimeWarning: This channel is already in use, continuing anyway. RuntimeWarning:此频道已在使用中,无论如何都在继续。 Use GPIO.setwarnings(False) to disable warnings. 使用GPIO.setwarnings(False)禁用警告。 GPIO.setup(led2, GPIO.OUT) GPIO.setup(led2,GPIO.OUT)

If I understand correctly the command GPIO.cleanup() should reset all pin of GPIO port and turn off the led. 如果我理解正确, GPIO.cleanup()命令应该重置GPIO端口的所有引脚并关闭GPIO.cleanup()

but this in not happening in fact one of the led remain on. 但事实并非如此,其中一位领导仍然存在。

How can change my code to resolve this issue? 如何更改我的代码来解决此问题?

Here is a little help, how to effectively separate your functions, and make them more general. 这里有一点帮助,如何有效地分离你的功能,并使它们更通用。 Although this is a working Python script I provided, I didn't tested it on my raspi, but I think it will work -- anyway, let me know if there were any problems! 虽然这是我提供的一个有效的Python脚本,但我没有在我的raspi上测试它,但我认为它会起作用 - 无论如何,让我知道是否有任何问题!

import RPi.GPIO as GPIO
import time

# Module level constants
LED1 = 22
LED2 = 17

# Sets up pins as outputs
def setup(*leds):
    GPIO.cleanup()
    GPIO.setmode(GPIO.BCM)
    for led in leds:
        GPIO.setup(led, GPIO.OUT)
        GPIO.output(led, GPIO.LOW)

# Turn on and off the leds
def blink(*leds):
    # Blink all leds passed
    for led in leds:
        GPIO.output(led, GPIO.HIGH)
        time.sleep(1)
        GPIO.output(led, GPIO.LOW)

if __name__ == '__main__':
    # Setup leds
    setup(LED1, LED2)
    # Run blinking forever
    try:
        while True:
            blink(LED1, LED2)
    # Stop on Ctrl+C and clean up
    except KeyboardInterrupt:
        GPIO.cleanup()

A friendly recommendation: 友好的推荐:

There is a dedicated Raspberry Pi StackExchange site too: https://raspberrypi.stackexchange.com/ 还有一个专用的Raspberry Pi StackExchange站点: https//raspberrypi.stackexchange.com/

You don't seem to have included main in your question. 您似乎没有在您的问题中包含main However the problem may occur if the programs exits for some reason other than KeyboardInterrupt . 但是,如果程序由于KeyboardInterrupt以外的某些原因退出,则可能会出现此问题。 It's better to free the resource in a finally block 最好在finally块中释放资源

try:
    main()
except KeyboardInterrupt:
    pass
finally:
    GPIO.cleanup()

You are calling main() function but it's not declared (defined), you are using while(blink) . 您正在调用main()函数,但它没有声明(已定义),您正在使用while(blink) So You need to delete the "main()" and put the "Try" before your main function which is the while(blink) loop. 所以你需要删除“main()”并在主函数(即while(blink)循环while(blink)之前放置“Try”。 Don't forget the proper tabs there. 不要忘记那里的正确标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM