LudovicRousseau / pyscard

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

UnicodeDecodeError #62

Closed scandale01 closed 6 years ago

scandale01 commented 6 years ago

Hello, I am using pyscard library in my NFC reader project. When I run, it give me error. I have tried your example code (link), but error is the same . Error log is here: F:\Python\Python36\python.exe "F:/My documents/My project/selectDF_TELECOM.py" insert a card (SIM card if possible) within 10s connecting to ACS ACR1252 CL Reader PICC 0

A0 A4 00 00 02 7F 10 Traceback (most recent call last): disconnecting from ACS ACR1252 CL Reader PICC 0 File "F:/My documents/My project/selectDF_TELECOM.py", line 51, in disconnecting from ACS ACR1252 CL Reader PICC 0 response, sw1, sw2 = cardservice.connection.transmit(apdu) File "F:\Python\Python36\lib\site-packages\smartcard\CardConnectionDecorator.py", line 82, in transmit return self.component.transmit(bytes, protocol) File "F:\Python\Python36\lib\site-packages\smartcard\CardConnection.py", line 146, in transmit data, sw1, sw2 = self.doTransmit(bytes, protocol) File "F:\Python\Python36\lib\site-packages\smartcard\pcsc\PCSCCardConnection.py", line 205, in doTransmit SCardGetErrorMessage(hresult)) File "F:\Python\Python36\lib\site-packages\smartcard\scard\scard.py", line 1278, in SCardGetErrorMessage return _scard.SCardGetErrorMessage(lErrCode) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcf in position 0: invalid continuation byte

Process finished with exit code 1

I have marked in debugging mode, that variable hresult is 31. hresult, response = SCardTransmit( self.hcard, pcscprotocolheader, bytes)

I am using: Windows 8.1 64 bit Python 3.6 pyscard 1.9.6 NFC tag: NXP NTAG213 NFC reader: ACR1252u sometimes ACM1252u

LudovicRousseau commented 6 years ago

It looks like a problem with SCardGetErrorMessage().

Try to execute this Python script:

import smartcard
print(smartcard.scard.SCardGetErrorMessage(0))
print(smartcard.scard.SCardGetErrorMessage(31))
scandale01 commented 6 years ago

Hello,

I have tried it, but seems that error is the same.

F:\Python\Python36\python.exe "F:/My documents/My project/forTesting.py" Traceback (most recent call last): File "F:/My documents/My project/forTesting.py", line 2, in print(smartcard.scard.SCardGetErrorMessage(0)) File "F:\Python\Python36\lib\site-packages\smartcard\scard\scard.py", line 1278, in SCardGetErrorMessage return _scard.SCardGetErrorMessage(lErrCode) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 0: invalid continuation byte

Process finished with exit code 1

LudovicRousseau commented 6 years ago

Can you try the same forTesting.py script with Python 2 instead of Python 3?

scandale01 commented 6 years ago

Yes, I have tried already, and it is working in Python 2. In first case error message is ""operation successfully finished". In second case "Connected to system device is not working"

So seems that, Python versions is the reason

LudovicRousseau commented 6 years ago

I think I fixed the problem. Try the version available from https://ci.appveyor.com/project/LudovicRousseau/pyscard

scandale01 commented 6 years ago

Hello, Yes, now project code is working. Thank you very much for your attention and time! Really, thank you!

LudovicRousseau commented 6 years ago

Thanks for the feedback.

scandale01 commented 6 years ago

Hello, I changed language of my operation system (native was Russian) and tried to run code. The encoding error was caused by cyrillic symbols in error message.

smartcard.Exceptions.CardConnectionException: Failed to transmit with protocol T1. **A device attached to the system is not functioning.** Bold text was in russian and Python 2.7 could handle it properly. So this still needs some attention on Py3.6.

Now the real issue is the fact that example selectDF_TELECOM.py runs flawlessy on macOS and Linux, but not on Windows 8.1 (64 bit). Console output follows:

F:\Python\Python36\python.exe "F:/My documents/selectDF_TELECOM.py"
insert a card (SIM card if possible) within 10s
connecting to ACS ACR1252 CL Reader PICC 0
> A0 A4 00 00 02 7F 10
Traceback (most recent call last):
  File "F:/My documents/selectDF_TELECOM.py", line 51, in <module>
    response, sw1, sw2 = cardservice.connection.transmit(apdu)
  File "F:\Python\Python36\lib\site-packages\smartcard\CardConnectionDecorator.py", line 82, in transmit
    return self.component.transmit(bytes, protocol)
  File "F:\Python\Python36\lib\site-packages\smartcard\CardConnection.py", line 146, in transmit
    data, sw1, sw2 = self.doTransmit(bytes, protocol)
  File "F:\Python\Python36\lib\site-packages\smartcard\pcsc\PCSCCardConnection.py", line 205, in doTransmit
    SCardGetErrorMessage(hresult))
smartcard.Exceptions.CardConnectionException: Failed to transmit with protocol T1. **A device attached to the system is not functioning**. 
disconnecting from ACS ACR1252 CL Reader PICC 0
disconnecting from ACS ACR1252 CL Reader PICC 0

Process finished with exit code 1

So this is exactly the same error as in first issue report, before it just couldn't print the reason. Does it have something to do with drivers of reader or still something on library side? Used driver: Advanced Card Systems Ltd. ver: 4.2.6.0 (according to windows cardreader properties) from . installer itself claims its version 4.2.6.1.

When I rolled back the driver, it changed to Microsoft's driver, released 21.06.2006, ver 6.3.9600.17415 and gave following traceback:

File "F:/My documents/NASYS/BarToNFC/selectDF_TELECOM.py", line 51, in <module>
    response, sw1, sw2 = cardservice.connection.transmit(apdu)
  File "F:\Python\Python36\lib\site-packages\smartcard\CardConnectionDecorator.py", line 82, in transmit
    return self.component.transmit(bytes, protocol)
  File "F:\Python\Python36\lib\site-packages\smartcard\CardConnection.py", line 146, in transmit
    data, sw1, sw2 = self.doTransmit(bytes, protocol)
  File "F:\Python\Python36\lib\site-packages\smartcard\pcsc\PCSCCardConnection.py", line 205, in doTransmit
    SCardGetErrorMessage(hresult))
smartcard.Exceptions.CardConnectionException: Failed to transmit with protocol T1. **The I/O operation has been aborted because of either a thread exit or an application request**. 
LudovicRousseau commented 6 years ago

The error is reported by the PC/SC layer. I don't think it is a problem in PySCard. You can try with another reader.