微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Raspberry Pi 4 上的 Waveshare PN532 NFC HAT

如何解决Raspberry Pi 4 上的 Waveshare PN532 NFC HAT

我为自己买了 Waveshare PN532 NFC HAT 并将其安装到我的 RaspBerry Pi 4 B 型。

我遵循了关于如何安装硬件和软件的官方指南。
现在我已经尝试运行一些给出的示例脚本,但出现错误

运行 example_get_uid.py 脚本运行良好。

运行 example_rw_mifare.py 脚本抛出此错误

pi@raspBerrypi:~/raspBerrypi/python $ python3 example_rw_mifare.py
Found PN532 with firmware version: 1.6
Waiting for RFID/NFC card to write to!
...Found card with UID: ['0x1','0x23','0x45','0x67']
Traceback (most recent call last):
  File "example_rw_mifare.py",line 57,in 
    uid,block_number=block_number,key_number=nfc.mifare_CMD_AUTH_A,key=key_a)
  File "/home/pi/raspBerrypi/python/pn532/pn532.py",line 395,in mifare_classic_authenticate_block
    if response[0]:
TypeError: 'nonetype' object is not subscriptable

所以看起来给定的 pn532 库中存在错误
有没有人以前遇到过这个错误并且知道如何解决它?

已经感谢您的回答。


编辑:

example_rw_mifare.py

    """
    This example shows connecting to the PN532 and writing an M1
    type RFID tag
    
    Warning: DO NOT write the blocks of 4N+3 (3,7,11,...,63)
    or else you will change the password for blocks 4N ~ 4N+2.
    
    Note: 
    1.  The first 6 bytes (KEY A) of the 4N+3 blocks are always shown as 0x00,since 'KEY A' is unreadable. In contrast,the last 6 bytes (KEY B) of the 
    4N+3 blocks are readable.
    2.  Block 0 is unwritable. 
    """
    import RPi.GPIO as GPIO
    
    import pn532.pn532 as nfc
    from pn532 import *
    
    
    pn532 = PN532_SPI(debug=False,reset=20,cs=4)
    #pn532 = PN532_I2C(debug=False,req=16)
    #pn532 = PN532_UART(debug=False,reset=20)
    
    ic,ver,rev,support = pn532.get_firmware_version()
    print('Found PN532 with firmware version: {0}.{1}'.format(ver,rev))
    
    # Configure PN532 to communicate with mifare cards
    pn532.SAM_configuration()
    
    print('Waiting for RFID/NFC card to write to!')
    while True:
     # Check if a card is available to read
     uid = pn532.read_passive_target(timeout=0.5)
     print('.',end="")
     # Try again if no card is available.
     if uid is not None:
       break
    print('Found card with UID:',[hex(i) for i in uid])
    
    """
    Warning: DO NOT write the blocks of 4N+3 (3,the last 6 bytes (KEY B) of the 
    4N+3 blocks are readable.
    2.  Block 0 is unwritable. 
    """
    # Write block #6
    block_number = 6
    key_a = b'\xFF\xFF\xFF\xFF\xFF\xFF'
    data = bytes([0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F])
    
    try:
     pn532.mifare_classic_authenticate_block(uid,key=key_a)
     pn532.mifare_classic_write_block(block_number,data)
     if pn532.mifare_classic_read_block(block_number) == data:
      print('write block %d successfully' % block_number)
    except nfc.PN532Error as e:
     print(e.errmsg)
    GPIO.cleanup()

这是pn532.py中的代码部分

def mifare_classic_authenticate_block(self,uid,block_number,ey_number,key):   # pylint: disable=invalid-name
 """Authenticate specified block number for a mifare classic card. Uid 
  should be a byte array with the UID of the card,block number should be
        the block to authenticate,key number should be the key type (like
        mifare_CMD_AUTH_A or mifare_CMD_AUTH_B),and key should be a byte array
        with the key data.  Returns True if the block was authenticated,or False
        if not authenticated.
        """
        # Build parameters for InDataExchange command to authenticate mifare card.
        uidlen = len(uid)
        keylen = len(key)
        params = bytearray(3+uidlen+keylen)
        params[0] = 0x01  # Max card numbers
        params[1] = key_number & 0xFF
        params[2] = block_number & 0xFF
        params[3:3+keylen] = key
        params[3+keylen:] = uid
        # Send InDataExchange request and verify response is 0x00.
        response = self.call_function(_COMMAND_INDATAEXCHANGE,params=params,response_length=1)
        if response[0]:
            raise PN532Error(response[0])
        return response[0] == 0x00

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?