-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnfc_reader.py
53 lines (40 loc) · 1.63 KB
/
nfc_reader.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
import time
import binascii
from pn532pi import Pn532, pn532
from pn532pi import Pn532I2c
from pn532pi import Pn532Spi
from pn532pi import Pn532Hsu
PN532_I2C = Pn532I2c(1)
nfc = Pn532(PN532_I2C)
def nfc_setup():
nfc.begin()
versiondata = nfc.getFirmwareVersion()
if not versiondata:
print("Didn't find PN53x board")
raise RuntimeError("Didn't find PN53x board") # halt
# Got ok data, print it out!
print("Found chip PN5 {:#x} Firmware ver. {:d}.{:d}".format((versiondata >> 24) & 0xFF, (versiondata >> 16) & 0xFF,
(versiondata >> 8) & 0xFF))
# Set the max number of retry attempts to read from a card
# This prevents us from waiting forever for a card, which is
# the default behaviour of the pn532.
nfc.setPassiveActivationRetries(0xFF)
# configure board to read RFID tags
nfc.SAMConfig()
print("Waiting for an ISO14443A card")
def nfc_loop():
# Wait for an ISO14443A type cards (Mifare, etc.). When one is found
# 'uid' will be populated with the UID, and uidLength will indicate
# if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
success, uid = nfc.readPassiveTargetID(pn532.PN532_MIFARE_ISO14443A_106KBPS)
if (success):
print("Found a card!")
print("UID Length: {:d}".format(len(uid)))
print("UID Value: {}".format(binascii.hexlify(uid)))
# Wait 1 second before continuing
time.sleep(1)
return uid
else:
# pn532 probably timed out waiting for a card
print("Timed out waiting for a card")
return False