revise ntag2xx_read_block() to return None on failure, update example

This commit is contained in:
jerryneedell 2022-02-14 16:54:06 -05:00
parent dea8f14cf1
commit 2b096008e7
2 changed files with 15 additions and 10 deletions

View file

@ -489,10 +489,11 @@ class PN532:
def ntag2xx_read_block(self, block_number): def ntag2xx_read_block(self, block_number):
"""Read a block of data from the card. Block number should be the block """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 to read. If the block is successfully read the first 4 bytes (after the
data starting at the specified block will be returned. If the block is leading 0x00 byte) will be returned.
not read then None will be returned. If the block is not read then None will be returned.
""" """
return self.mifare_classic_read_block(block_number)[ ntag2xx_block = self.mifare_classic_read_block(block_number)
0:4 if ntag2xx_block is not None:
] # only 4 bytes per page return ntag2xx_block[0:4] # only 4 bytes per page
return None

View file

@ -67,7 +67,11 @@ data[0:4] = b"\xFE\xED\xBE\xEF"
# Write 4 byte block. # Write 4 byte block.
pn532.ntag2xx_write_block(6, data) pn532.ntag2xx_write_block(6, data)
# Read block #6 # Read block #6
print( ntag2xx_block = pn532.ntag2xx_read_block(6)
"Wrote to block 6, now trying to read that data:", if ntag2xx_block is not None:
[hex(x) for x in pn532.ntag2xx_read_block(6)], 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?")