TraceBack(最后一次调用),以及 GPIO.setmode(GPIO.BOARD) 或 GPIO.setmode(GPIO.BCM) 错误
TraceBack (most recent call last), and GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM) errors
我一直在努力编写一个脚本,该脚本将 Pi 的 BCM 引脚 17、27 和 10 打开 1 秒,然后关闭 1 秒,连续 100 次。当我 运行 时,脚本 LED1 将打开然后关闭,程序关闭并出现此错误:
Traceback (most recent call last):
File "LedBlink.py", line 47, in <module>
LED2Blink()
File "LedBlink.py", line 27, in LED2Blink
GPIO.setup(LED2, GPIO.OUT)
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)
import RPi.GPIO as GPIO
import time
LED1 = 17
LED2 = 27
LED3 = 10
GPIO.setmode(GPIO.BCM)
def LED1Blink():
GPIO.setup(LED1, GPIO.OUT)
GPIO.output(LED1,True)
time.sleep(1)
GPIO.output(LED1,False)
time.sleep(1)
GPIO.cleanup()
def LED2Blink():
GPIO.setup(LED2, GPIO.OUT)
GPIO.output(LED2,True)
time.sleep(1)
GPIO.output(LED2,False)
time.sleep(1)
GPIO.cleanup()
def LED3Blink():
GPIO.setup(LED3, GPIO.OUT)
GPIO.output(LED3,True)
time.sleep(1)
GPIO.output(LED3,False)
time.sleep(1)
GPIO.cleanup()
i = 0
while i < 100:
LED1Blink()
LED2Blink()
LED3Blink()
i + 1
else:
print "finished loop"
问题是您在每个方法的末尾调用 GPIO.cleanup()
。如 documentation 中所述,Note that GPIO.cleanup() also clears the pin numbering system in use.
您想要的是 GPIO.cleanup(channel)
,而通道对应于脚本中的 LED1, LED2, LED3
。
最佳做法是设置和清理频道仅一次,例如
import RPi.GPIO as GPIO
import time
LED1 = 17
LED2 = 27
LED3 = 10
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED1, GPIO.OUT)
GPIO.setup(LED2, GPIO.OUT)
GPIO.setup(LED3, GPIO.OUT)
def LED1Blink():
GPIO.output(LED1,True)
time.sleep(1)
GPIO.output(LED1,False)
time.sleep(1)
def LED2Blink():
GPIO.output(LED2,True)
time.sleep(1)
GPIO.output(LED2,False)
time.sleep(1)
def LED3Blink():
GPIO.output(LED3,True)
time.sleep(1)
GPIO.output(LED3,False)
time.sleep(1)
i = 0
if i < 100:
LED1Blink()
LED2Blink()
LED3Blink()
i + 1
else:
GPIO.cleanup()
print "finished loop"
切勿多次调用 GPIO.cleanup(),因为在清除 PINS 的同时,它还会清除 Pin MODE!因此,如果您在程序之间调用它,那么下一个语句执行将没有 pin MODE,并且会发出错误。
"TraceBack (most recent call last), and GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM) errors"
因此,如果满足特定条件,请始终在末尾或程序可以 end/break 之间的任何地方使用它。
我一直在努力编写一个脚本,该脚本将 Pi 的 BCM 引脚 17、27 和 10 打开 1 秒,然后关闭 1 秒,连续 100 次。当我 运行 时,脚本 LED1 将打开然后关闭,程序关闭并出现此错误:
Traceback (most recent call last):
File "LedBlink.py", line 47, in <module>
LED2Blink()
File "LedBlink.py", line 27, in LED2Blink
GPIO.setup(LED2, GPIO.OUT)
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)
import RPi.GPIO as GPIO
import time
LED1 = 17
LED2 = 27
LED3 = 10
GPIO.setmode(GPIO.BCM)
def LED1Blink():
GPIO.setup(LED1, GPIO.OUT)
GPIO.output(LED1,True)
time.sleep(1)
GPIO.output(LED1,False)
time.sleep(1)
GPIO.cleanup()
def LED2Blink():
GPIO.setup(LED2, GPIO.OUT)
GPIO.output(LED2,True)
time.sleep(1)
GPIO.output(LED2,False)
time.sleep(1)
GPIO.cleanup()
def LED3Blink():
GPIO.setup(LED3, GPIO.OUT)
GPIO.output(LED3,True)
time.sleep(1)
GPIO.output(LED3,False)
time.sleep(1)
GPIO.cleanup()
i = 0
while i < 100:
LED1Blink()
LED2Blink()
LED3Blink()
i + 1
else:
print "finished loop"
问题是您在每个方法的末尾调用 GPIO.cleanup()
。如 documentation 中所述,Note that GPIO.cleanup() also clears the pin numbering system in use.
您想要的是 GPIO.cleanup(channel)
,而通道对应于脚本中的 LED1, LED2, LED3
。
最佳做法是设置和清理频道仅一次,例如
import RPi.GPIO as GPIO
import time
LED1 = 17
LED2 = 27
LED3 = 10
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED1, GPIO.OUT)
GPIO.setup(LED2, GPIO.OUT)
GPIO.setup(LED3, GPIO.OUT)
def LED1Blink():
GPIO.output(LED1,True)
time.sleep(1)
GPIO.output(LED1,False)
time.sleep(1)
def LED2Blink():
GPIO.output(LED2,True)
time.sleep(1)
GPIO.output(LED2,False)
time.sleep(1)
def LED3Blink():
GPIO.output(LED3,True)
time.sleep(1)
GPIO.output(LED3,False)
time.sleep(1)
i = 0
if i < 100:
LED1Blink()
LED2Blink()
LED3Blink()
i + 1
else:
GPIO.cleanup()
print "finished loop"
切勿多次调用 GPIO.cleanup(),因为在清除 PINS 的同时,它还会清除 Pin MODE!因此,如果您在程序之间调用它,那么下一个语句执行将没有 pin MODE,并且会发出错误。 "TraceBack (most recent call last), and GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM) errors"
因此,如果满足特定条件,请始终在末尾或程序可以 end/break 之间的任何地方使用它。