Python 带有 GPIO.setup 和 GPIO.cleanup 的运行时警告不适用于 KeyboardInterrupt

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23684381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:19:04  来源:igfitidea点击:

RuntimeWarnings with GPIO.setup and GPIO.cleanup not work with KeyboardInterrupt

pythonraspberry-pikeyboardinterrupt

提问by Denis0189

I have a problem with my code working with raspberry pi. I just started with python so i need some help.

我的代码在使用 raspberry pi 时遇到问题。我刚开始使用 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. Use GPIO.setwarnings(False) to disable warnings. GPIO.setup(led1, GPIO.OUT) and:

RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings. GPIO.setup(led2, GPIO.OUT)

运行时警告:此通道已在使用中,无论如何都要继续。使用 GPIO.setwarnings(False) 禁用警告。GPIO.setup(led1, GPIO.OUT) 和:

运行时警告:此通道已在使用中,无论如何都要继续。使用 GPIO.setwarnings(False) 禁用警告。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 端口的所有引脚并关闭 LED。

but this in not happening in fact one of the led remain on.

但这实际上并没有发生,其中一个 LED 仍然亮着。

How can change my code to resolve this issue?

如何更改我的代码以解决此问题?

采纳答案by Peter Varo

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/

回答by John La Rooy

You don't seem to have included mainin your question. However the problem may occur if the programs exits for some reason other than KeyboardInterrupt. It's better to free the resource in a finallyblock

您似乎没有包含main在您的问题中。但是,如果程序由于某些原因而不是KeyboardInterrupt. 最好在一个finally块中释放资源

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

回答by user5890381

You are calling main()function but it's not declared (defined), you are using while(blink). So You need to delete the "main()" and put the "Try" before your main function which is the while(blink)loop. Don't forget the proper tabs there.

您正在调用main()函数,但未声明(定义),您正在使用while(blink). 因此,您需要删除“main()”并将“Try”放在作为while(blink)循环的主函数之前。不要忘记那里的正确标签。