LudovicRousseau / pyscard

pyscard smartcard library for python
http://pyscard.sourceforge.net/
GNU Lesser General Public License v2.1
383 stars 110 forks source link

CardConnectionException: Card not connected or Failed to transmit with protocol T0. Card protocol mismatch. #70

Closed tomekit closed 5 years ago

tomekit commented 5 years ago

I've got the ACR122U reader connected to Ubuntu 18.10 machine. I am using RFIDIOt sample scripts which do use the: pyscard. For instance cardselect.py: https://github.com/AdamLaurie/RFIDIOt/blob/master/cardselect.py

Everything worked fine until it stopped same day it was all configured.

I am getting the immediate exception (in case card is not present):

07:50 $ ./cardselect.py 
Traceback (most recent call last):
  File "./cardselect.py", line 25, in <module>
    import rfidiot
  File "/usr/local/lib/python2.7/dist-packages/rfidiot/__init__.py", line 194, in <module>
    card= RFIDIOt.rfidiot(readernum,readertype,line,speed,timeout,rfidiotglobals.Debug,noinit,nfcreader)
  File "/usr/local/lib/python2.7/dist-packages/rfidiot/RFIDIOt.py", line 161, in __init__
    self.acs_set_retry(to)
  File "/usr/local/lib/python2.7/dist-packages/rfidiot/RFIDIOt.py", line 1320, in acs_set_retry
    return self.acs_send_apdu(self.PCSC_APDU['ACS_SET_RETRY'])
  File "/usr/local/lib/python2.7/dist-packages/rfidiot/RFIDIOt.py", line 1187, in acs_send_apdu
    result, sw1, sw2= self.acs_transmit_apdu(apduout)
  File "/usr/local/lib/python2.7/dist-packages/rfidiot/RFIDIOt.py", line 1211, in acs_transmit_apdu
    result, sw1, sw2= self.pcsc_connection.transmit(apdu,protocol= self.pcsc_protocol)
  File "/usr/local/lib/python2.7/dist-packages/smartcard/CardConnectionDecorator.py", line 82, in transmit
    return self.component.transmit(bytes, protocol)
  File "/usr/local/lib/python2.7/dist-packages/smartcard/CardConnection.py", line 146, in transmit
    data, sw1, sw2 = self.doTransmit(bytes, protocol)
  File "/usr/local/lib/python2.7/dist-packages/smartcard/pcsc/PCSCCardConnection.py", line 198, in doTransmit
    raise CardConnectionException('Card not connected')
smartcard.Exceptions.CardConnectionException: Card not connected

or immediate exception when card is present:

07:51 $ ./cardselect.py 
Traceback (most recent call last):
  File "./cardselect.py", line 25, in <module>
    import rfidiot
  File "/usr/local/lib/python2.7/dist-packages/rfidiot/__init__.py", line 194, in <module>
    card= RFIDIOt.rfidiot(readernum,readertype,line,speed,timeout,rfidiotglobals.Debug,noinit,nfcreader)
  File "/usr/local/lib/python2.7/dist-packages/rfidiot/RFIDIOt.py", line 161, in __init__
    self.acs_set_retry(to)
  File "/usr/local/lib/python2.7/dist-packages/rfidiot/RFIDIOt.py", line 1320, in acs_set_retry
    return self.acs_send_apdu(self.PCSC_APDU['ACS_SET_RETRY'])
  File "/usr/local/lib/python2.7/dist-packages/rfidiot/RFIDIOt.py", line 1187, in acs_send_apdu
    result, sw1, sw2= self.acs_transmit_apdu(apduout)
  File "/usr/local/lib/python2.7/dist-packages/rfidiot/RFIDIOt.py", line 1211, in acs_transmit_apdu
    result, sw1, sw2= self.pcsc_connection.transmit(apdu,protocol= self.pcsc_protocol)
  File "/usr/local/lib/python2.7/dist-packages/smartcard/CardConnectionDecorator.py", line 82, in transmit
    return self.component.transmit(bytes, protocol)
  File "/usr/local/lib/python2.7/dist-packages/smartcard/CardConnection.py", line 146, in transmit
    data, sw1, sw2 = self.doTransmit(bytes, protocol)
  File "/usr/local/lib/python2.7/dist-packages/smartcard/pcsc/PCSCCardConnection.py", line 205, in doTransmit
    SCardGetErrorMessage(hresult))
smartcard.Exceptions.CardConnectionException: Failed to transmit with protocol T0. Card protocol mismatch.

My setup is mostly this:

sudo apt-get install libccid pcscd libpcsclite-dev libpcsclite1 pcsc-tools
# Install official drivers
sudo dpkg -i libacsccid1_1.1.6-1~ubuntu18.10.1_amd64.deb

sudo modprobe -r pn533_usb
sudo service pcscd restart

sudo pip install pyscard

# Then build the the RFIDIOt etc

After this the: pcsc_scan works perfectly fine.

Any idea what I am doing wrong? Thanks !

tomekit commented 5 years ago

Right, so it looks like that uninstalling the official drivers: sudo dpkg -r libacsccid Removing things from blacklist and restarting the: pcscd service solved the problem for me.

roaldnefs commented 5 years ago

I'm currently running into the same error: smartcard.Exceptions.CardConnectionException: Failed to transmit with protocol T0. Card protocol mismatch. on Ubuntu 19.04.

Fixed it by uninstalling the official ACS drivers and restarting the pcscd service, e.g.:

# List the current installed drivers
$ dpkg --get-selections | grep libacsccid
libacsccid1

# Remove the driver
$ sudo dpkg -r libacsccid1

# Restart the pcscd service
$ sudo service pcscd restart
LudovicRousseau commented 5 years ago

Interesting to note that my CCID driver https://github.com/LudovicRousseau/CCID works better than the fork from ACS https://github.com/acshk/acsccid with an ACS ACR122U reader :-)

@tomekit & @roaldnefs I suggest you to report the problem to https://github.com/acshk/acsccid so that @godfreychung can have a look at the issue.

ramymagdy-rm commented 4 years ago

Hi, I have almost the same problem, the reader is ACM1252SM rev 1.00C I've installed the driver for MacOS from ACS, and I'm using pyscard module also,

for some reason on older installation, there's no protocol problem but on newer one there's it's same MacOS "mojave "and pyscard versions "1.9.9" but different other libraries such as libusb, ndeflib, ndef, swig, pkg-config, pcsc-lite

exception: Failed to transmit with protocol T0 probably because of parameter value hresult = -2146434967

LudovicRousseau commented 4 years ago

@ramymagdy-rm you should report the problem to ACS if you use their driver. Maybe they know how to debug the situation with the ACM1252SM reader.

WilmerSuarez commented 4 years ago

I'm having a similar issue using the ACR122U on macOS Catalina. I'm using the pyscard library

smartcard.Exceptions.CardConnectionException: Failed to transmit with protocol T1. Transaction failed.

This occurs when transmitting ~40 bytes; but I don't see that error when transmitting smaller commands. Not sure what that means or what to do. I'm not using any of the drivers from ACS.

LudovicRousseau commented 4 years ago

Same answer as before: report the problem to ACS. The problem is not with pyscard but within a lower layer (the ACS driver?).

fananimi commented 4 years ago

Interesting to note that my CCID driver https://github.com/LudovicRousseau/CCID works better than the fork from ACS https://github.com/acshk/acsccid with an ACS ACR122U reader :-)

@tomekit & @roaldnefs I suggest you to report the problem to https://github.com/acshk/acsccid so that @godfreychung can have a look at the issue.

Very Interesting @LudovicRousseau , I already install ccid by ACR, can you explain how to uninstall them and replace by yours ?

Thanks.