2013-12-08 241 views
1

我正在我的树莓派下面的python脚本:Python的树莓派GPIO错误

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 

任何想法可能是错误的?

由于

UPDATE

如果我把GPIO代码略低于DEF例子():与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) 
+0

任何帮助任何人吗? – John

回答

4

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

GPIO.setmode(GPIO.BCM) 
+0

完美,我将它改为GPIO.BCM,现在它的全部工作。唯一的问题是我需要改变端口号来匹配BCM号码,这是22 – John

+1

这很奇怪,因为第15号板和bcm 22一样...? – thommie

+0

是的,设置正确的模式有帮助,特别是像http://raspi.tv/download/RPi.GPIO-Cheat-Sheet.pdf这样的cheatsheet还有其他可用的。 – Simara