1+ #!/usr/bin/env python3
2+ # -*- coding: utf8 -*-
3+
4+ import RFIDI2CMicroPython
5+ import random
6+
7+ continue_reading = True
8+
9+ # Create random data
10+ def random_data (size = 16 ):
11+ data = []
12+ for i in range (size ):
13+ data .append (random .randint (0 , 255 ))
14+ return (data )
15+
16+ # Create an object of the class mrfc
17+ MRFC522Reader = RFIDI2CMicroPython .MRFC522 ()
18+
19+ MRFC522Reader .showReaderDetails ()
20+
21+ while continue_reading :
22+ # Scan for cards
23+ (status , backData , tagType ) = MRFC522Reader .scan ()
24+ if status == MRFC522Reader .MI_OK :
25+ print ('Card detected, Type:' ,tagType )
26+
27+ # Get UID of the card
28+ (status , uid , backBits ) = MRFC522Reader .transceive ()
29+ if status == MRFC522Reader .MI_OK :
30+ print ('Card identified, UID:' ,hex (uid [0 ]),hex (uid [1 ]),hex (uid [2 ]),hex (uid [3 ]))
31+
32+ # Select the scanned card
33+ (status , backData , backBits ) = MRFC522Reader .select (uid )
34+ if status == MRFC522Reader .MI_OK :
35+ print ('Card selected' )
36+
37+ # Authenticate
38+ mode = MRFC522Reader .MIFARE_AUTHKEY1
39+ blockAddr = 8
40+ key = [0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ]
41+ blockAddrList = []
42+ for blockAddr in MRFC522Reader .MIFARE_USERDATA :
43+ (status , backData , backBits ) = MRFC522Reader .authenticate (
44+ mode ,
45+ blockAddr ,
46+ key ,
47+ uid )
48+
49+ if (status == MRFC522Reader .MI_OK ):
50+ # Write new data to card
51+ data = random_data ()
52+ (status , backData , backBits ) = MRFC522Reader .write (
53+ blockAddr ,
54+ data )
55+ if (status == MRFC522Reader .MI_OK ):
56+ print ('Data ' ,blockAddr ,' ' , end = '' )
57+ blockAddrList .append (blockAddr )
58+ for i in range (0 ,16 ):
59+ print (data [i ]," " , end = '' )
60+ print ()
61+ else :
62+ print ('Error while writing new data' )
63+ continue_reading = False
64+
65+ else :
66+ print ('Authentication error' )
67+ print (blockAddrList )
68+ # Deauthenticate
69+ MRFC522Reader .deauthenticate ()
0 commit comments