This repository has been archived by the owner on Dec 21, 2018. It is now read-only.
/
readmifare.py
86 lines (76 loc) · 3.36 KB
/
readmifare.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Example of detecting and reading a block from a MiFare NFC card.
# Author: Tony DiCola
# Copyright (c) 2015 Adafruit Industries
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import binascii
import sys
import Adafruit_PN532 as PN532
# Setup how the PN532 is connected to the Raspbery Pi/BeagleBone Black.
# It is recommended to use a software SPI connection with 4 digital GPIO pins.
# Configuration for a Raspberry Pi:
CS = 18
MOSI = 23
MISO = 24
SCLK = 25
# Configuration for a BeagleBone Black:
# CS = 'P8_7'
# MOSI = 'P8_8'
# MISO = 'P8_9'
# SCLK = 'P8_10'
# Create an instance of the PN532 class.
pn532 = PN532.PN532(cs=CS, sclk=SCLK, mosi=MOSI, miso=MISO)
# Call begin to initialize communication with the PN532. Must be done before
# any other calls to the PN532!
pn532.begin()
# Get the firmware version from the chip and print(it out.)
ic, ver, rev, support = pn532.get_firmware_version()
print('Found PN532 with firmware version: {0}.{1}'.format(ver, rev))
# Configure PN532 to communicate with MiFare cards.
pn532.SAM_configuration()
# Main loop to detect cards and read a block.
print('Waiting for MiFare card...')
while True:
# Check if a card is available to read.
uid = pn532.read_passive_target()
# Try again if no card is available.
if uid is None:
continue
print('Found card with UID: 0x{0}'.format(binascii.hexlify(uid)))
# Authenticate block 4 for reading with default key (0xFFFFFFFFFFFF).
if not pn532.mifare_classic_authenticate_block(uid, 4, PN532.MIFARE_CMD_AUTH_B,
[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]):
print('Failed to authenticate block 4!')
continue
# Read block 4 data.
data = pn532.mifare_classic_read_block(4)
if data is None:
print('Failed to read block 4!')
continue
# Note that 16 bytes are returned, so only show the first 4 bytes for the block.
print('Read block 4: 0x{0}'.format(binascii.hexlify(data[:4])))
# Example of writing data to block 4. This is commented by default to
# prevent accidentally writing a card.
# Set first 4 bytes of block to 0xFEEDBEEF.
# data[0:4] = [0xFE, 0xED, 0xBE, 0xEF]
# # Write entire 16 byte block.
# pn532.mifare_classic_write_block(4, data)
# print('Wrote to block 4, exiting program!')
# # Exit the program to prevent continually writing to card.
# sys.exit(0)