Python Raspberry Pi GPIO错误

时间:2013-12-08 18:02:25

标签: python raspberry-pi gpio

我在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示例()之下:并且在rfid = SL030()之上,如下所示,那么它似乎没有错误地工作:

def example():

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

    rfid = SL030()

* 更新 - 解决方案 *

感谢André,我改变了:

GPIO.setmode(GPIO.BOARD)

于:     GPIO.setmode(GPIO.BCM)

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

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

1 个答案:

答案 0 :(得分:4)

question开始,GPIO有两种模式:GPIO.BCMGPIO.BOARD ...尝试使用另一种模式:

GPIO.setmode(GPIO.BCM)