Pyscard 未能列出读者
Posted
技术标签:
【中文标题】Pyscard 未能列出读者【英文标题】:Pyscard failed to list readers 【发布时间】:2014-09-06 11:29:22 【问题描述】:我有一个 ACR38 阅读器,我使用 Python2.7。我搜索了使用python与读者交流的方法,最后找到了下面的代码here
import sys
sys.path.append('D:\PythonX\Lib\site-packages')
from smartcard.scard import *
import smartcard.util
SELECT = [0x00, 0xA4, 0x04, 0x00, 0x0A, 0xA0, 0x00, 0x00, 0x00, 0x62,
0x03, 0x01, 0x0C, 0x06, 0x01]
COMMAND = [0x00, 0x00, 0x00, 0x00]
try:
hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER)
if hresult != SCARD_S_SUCCESS:
raise Exception('Failed to establish context : ' +
SCardGetErrorMessage(hresult))
print 'Context established!'
try:
hresult, readers = SCardListReaders(hcontext, [])
if hresult != SCARD_S_SUCCESS:
raise Exception('Failed to list readers: ' +
SCardGetErrorMessage(hresult))
print 'PCSC Readers:', readers
if len(readers) < 1:
raise Exception('No smart card readers')
reader = readers[0]
print "Using reader:", reader
try:
hresult, hcard, dwActiveProtocol = SCardConnect(hcontext, reader,
SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1)
if hresult != SCARD_S_SUCCESS:
raise Exception('Unable to connect: ' +
SCardGetErrorMessage(hresult))
print 'Connected with active protocol', dwActiveProtocol
try:
hresult, response = SCardTransmit(hcard, dwActiveProtocol,
SELECT)
if hresult != SCARD_S_SUCCESS:
raise Exception('Failed to transmit: ' +
SCardGetErrorMessage(hresult))
print 'Select: ' + smartcard.util.toHexString(response,
smartcard.util.HEX)
hresult, response = SCardTransmit(hcard, dwActiveProtocol,
COMMAND)
if hresult != SCARD_S_SUCCESS:
raise Exception('Failed to transmit: ' +
SCardGetErrorMessage(hresult))
print 'Command: ' + smartcard.util.toHexString(response,
smartcard.util.HEX)
finally:
hresult = SCardDisconnect(hcard, SCARD_UNPOWER_CARD)
if hresult != SCARD_S_SUCCESS:
raise Exception('Failed to disconnect: ' +
SCardGetErrorMessage(hresult))
print 'Disconnected'
except Exception, message:
print "Exception:", message
finally:
hresult = SCardReleaseContext(hcontext)
if hresult != SCARD_S_SUCCESS:
raise Exception('Failed to release context: ' +
SCardGetErrorMessage(hresult))
print 'Released context.'
except Exception, message:
print "Exception:", message
import sys
if 'win32' == sys.platform:
print 'press Enter to continue'
sys.stdin.read(1)
但是当我运行代码时,我收到以下错误:
>>> ================================ RESTART ================================
>>>
Context established!
Released context.
Exception: Failed to list readers: Cannot find a smart card reader.
press Enter to continue
Traceback (most recent call last):
File "C:/Users/ghasemi.IT/Desktop/123", line 76, in <module>
sys.stdin.read(1)
AttributeError: read
>>> ================================ RESTART ================================
问:为什么我的阅读器在libusb-win32 设备下的设备管理器中没有列出阅读器?我应该删除 Libusb 吗?
【问题讨论】:
【参考方案1】:你的代码看不到读者的原因是你在这里混合了两种方法:
-
您的代码尝试使用 pyscard,它是 PC/SC 接口的 Python 包装器。
您让 libusb 管理 ACR38 USB 设备。
libusb 是一个用于直接访问 USB 设备的库,因此如果您让 libusb 的 USB 驱动程序管理阅读器,您还需要使用 libusb 接口库从您的程序中访问阅读器。
如果您想使用 pyscard (PC/SC),您还需要让 PC/SC 管理阅读器。在这种情况下,您需要为该阅读器安装 CCID 驱动程序而不是 libusb 驱动程序。 ACS 提供了一个PC/SC driver package,其中包含该阅读器所需的驱动程序。
【讨论】:
好教程:james-ridgway.co.uk/blog/installing-acr122u-drivers-on-linux以上是关于Pyscard 未能列出读者的主要内容,如果未能解决你的问题,请参考以下文章