From 2b096008e7ba50a5521ce1ab17617478c59df3f7 Mon Sep 17 00:00:00 2001 From: jerryneedell Date: Mon, 14 Feb 2022 16:54:06 -0500 Subject: [PATCH] revise ntag2xx_read_block() to return None on failure, update example --- adafruit_pn532/adafruit_pn532.py | 13 +++++++------ examples/pn532_readwrite_ntag2xx.py | 12 ++++++++---- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/adafruit_pn532/adafruit_pn532.py b/adafruit_pn532/adafruit_pn532.py index 6671dc0..9c2a7eb 100644 --- a/adafruit_pn532/adafruit_pn532.py +++ b/adafruit_pn532/adafruit_pn532.py @@ -489,10 +489,11 @@ class PN532: 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. + to read. If the block is successfully read the first 4 bytes (after the + leading 0x00 byte) will be returned. + If the block is not read then None will be returned. """ - return self.mifare_classic_read_block(block_number)[ - 0:4 - ] # only 4 bytes per page + ntag2xx_block = self.mifare_classic_read_block(block_number) + if ntag2xx_block is not None: + return ntag2xx_block[0:4] # only 4 bytes per page + return None diff --git a/examples/pn532_readwrite_ntag2xx.py b/examples/pn532_readwrite_ntag2xx.py index 268a89f..78c0579 100644 --- a/examples/pn532_readwrite_ntag2xx.py +++ b/examples/pn532_readwrite_ntag2xx.py @@ -67,7 +67,11 @@ data[0:4] = b"\xFE\xED\xBE\xEF" # Write 4 byte block. pn532.ntag2xx_write_block(6, data) # Read block #6 -print( - "Wrote to block 6, now trying to read that data:", - [hex(x) for x in pn532.ntag2xx_read_block(6)], -) +ntag2xx_block = pn532.ntag2xx_read_block(6) +if ntag2xx_block is not None: + print( + "Wrote to block 6, now trying to read that data:", + [hex(x) for x in pn532.ntag2xx_read_block(6)], + ) +else: + print("Read failed - did you remove the card?")