覆盆子皮4上的Waveshare PN532 NFC帽子

2024-05-23 19:03:33 发布

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

我给自己买了Waveshare PN532 NFC HAT并把它安装到我的Raspberry Pi 4型号B上

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

运行示例_get_uid.py脚本工作得非常好

运行示例_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库中似乎有错误。
有没有人以前有过这个错误,并且知道如何修复它

谢谢你的回答


编辑:

示例_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, reset=20, 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, 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. 
    """
    # 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, block_number=block_number, key_number=nfc.MIFARE_CMD_AUTH_A, 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

Tags: thetokeypynumberuidifis