【问题标题】:Python Raspberry Pi GPIO errorPython 树莓派 GPIO 错误
【发布时间】:2013-12-09 06:38:05
【问题描述】:

我在我的 Raspberry Pi 上运行以下 python 脚本:

http://www.skpang.co.uk/dl/rfid.py

我在最后修改了脚本以访问 GPIO 引脚 15 并将其打开和关闭。这是我在底部的代码:

def example():

rfid = SL030()
fw = rfid.get_firmware()
print("RFID reader firmware:" + fw)
print()

GPIO.setmode(GPIO.BOARD)
GPIO.setup(15, GPIO.OUT)
GPIO.output(15,True)


while True:
    rfid.wait_tag()
    print("card present")

    if rfid.select_mifare():
        type = rfid.get_type()
        print("type:" + rfid.get_typename(type))

        id = rfid.get_uidstr()
        try:
            user = cards[id]
            print(user)
            #os.system("aplay " + user)
        except KeyError:
            print("Unknown card:" + id)

    rfid.wait_notag()
    print("card removed")
    print()

我面临的问题是,虽然它运行 15 号引脚,但脚本停止并出现以下错误:

Traceback (most recent call last):
  File "./rfid.py", line 212, in <module>
    example()
  File "./rfid.py", line 182, in example
rfid.wait_tag()
  File "./rfid.py", line 45, in wait_tag
while not self.tag_present():
  File "./rfid.py", line 40, in tag_present
    return GPIO.input(CFG_TAG_DETECT) == False
    RPi.GPIO.InvalidChannelException: The channel sent is invalid on a Raspberry Pi

有什么想法可能是错的吗?

谢谢

更新

如果我将 GPIO 代码放在 def example(): 下方和 rfid = SL030() 上方,如下所示,那么它似乎可以正常工作:

def example():

    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(15, GPIO.OUT)
    GPIO.output(15,True)

    rfid = SL030()

*更新 - 解决方案*

感谢安德烈,我改变了:

GPIO.setmode(GPIO.BOARD)

到: GPIO.setmode(GPIO.BCM)

然后更改端口以匹配 BCM 端口,如下所示:

GPIO.setup(22, GPIO.OUT)
GPIO.output(22,True)

【问题讨论】:

    标签: python raspberry-pi gpio


    【解决方案1】:

    从这个question看来,GPIO有两种模式:GPIO.BCMGPIO.BOARD...尝试使用另一种:

    GPIO.setmode(GPIO.BCM)
    

    【讨论】:

    • 完美,我将其更改为 GPIO.BCM,现在一切正常。唯一的问题是我需要更改端口号以匹配 BCM 号,即 22
    • 这很奇怪,因为板 15 与 bcm 22 相同...?
    • 是的,设置正确的模式会有所帮助,尤其是像 raspi.tv/download/RPi.GPIO-Cheat-Sheet.pdf 这样的备忘单还有其他可用的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 2013-11-23
    相关资源
    最近更新 更多