adafruit / Adafruit_CircuitPython_PN532

CircuitPython driver for the PN532 NFC/RFID Breakout and PN532 NFC/RFID Shield
MIT License
91 stars 47 forks source link

ntag2xx_read_block() should return None on failure #52

Closed jerryneedell closed 2 years ago

jerryneedell commented 2 years ago

A Discord user reported this error: https://discord.com/channels/327254708534116352/537365702651150357/942511372028223538

    cardReadValues = (pn532.ntag2xx_read_block(6))
    cardRead = int.from_bytes((cardReadValues), "big")
    print("Reading! This is Card", (cardRead), "!")
    audio.play(sounds[cardRead])

    while audio.playing:
        pass
    print("stopped")

Here's the bit of code, and here's the error

Traceback (most recent call last):
  File "code.py", line 73, in <module>
  File "/lib/adafruit_pn532/adafruit_pn532.py", line 497, in ntag2xx_read_block
TypeError: 'NoneType' object isn't subscriptable

The problem was the card was being removed too quickly, but the function should return none rather than throw an error according to the doctoring:


    def ntag2xx_read_block(self, block_number):
        """Read a block of data from the card.  Block number should be the block
        to read.  If the block is successfully read a bytearray of length 16 with
        data starting at the specified block will be returned.  If the block is
        not read then None will be returned.
``` at 
https://github.com/adafruit/Adafruit_CircuitPython_PN532/blob/main/adafruit_pn532/adafruit_pn532.py#L490

If it returned None, the user could test for None before using the result.