Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions adafruit_pn532/adafruit_pn532.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,10 +489,11 @@ def ntag2xx_write_block(self, block_number, data):

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
12 changes: 8 additions & 4 deletions examples/pn532_readwrite_ntag2xx.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@
# 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 ntag2xx_block],
)
else:
print("Read failed - did you remove the card?")