无法通过Python使用NFC / RFID读取器读取卡

时间:2019-05-22 16:20:16

标签: python nfc pcsc acr122 pyscard

我已经购买了NFC读卡器(ACS / ACR122U),并通过USB端口插入了树莓3;我已经安装了Swig,pcsc-tools,pcscd,libpcsclite ... pcscd服务由systemctl启动,并绑定到pcscd.socket。 我启动nfc-scan-device时可以看到阅读器,也可以阅读nfc-list时阅读器随附的卡 然后,我尝试使用python3和pyscard读取tag-Id,但它不起作用。 我可以看到阅读器并启动连接,而没有任何错误消息,但是无法读取tagID。

sudo nfc-list 返回:

>nfc-list uses libnfc 1.7.1
>NFC device: ACS / ACR122U PICC Interface opened
>1 ISO14443A passive target(s) found:
>ISO/IEC 14443A (106 kbps) target:
>ATQA (SENS_RES): 00  04  
>UID (NFCID1): d6  71  c5  f0  
>SAK (SEL_RES): 08`  

因此可以访问NFC读卡器,我什至可以获取卡的标签ID

然后我尝试在python上使用它 我在那里找到了一些信息: https://pyscard.sourceforge.io/user-guide.html https://pyscard.sourceforge.io/epydoc/smartcard-module.html

我首先在pipenv环境中尝试它,但是没有用。 然后,我删除了pipenv,以确保它不在关键路径上……

第一次测试

我尝试了可以​​在pyscard文档上找到的基本脚本以及大多数教程...我们将其称为 nfcReader-1.py

from smartcard.System import readers
import sys

getuid=[0xFF, 0xCA, 0x00, 0x00, 0x00]
r = readers()
if len(r) < 1:
  print("error: No readers available!")
  sys.exit()

print("Available readers: ", r)
reader = r[0]
print("Using: ", reader)
conn = reader.createConnection()
conn.connect()
data, sw1, sw2 = conn.transmit(getuid)
if (sw1, sw2) == (0x90, 0x0):
  print("Status: The operation completed successfully.")
elif (sw1, sw2) == (0x63, 0x0):
  print("Status: The operation failed.")

print("uid={}".format(data))
conn=disconnect()
r=None  #to prevent error messages when calling sys.exit() below
sys.exit()

>Available readers:  ['ACS ACR122U PICC Interface 00 00']
>Using:  ACS ACR122U PICC Interface 00 00
>Status: The operation failed.
>uid=[]

找到了读取器,但我无法读取卡。 send()方法返回[0x63,x0x00]([99,00]),这意味着发生了奇怪的事情 当我在python命令行中键入每个命令时,它都不起作用

第二次测试

我也尝试另一种方式。我在pyscard文档中找到了第二种方法,但结果完全一样:

from smartcard.CardType import AnyCardType
from smartcard.CardRequest import CardRequest
from smartcard.CardConnection import CardConnection
from smartcard.util import toHexString

import sys
getuid=[0xFF, 0xCA, 0x00, 0x00, 0x00]
act = AnyCardType()
cr = CardRequest( timeout=10, cardType=act )
cs = cr.waitforcard()
cs.connection.connect()

print(toHexString( cs.connection.getATR() ))
print(cs.connection.getReader())
data, sw1, sw2 = cs.connection.transmit( cmdmap['getuid'] )
if (sw1, sw2) == (0x90, 0x0):
  print("Status: The operation completed successfully.")
elif (sw1, sw2) == (0x63, 0x0):
  print("Status: The operation failed.")

print("uid={}".format(data))
cs=None  #to prevent error message when calling sys.exit() below
sys.exit()

响应没有不同:

>3B 00
>ACS ACR122U PICC Interface 00 00
>Status: The operation failed.
>uid=[]

我使用的命令是[0xFF,0xCA,0x00,0x00,0x00],这似乎是获取tagid的命令。 ACS阅读器文档(https://www.acs.com.hk/en/download-manual/419/API-ACR122U-2.04.pdf)确认这是正确的顺序。  ACS tagid sequence description

至少,我尝试使用 sudo 启动python脚本,以防出现问题。并且我将套接字文件夹(/var/run/pcscd/pcscd.comm)的权限更改为777 ...以防万一

有人对我解决这个奇怪问题的方法有任何想法吗? 谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我在使用 NFC 读卡器 (ACS / ACR122U) 时遇到了同样的问题,我只想连接到 Windows 10 上的 PC 以读取保存在客户卡上的 ID。我设法通过替换来解决它:

  • data, sw1, sw2 = cs.connection.transmit( cmdmap['getuid'] )

  • data, sw1, sw2 = cs.connection.transmit(getuid)

原因是你还没有定义cmdmap,只是简单的读取数据,甚至没有必要。

在此之后我想转换数据,一个解决方案是:

  • data = toHexString(data)

请注意,在我的情况下,这些数据的顺序相反,但这是一个简单的解决方案。