我正在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()我所面临的问题是,虽然它操作pin 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()* SOLUTION*更新
多亏安德烈,我改变了:
GPIO.setmode(GPIO.BOARD)致: GPIO.setmode(GPIO.BCM)
然后将端口更改为与BCM端口匹配,如下所示:
GPIO.setup(22, GPIO.OUT)
GPIO.output(22,True)发布于 2013-12-08 21:54:00
从这个question看来,GPIO有两种模式:GPIO.BCM和GPIO.BOARD.试着使用另一个:
GPIO.setmode(GPIO.BCM)https://stackoverflow.com/questions/20456905
复制相似问题