Skip to content

Commit 2988363

Browse files
documentation update
1 parent cc2af66 commit 2988363

File tree

6 files changed

+84
-83
lines changed

6 files changed

+84
-83
lines changed

README.md

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Python RC522 library
2-
pi-rc522 consists of two Python classes for controlling an SPI RFID module "RC522" using Raspberry Pi. You can get this on AliExpress for $3.
2+
pi-rc522 consists of two Python classes for controlling an SPI RFID module "RC522" using Raspberry Pi. You can get this module on AliExpress or Ebay for $3.
33

44
Based on [MFRC522-python](https://github.com/mxgxw/MFRC522-python/blob/master/README.md).
55

6-
You'll need **SPI-Py**. Recently Raspbian switched to newer version of SPI driver. This caused compatibility issues with the outdated SPI-Py. Hence it's required to use a slightly modified version of SPI-Py. You can find it [**here**](https://github.com/mab5vot9us9a/SPI-Py).
6+
You'll also need to install the [**SPI-Py**](https://github.com/lthiery/SPI-Py) library.
77

88
[MIFARE datasheet](http://www.nxp.com/documents/data_sheet/MF1S503x.pdf) can be useful.
99

@@ -29,87 +29,87 @@ and you can connect RST pin to any other free GPIO pin and call the constructor
2929
__NOTE:__ For RPi A+/B+/2/3 with 40 pin connector, SPI1/2 is available on top of SPI0. Kernel 4.4.x or higher and *dtoverlay* configuration is required. For SPI1/2, *pin_ce=__BOARD numbering pin__* is required.
3030

3131
## Usage
32-
The library is split to two classes - **RFID** and **RFIDUtil**. You can use only RFID, RFIDUtil just makes life a little bit better.
32+
The library is split to two classes - **RFID** and **RFIDUtil**. You can use only RFID, RFIDUtil just makes life a little bit better.
3333
You basically want to start with *while True* loop and "poll" the tag state. That's done using *request* method. Most of the methods
3434
return error state, which is simple boolean - True is error, False is not error. The *request* method returns True if tag is **not**
3535
present. If request is successful, you should call *anticoll* method. It runs anti-collision alghoritms and returns used tag UID, which
36-
you'll use for *select_tag* method. Now you can do whatever you want. Important methods are documented. You can also look to
37-
Read and KeyChange modules for RFIDUtil usage example.
36+
you'll use for *select_tag* method. Now you can do whatever you want. Important methods are documented. You can also look at the Read and KeyChange examples for RFIDUtil usage.
3837

3938
```python
40-
rdr = RFID.RFID()
39+
from pirc522 import RFID
40+
rdr = RFID()
41+
4142
while True:
4243
(error, tag_type) = rdr.request()
4344
if not error:
44-
print ("Tag detected")
45+
print("Tag detected")
4546
(error, uid) = rdr.anticoll()
4647
if not error:
47-
print ("UID: " + str(uid))
48-
#Select Tag is required before Auth
48+
print("UID: " + str(uid))
49+
# Select Tag is required before Auth
4950
if not rdr.select_tag(uid):
50-
#Auth for block 10 (block 2 of sector 2) using default shipping key A
51+
# Auth for block 10 (block 2 of sector 2) using default shipping key A
5152
if not rdr.card_auth(rdr.auth_a, 10, [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF], uid):
52-
#This will print something like (False, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
53-
print ("Reading block 10: " + str(rdr.read(10)))
54-
#Always stop crypto1 when done working
53+
# This will print something like (False, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
54+
print("Reading block 10: " + str(rdr.read(10)))
55+
# Always stop crypto1 when done working
5556
rdr.stop_crypto()
56-
57-
58-
#Calls GPIO cleanup
57+
58+
# Calls GPIO cleanup
5959
rdr.cleanup()
6060
```
6161

6262
### Util usage
63-
**RFIDUtil** contains few useful methods for dealing with tags.
63+
**RFIDUtil** contains a few useful methods for dealing with tags.
6464

6565
```python
66-
import RFID
66+
from pirc522 import RFID
6767
import signal
6868
import time
6969

70-
rdr = RFID.RFID()
70+
rdr = RFID()
7171
util = rdr.util()
72-
#Set util debug to true - it will print what's going on
72+
# Set util debug to true - it will print what's going on
7373
util.debug = True
7474

7575
while True:
76-
#Request tag
76+
# Request tag
7777
(error, data) = rdr.request()
7878
if not error:
79-
print ("\nDetected")
79+
print("\nDetected")
8080

8181
(error, uid) = rdr.anticoll()
8282
if not error:
83-
#Print UID
84-
print ("Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]))
83+
# Print UID
84+
print("Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]))
8585

86-
#Set tag as used in util. This will call RFID.select_tag(uid)
86+
# Set tag as used in util. This will call RFID.select_tag(uid)
8787
util.set_tag(uid)
88-
#Save authorization info (key B) to util. It doesn't call RFID.card_auth(), that's called when needed
88+
# Save authorization info (key B) to util. It doesn't call RFID.card_auth(), that's called when needed
8989
util.auth(rdr.auth_b, [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
90-
#Print contents of block 4 in format "S1B0: [contents in decimal]". RFID.card_auth() will be called now
90+
# Print contents of block 4 in format "S1B0: [contents in decimal]". RFID.card_auth() will be called now
9191
util.read_out(4)
92-
#Print it again - now auth won't be called, because it doesn't have to be
92+
# Print it again - now auth won't be called, because it doesn't have to be
9393
util.read_out(4)
94-
#Print contents of different block - S1B2 - RFID.card_auth() will be called again
94+
# Print contents of different block - S1B2 - RFID.card_auth() will be called again
9595
util.read_out(6)
96-
#We can change authorization info if you have different key in other sector
96+
# We can change authorization info if you have different key in other sector
9797
util.auth(rdr.auth_a, [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
9898
#If you want to use methods from RFID itself, you can use this for authorization
99-
#This will authorize for block 1 of sector 2 -> block 9
100-
#This is once more called only if it's not already authorized for this block
99+
# This will authorize for block 1 of sector 2 -> block 9
100+
# This is once more called only if it's not already authorized for this block
101101
util.do_auth(util.block_addr(2, 1))
102-
#Now we can do some "lower-level" stuff with block 9
102+
# Now we can do some "lower-level" stuff with block 9
103103
rdr.write(9, [0x01, 0x23, 0x45, 0x67, 0x89, 0x98, 0x76, 0x54, 0x32, 0x10, 0x69, 0x27, 0x46, 0x66, 0x66, 0x64])
104-
#We can rewrite specific bytes in block using this method. None means "don't change this byte"
105-
#Note that this won't do authorization, because we've already called do_auth for block 9
104+
# We can rewrite specific bytes in block using this method. None means "don't change this byte"
105+
# Note that this won't do authorization, because we've already called do_auth for block 9
106106
util.rewrite(9, [None, None, 0xAB, 0xCD, 0xEF])
107-
#This will write S2B1: [0x01, 0x23, 0xAB, 0xCD, 0xEF, 0x98, 0x76......] because we've rewritten third, fourth and fifth byte
107+
# This will write S2B1: [0x01, 0x23, 0xAB, 0xCD, 0xEF, 0x98, 0x76......] because we've rewritten third, fourth and fifth byte
108108
util.read_out(9)
109-
#Let's see what do we have in whole tag
109+
# Let's see what do we have in whole tag
110110
util.dump()
111-
#We must stop crypto
111+
# We must stop crypto
112112
util.deauth()
113-
113+
114114
time.sleep(1)
115115
```

examples/KeyChange.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,43 @@
1-
import RFID
21
import signal
32
import time
43

4+
from pirc522 import RFID
5+
56
run = True
6-
rdr = RFID.RFID()
7+
rdr = RFID()
78
util = rdr.util()
89
util.debug = True
910

1011
def end_read(signal,frame):
1112
global run
12-
print ("\nCtrl+C captured, ending read.")
13+
print("\nCtrl+C captured, ending read.")
1314
run = False
1415
rdr.cleanup()
1516

1617
signal.signal(signal.SIGINT, end_read)
1718

18-
print ("Starting")
19+
print("Starting")
1920
while run:
2021
(error, data) = rdr.request()
2122
if not error:
22-
print ("\nDetected: " + format(data, "02x"))
23+
print("\nDetected: " + format(data, "02x"))
2324

2425
(error, uid) = rdr.anticoll()
2526
if not error:
26-
print ("Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]))
27+
print("Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]))
2728

28-
print ("Setting tag")
29+
print("Setting tag")
2930
util.set_tag(uid)
30-
print ("\nAuthorizing")
31+
print("\nAuthorizing")
3132
util.auth(rdr.auth_a, [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF])
32-
print ("\nWriting modified bytes")
33+
print("\nWriting modified bytes")
3334
util.rewrite(4, [None, None, 0x69, 0x24, 0x40])
3435
util.read_out(4)
3536
"""
36-
print ("\nWriting zero bytes")
37+
print("\nWriting zero bytes")
3738
util.rewrite(2, [None, None, 0, 0, 0])
3839
util.read_out(2)
39-
print ("\nDeauthorizing")
40+
print("\nDeauthorizing")
4041
util.deauth()
4142
"""
4243

examples/Read.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
1-
import RFID
21
import signal
32
import time
43

4+
from pirc522 import RFID
5+
56
run = True
6-
rdr = RFID.RFID()
7+
rdr = RFID()
78
util = rdr.util()
89
util.debug = True
910

1011
def end_read(signal,frame):
1112
global run
12-
print ("\nCtrl+C captured, ending read.")
13+
print("\nCtrl+C captured, ending read.")
1314
run = False
1415
rdr.cleanup()
1516

1617
signal.signal(signal.SIGINT, end_read)
1718

18-
print ("Starting")
19+
print("Starting")
1920
while run:
2021
(error, data) = rdr.request()
2122
if not error:
22-
print ("\nDetected: " + format(data, "02x"))
23+
print("\nDetected: " + format(data, "02x"))
2324

2425
(error, uid) = rdr.anticoll()
2526
if not error:
26-
print ("Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]))
27+
print("Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]))
2728

28-
print ("Setting tag")
29+
print("Setting tag")
2930
util.set_tag(uid)
30-
print ("\nAuthorizing")
31+
print("\nAuthorizing")
3132
#util.auth(rdr.auth_a, [0x12, 0x34, 0x56, 0x78, 0x96, 0x92])
3233
util.auth(rdr.auth_b, [0x74, 0x00, 0x52, 0x35, 0x00, 0xFF])
33-
print ("\nReading")
34+
print("\nReading")
3435
util.read_out(4)
35-
print ("\nDeauthorizing")
36+
print("\nDeauthorizing")
3637
util.deauth()
37-
38+
3839
time.sleep(1)

examples/UtilExample.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import RFID
21
import signal
32
import time
43

5-
rdr = RFID.RFID()
4+
from pirc522 import RFID
5+
6+
rdr = RFID()
67
util = rdr.util()
78
#Set util debug to true - it will print what's going on
89
util.debug = True
@@ -15,35 +16,35 @@
1516

1617
(error, uid) = rdr.anticoll()
1718
if not error:
18-
#Print UID
19+
# Print UID
1920
print("Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]))
2021

21-
#Set tag as used in util. This will call RFID.select_tag(uid)
22+
# Set tag as used in util. This will call RFID.select_tag(uid)
2223
util.set_tag(uid)
23-
#Save authorization info (key B) to util. It doesn't call RFID.card_auth(), that's called when needed
24+
# Save authorization info (key B) to util. It doesn't call RFID.card_auth(), that's called when needed
2425
util.auth(rdr.auth_b, [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
25-
#Print contents of block 4 in format "S1B0: [contents in decimal]". RFID.card_auth() will be called now
26+
# Print contents of block 4 in format "S1B0: [contents in decimal]". RFID.card_auth() will be called now
2627
util.read_out(4)
27-
#Print it again - now auth won't be called, because it doesn't have to be
28+
# Print it again - now auth won't be called, because it doesn't have to be
2829
util.read_out(4)
29-
#Print contents of different block - S1B2 - RFID.card_auth() will be called again
30+
# Print contents of different block - S1B2 - RFID.card_auth() will be called again
3031
util.read_out(6)
31-
#We can change authorization info if you have different key in other sector
32+
# We can change authorization info if you have different key in other sector
3233
util.auth(rdr.auth_a, [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
33-
#If you want to use methods from RFID itself, you can use this for authorization
34-
#This will authorize for block 1 of sector 2 -> block 9
35-
#This is once more called only if it's not already authorized for this block
34+
# If you want to use methods from RFID itself, you can use this for authorization
35+
# This will authorize for block 1 of sector 2 -> block 9
36+
# This is once more called only if it's not already authorized for this block
3637
util.do_auth(util.block_addr(2, 1))
37-
#Now we can do some "lower-level" stuff with block 9
38+
# Now we can do some "lower-level" stuff with block 9
3839
rdr.write(9, [0x01, 0x23, 0x45, 0x67, 0x89, 0x98, 0x76, 0x54, 0x32, 0x10, 0x69, 0x27, 0x46, 0x66, 0x66, 0x64])
39-
#We can rewrite specific bytes in block using this method. None means "don't change this byte"
40-
#Note that this won't do authorization, because we've already called do_auth for block 9
40+
# We can rewrite specific bytes in block using this method. None means "don't change this byte"
41+
# Note that this won't do authorization, because we've already called do_auth for block 9
4142
util.rewrite(9, [None, None, 0xAB, 0xCD, 0xEF])
42-
#This will write S2B1: [0x01, 0x23, 0xAB, 0xCD, 0xEF, 0x98, 0x76......] because we've rewritten third, fourth and fifth byte
43+
# This will write S2B1: [0x01, 0x23, 0xAB, 0xCD, 0xEF, 0x98, 0x76......] because we've rewritten third, fourth and fifth byte
4344
util.read_out(9)
44-
#Let's see what do we have in whole tag
45+
# Let's see what do we have in whole tag
4546
util.dump()
46-
#We must stop crypto
47+
# We must stop crypto
4748
util.deauth()
4849

4950
time.sleep(1)

pirc522/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ def util(self):
358358
If module is not present, returns None.
359359
"""
360360
try:
361-
import RFIDUtil
362-
return RFIDUtil.RFIDUtil(self)
361+
from .util import RFIDUtil
362+
return RFIDUtil(self)
363363
except ImportError:
364364
return None

pirc522/util.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import RFID
2-
31

42
class RFIDUtil(object):
53
rfid = None

0 commit comments

Comments
 (0)