forked from tsndr/MFRC522-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
write.py
101 lines (84 loc) · 2.52 KB
/
write.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
96
97
98
99
100
101
#!/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 Writer\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:
if TagSize > 0:
if Sector >= 32:
MaxChars = 16 * 15
else:
MaxChars = 16 * 3
message = "Data [max %s chars]: " % MaxChars
else:
message = "Data: "
try:
text = raw_input(message)
except:
print "\n"
continue
else:
print "Waiting for Tag...\n"
while True:
(status, TagSize) = RFID.Request(RFID.PICC_REQIDL)
if status != RFID.MI_OK:
continue
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
# Writing data
RFID.SelectTag(UID)
status = RFID.Auth(RFID.PICC_AUTHENT1A, TrailerBlockAddr, KEY, UID)
if status == RFID.MI_OK:
data = bytearray()
data.extend(bytearray(text.ljust(len(BlockAddrs) * 16)))
i = 0
for block_num in BlockAddrs:
RFID.Write(block_num, data[(i*16):(i+1)*16])
i += 1
print "UID: ", format_uid(UID)
print "Data: ", text[0:(len(BlockAddrs) * 16)], "\n"
else:
print "Can't access sector", Sector, "!\n"
RFID.StopCrypto1()
break
RFID.AntennaOff()
GPIO.cleanup()