【发布时间】: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