-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_uid.py
More file actions
52 lines (39 loc) · 1.23 KB
/
read_uid.py
File metadata and controls
52 lines (39 loc) · 1.23 KB
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
import time
from smartcard.System import readers
from smartcard.Exceptions import NoCardException, CardConnectionException
# Find PICC reader
reader_list = readers()
print("Readers:", reader_list)
picc_reader = None
for r in reader_list:
if "PICC" in str(r):
picc_reader = r
break
if picc_reader is None:
print("No PICC reader found!")
exit()
connection = picc_reader.createConnection()
last_uid = None
print("Waiting for NFC cards... Place a card on the reader.\n")
while True:
try:
# Try to connect
connection.connect()
GET_UID = [0xFF, 0xCA, 0x00, 0x00, 0x00]
response, sw1, sw2 = connection.transmit(GET_UID)
if sw1 == 0x90 and sw2 == 0x00:
uid = "".join("{:02X}".format(x) for x in response)
# Only print when UID changes
if uid != last_uid:
print("Card detected! UID:", uid)
last_uid = uid
else:
last_uid = None
except NoCardException:
# Card removed → reset state
if last_uid is not None:
print("Card removed.")
last_uid = None
except CardConnectionException:
last_uid = None
time.sleep(0.2) # prevent CPU overload