创建NFC读卡python脚本

Posted inspirationplace

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了创建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(" ")
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读卡python脚本的主要内容,如果未能解决你的问题,请参考以下文章

NFC读卡APP

使用NFC读卡器读取银行卡信息

web调用IC卡读卡器开发第七章--NFC标签NDEF数据

NFC 读卡器和卡上匹配 (MOC) 兼容性

恢复出厂设置 ACR1255U-J1 NFC 读卡器

NFC移动支付说明[关闭]