Python Raspberry Pi GPIO 错误

Joh*_*ohn 3 python gpio raspberry-pi

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

\n\n

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

\n\n

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

\n\n
def example():\n\nrfid = SL030()\nfw = rfid.get_firmware()\nprint("RFID reader firmware:" + fw)\nprint()\n\nGPIO.setmode(GPIO.BOARD)\nGPIO.setup(15, GPIO.OUT)\nGPIO.output(15,True)\n\n\nwhile True:\n    rfid.wait_tag()\n    print("card present")\n\n    if rfid.select_mifare():\n        type = rfid.get_type()\n        print("type:" + rfid.get_typename(type))\n\n        id = rfid.get_uidstr()\n        try:\n            user = cards[id]\n            print(user)\n            #os.system("aplay " + user)\n        except KeyError:\n            print("Unknown card:" + id)\n\n    rfid.wait_notag()\n    print("card removed")\n    print()\n
Run Code Online (Sandbox Code Playgroud)\n\n

我面临的问题是,虽然它操作引脚 15,但脚本会因以下错误而停止:

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

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

\n\n

谢谢

\n\n

更新

\n\n

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

\n\n
def example():\n\n    GPIO.setmode(GPIO.BOARD)\n    GPIO.setup(15, GPIO.OUT)\n    GPIO.output(15,True)\n\n    rfid = SL030()\n
Run Code Online (Sandbox Code Playgroud)\n\n

*更新 - 解决方案*

\n\n

感谢 Andr\xc3\xa9,我改变了:

\n\n
GPIO.setmode(GPIO.BOARD)\n
Run Code Online (Sandbox Code Playgroud)\n\n

到:\n GPIO.setmode(GPIO.BCM)

\n\n

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

\n\n
GPIO.setup(22, GPIO.OUT)\nGPIO.output(22,True)\n
Run Code Online (Sandbox Code Playgroud)\n

小智 6

从这个问题来看,GPIO 有两种模式:GPIO.BCM并且GPIO.BOARD...尝试使用另一种:

GPIO.setmode(GPIO.BCM)
Run Code Online (Sandbox Code Playgroud)