Skip to content

Commit 2b09600

Browse files
committed
revise ntag2xx_read_block() to return None on failure, update example
1 parent dea8f14 commit 2b09600

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

adafruit_pn532/adafruit_pn532.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -489,10 +489,11 @@ def ntag2xx_write_block(self, block_number, data):
489489

490490
def ntag2xx_read_block(self, block_number):
491491
"""Read a block of data from the card. Block number should be the block
492-
to read. If the block is successfully read a bytearray of length 16 with
493-
data starting at the specified block will be returned. If the block is
494-
not read then None will be returned.
492+
to read. If the block is successfully read the first 4 bytes (after the
493+
leading 0x00 byte) will be returned.
494+
If the block is not read then None will be returned.
495495
"""
496-
return self.mifare_classic_read_block(block_number)[
497-
0:4
498-
] # only 4 bytes per page
496+
ntag2xx_block = self.mifare_classic_read_block(block_number)
497+
if ntag2xx_block is not None:
498+
return ntag2xx_block[0:4] # only 4 bytes per page
499+
return None

examples/pn532_readwrite_ntag2xx.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@
6767
# Write 4 byte block.
6868
pn532.ntag2xx_write_block(6, data)
6969
# Read block #6
70-
print(
71-
"Wrote to block 6, now trying to read that data:",
72-
[hex(x) for x in pn532.ntag2xx_read_block(6)],
73-
)
70+
ntag2xx_block = pn532.ntag2xx_read_block(6)
71+
if ntag2xx_block is not None:
72+
print(
73+
"Wrote to block 6, now trying to read that data:",
74+
[hex(x) for x in pn532.ntag2xx_read_block(6)],
75+
)
76+
else:
77+
print("Read failed - did you remove the card?")

0 commit comments

Comments
 (0)