Python覆盆子Pi-GPI

2024-05-03 11:30:03 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我已经修改了接近尾端的脚本,以访问GPIO pin 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 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.BCM)

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

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

Tags: inpy脚本truegetgpioexampledef