Skip to content

Commit ea97723

Browse files
Copied and modified readwrite tutorial to support mifare classic cards
1 parent 6e2b2a6 commit ea97723

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

examples/readwrite_mifare.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
from digitalio import DigitalInOut
33+
#
34+
# NOTE: pick the import that matches the interface being used
35+
#
36+
from adafruit_pn532.adafruit_pn532 import MIFARE_CMD_AUTH_B
37+
from adafruit_pn532.i2c import PN532_I2C
38+
#from adafruit_pn532.spi import PN532_SPI
39+
#from adafruit_pn532.uart import PN532_UART
40+
41+
# I2C connection:
42+
i2c = busio.I2C(board.SCL, board.SDA)
43+
44+
# Non-hardware reset/request with I2C
45+
pn532 = PN532_I2C(i2c, debug=False)
46+
47+
# With I2C, we recommend connecting RSTPD_N (reset) to a digital pin for manual
48+
# harware reset
49+
#reset_pin = DigitalInOut(board.D6)
50+
# On Raspberry Pi, you must also connect a pin to P32 "H_Request" for hardware
51+
# wakeup! this means we don't need to do the I2C clock-stretch thing
52+
#req_pin = DigitalInOut(board.D12)
53+
#pn532 = PN532_I2C(i2c, debug=False, reset=reset_pin, req=req_pin)
54+
55+
# SPI connection:
56+
#spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
57+
#cs_pin = DigitalInOut(board.D5)
58+
#pn532 = PN532_SPI(spi, cs_pin, debug=False)
59+
60+
# UART connection
61+
#uart = busio.UART(board.TX, board.RX, baudrate=115200, timeout=100)
62+
#pn532 = PN532_UART(uart, debug=False)
63+
64+
ic, ver, rev, support = pn532.get_firmware_version()
65+
print('Found PN532 with firmware version: {0}.{1}'.format(ver, rev))
66+
67+
# Configure PN532 to communicate with MiFare cards
68+
pn532.SAM_configuration()
69+
70+
print('Waiting for RFID/NFC card to write to!')
71+
72+
key = b'\xFF\xFF\xFF\xFF\xFF\xFF'
73+
74+
while True:
75+
# Check if a card is available to read
76+
uid = pn532.read_passive_target(timeout=0.5)
77+
print('.', end="")
78+
# Try again if no card is available.
79+
if uid is not None:
80+
break
81+
82+
print("")
83+
84+
print('Found card with UID:', [hex(i) for i in uid])
85+
print("Authenticating block 4 ...")
86+
87+
authenticated = pn532.mifare_classic_authenticate_block(uid,4,MIFARE_CMD_AUTH_B,key)
88+
if not authenticated:
89+
print("Authentication failed!")
90+
91+
# Set 16 bytes of block to 0xFEEDBEEF
92+
data = bytearray(16)
93+
data[0:16] = b'\xFE\xED\xBE\xEF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
94+
95+
# Write 16 byte block.
96+
pn532.mifare_classic_write_block(4, data)
97+
# Read block #6
98+
print('Wrote to block 4, now trying to read that data:',
99+
[hex(x) for x in pn532.mifare_classic_read_block(4)])

0 commit comments

Comments
 (0)