2

我买了一个 NFC 读卡器 (ACS / ACR122U),我通过 USB 端口插入我的树莓派 3;我已经安装了 swig、pcsc-tools、pcscd、libpcsclite... pcscd 服务由 systemctl 启动并绑定到 pcscd.socket。启动时我可以看到读卡器,启动时nfc-scan-device也可以读取读卡器提供的卡nfc-list 然后我尝试使用 python3 和 pyscard 读取标签 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 doc 和大多数教程上找到的基本脚本......我们称之为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=[]

找到了读卡器,但我无法读取卡。transmit() 方法返回 [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 序列描述

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

有人知道我可以如何解决这个奇怪的问题吗?谢谢你的帮助。

4

1 回答 1

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)

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

于 2021-02-28T06:13:11.990 回答