开启SPI通讯
sudo raspi-config
找到SPi打开。
安装SPI模块
git clone https://github.com/lthiery/SPI-Py
cd SPI-Py
sudo python3 setup.py install
安装RFID模块
pip3 install pi-rc522
或者sudo pip3 install pi-rc522
获取UID
#!/usr/bin/python3
from pirc522 import RFID
rdr = RFID()
while True:
rdr.wait_for_tag()
(error, tag_type) = rdr.request()
if not error:
print("Tag detected")
(error, uid) = rdr.anticoll()
if not error:
print("UID: " + str(uid))
# Select Tag is required before Auth
if not rdr.select_tag(uid):
# Auth for block 10 (block 2 of sector 2) using default shipping key A
if not rdr.card_auth(rdr.auth_a, 10, [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF], uid):
# This will print something like (False, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
print("Reading block 10: " + str(rdr.read(10)))
# Always stop crypto1 when done working
rdr.stop_crypto()
# Calls GPIO cleanup
rdr.cleanup()