I have an ACR38 reader and I use Python2.7. I searched for the ways to communicate with a reader using python and finally the below code found 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)
But when I run the code, I receive the below error :
>>> ================================ 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 ================================
Q: Why It failed to list readers while I have my reader in device manager under libusb-win32 devices? Should I remove Libusb?
The reason why you don't see the reader with your code is that you are mixing two approaches here:
libusb is a library for direct access to USB devices, so if you let libusb's USB driver manage the reader, you would also need to use the libusb interface library to access the reader from you program.
If you want to use pyscard (PC/SC), you would also need to let PC/SC manage the reader. In this case you need to install a CCID driver for that reader instead of the libusb driver. ACS provides a PC/SC driver package that contains the necessary drivers for that reader.