创建NFC读卡python脚本
作者:互联网
读卡器:ACR122U,主机环境:ubuntu 20.00
1.apt install libnfc-bin
2.apt install libpcsclite1
3.apt install vim
4.vi /etc/modprobe.d/blacklist-libnfc.conf
在文件尾增加:
blacklist pn533
blacklist pn533_usb
blacklist nfc
5.获取nfc-tools 1.8.0(因为ACR122U在1.7.1上有bug)
github:https://github.com/nfc-tools/libnfc/releases/tag/libnfc-1.8.0
download:https://github.com/nfc-tools/libnfc/releases/download/libnfc-1.8.0/libnfc-1.8.0.tar.bz2
unzip and compile:
tar xjf libnfc-1.8.0.tar.bz2
cd libnfc-1.8.0
./configure --with-drivers=acr122s,acr122_pcsc,acr122_usb --sysconfdir=/etc --prefix=/usr
make
makeinstall
6.try it:nfc-list
7.编写一段python代码,获取UID:
`import time
import os
def execCmd(cmd):
ret = os.popen(cmd)
msg = ret.read()
ret.close()
return msg
def saveCardUIDtoFile(str:str):
file = open(file = "/UID/cardUID",mode = "w")
file.write(str)
file.close()
cmd = "nfc-list"
alreadyDetected = False
findCount = 0
while(True):
ret = execCmd(cmd)
ret = ret.split("\n")
findCount = 0
for element in ret:
if element.find("UID") != -1:
findCount += 1
if alreadyDetected == False:
alreadyDetected = True
print("NFC tag is detected.")
UIDList = element.split(" ")
UID = UIDList[9] + UIDList[11] + UIDList[13] + UIDList[15]
saveCardUIDtoFile(UID)
print("UID is: %r" % UID)
if findCount == 0:
if alreadyDetected == True:
alreadyDetected = False
print("NFC tag is removed.")
saveCardUIDtoFile("")
`
8.enjoy
标签:NFC,UID,python,libnfc,1.8,ret,读卡,alreadyDetected,nfc 来源: https://www.cnblogs.com/InspirationPlace/p/13958498.html