forked from tsndr/MFRC522-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.py
95 lines (75 loc) · 2.25 KB
/
read.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
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python
import RPi.GPIO as GPIO
import MFRC522
# Keys
DEFAULT_KEY = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
# Selecting key
KEY = DEFAULT_KEY
def format_uid(uid):
s = ""
for i in range(0, len(uid)):
s += "%x" % uid[i]
return s.upper()
RFID = MFRC522.MFRC522()
print "# RFID Reader\n"
print "Info: Leave the sector field empty to exit.\n"
# Get tag size if available
(Status, TagSize) = RFID.Request(RFID.PICC_REQIDL)
while True:
if TagSize > 0:
message = "Sector [1 - %s]: " % (TagSize - 1)
else:
message = "Sector: "
try:
Sector = input(message)
except:
print ""
break
else:
print "Waiting for Tag...\n"
while True:
(Status, TagSize) = RFID.Request(RFID.PICC_REQIDL)
if Status != RFID.MI_OK:
continue
if TagSize < 1:
print("Can't read tag properly!")
break
if Sector < 1 or Sector > (TagSize - 1):
print "Sector out of range (1 - %s)\n" % (TagSize - 1)
break
# Selecting blocks
BaseBlockLength = 4
if Sector < 32:
BlockLength = BaseBlockLength
StartAddr = Sector * BlockLength
else:
BlockLength = 16
StartAddr = 32 * BaseBlockLength + (Sector - 32) * BlockLength
BlockAddrs = []
for i in range(0, (BlockLength - 1)):
BlockAddrs.append((StartAddr + i))
TrailerBlockAddr = (StartAddr + (BlockLength - 1))
# Initializing tag
(Status, UID) = RFID.Anticoll()
if Status != RFID.MI_OK:
break
# Reading sector
RFID.SelectTag(UID)
Status = RFID.Auth(RFID.PICC_AUTHENT1A, TrailerBlockAddr, KEY, UID)
data = []
text_read = ""
if Status == RFID.MI_OK:
for block_num in BlockAddrs:
block = RFID.Read(block_num)
if block:
data += block
if data:
text_read = "".join(chr(i) for i in data)
print "UID: ", format_uid(UID)
print "Data: ", text_read,"\n"
else:
print "Can't access sector", Sector, "!\n"
RFID.StopCrypto1()
break
RFID.AntennaOff()
GPIO.cleanup()