ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

创建NFC读卡python脚本

2020-11-11 14:32:36  阅读:827  来源: 互联网

标签:NFC UID python libnfc 1.8 ret 读卡 alreadyDetected nfc


读卡器: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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有