Skip to content

Commit 949a5c6

Browse files
authored
Merge pull request #21 from fezthedev/readwrite_mifare
Copied and modified readwrite tutorial to support mifare classic cards
2 parents 3d9866b + 1ef38a4 commit 949a5c6

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

examples/pn532_readwrite_mifare.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Example of detecting and reading a block from a MiFare classic NFC card.
2+
# Author: Tony DiCola & Roberto Laricchia
3+
# MiFare Classic modification: Francesco Crisafulli
4+
#
5+
# Copyright (c) 2015 Adafruit Industries
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in all
15+
# copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
# SOFTWARE.
24+
25+
"""
26+
This example shows connecting to the PN532 and writing & reading a mifare classic
27+
type RFID tag
28+
"""
29+
30+
import board
31+
import busio
32+
#
33+
# NOTE: pick the import that matches the interface being used
34+
#
35+
from adafruit_pn532.adafruit_pn532 import MIFARE_CMD_AUTH_B
36+
from adafruit_pn532.i2c import PN532_I2C
37+
#from adafruit_pn532.spi import PN532_SPI
38+
#from adafruit_pn532.uart import PN532_UART
39+
40+
# I2C connection:
41+
i2c = busio.I2C(board.SCL, board.SDA)
42+
43+
# Non-hardware reset/request with I2C
44+
pn532 = PN532_I2C(i2c, debug=False)
45+
46+
# With I2C, we recommend connecting RSTPD_N (reset) to a digital pin for manual
47+
# harware reset
48+
#reset_pin = DigitalInOut(board.D6)
49+
# On Raspberry Pi, you must also connect a pin to P32 "H_Request" for hardware
50+
# wakeup! this means we don't need to do the I2C clock-stretch thing
51+
#req_pin = DigitalInOut(board.D12)
52+
#pn532 = PN532_I2C(i2c, debug=False, reset=reset_pin, req=req_pin)
53+
54+
# SPI connection:
55+
#spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
56+
#cs_pin = DigitalInOut(board.D5)
57+
#pn532 = PN532_SPI(spi, cs_pin, debug=False)
58+
59+
# UART connection
60+
#uart = busio.UART(board.TX, board.RX, baudrate=115200, timeout=100)
61+
#pn532 = PN532_UART(uart, debug=False)
62+
63+
ic, ver, rev, support = pn532.get_firmware_version()
64+
print('Found PN532 with firmware version: {0}.{1}'.format(ver, rev))
65+
66+
# Configure PN532 to communicate with MiFare cards
67+
pn532.SAM_configuration()
68+
69+
print('Waiting for RFID/NFC card to write to!')
70+
71+
key = b'\xFF\xFF\xFF\xFF\xFF\xFF'
72+
73+
while True:
74+
# Check if a card is available to read
75+
uid = pn532.read_passive_target(timeout=0.5)
76+
print('.', end="")
77+
# Try again if no card is available.
78+
if uid is not None:
79+
break
80+
81+
print("")
82+
83+
print('Found card with UID:', [hex(i) for i in uid])
84+
print("Authenticating block 4 ...")
85+
86+
authenticated = pn532.mifare_classic_authenticate_block(uid, 4, MIFARE_CMD_AUTH_B, key)
87+
if not authenticated:
88+
print("Authentication failed!")
89+
90+
# Set 16 bytes of block to 0xFEEDBEEF
91+
data = bytearray(16)
92+
data[0:16] = b'\xFE\xED\xBE\xEF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
93+
94+
# Write 16 byte block.
95+
pn532.mifare_classic_write_block(4, data)
96+
# Read block #6
97+
print('Wrote to block 4, now trying to read that data:',
98+
[hex(x) for x in pn532.mifare_classic_read_block(4)])

0 commit comments

Comments
 (0)