LudovicRousseau / pyscard

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

Exception ListReadersException raised after disconnecting PCSC reader #13

Closed mayito closed 7 years ago

mayito commented 9 years ago

Hi,

I've noticed pyscard is not able to maintain the list of readers when a reader is connected / disconnected to / from my computer which runs on Windows 8.1 (64 bits).

I've written the following piece of code from the documentation to see if connections and disconnections of the reader are well detected :

from sys import stdin, exc_info
from time import sleep
from smartcard.ReaderMonitoring import ReaderMonitor, ReaderObserver

class printobserver( ReaderObserver ):
    def update( self, observable, (addedreaders, removedreaders) ):
        print "Added readers", addedreaders
        print "Removed readers", removedreaders

if __name__ == "__main__":
    try:
        print "Add or remove a smartcard reader to the system."
        print "This program will exit in 60 seconds"
        print ""
        readermonitor = ReaderMonitor()
        readerobserver = printobserver()
        readermonitor.addObserver( readerobserver )

        sleep(60)
    except error:
        print exc_info()[0], ': ', exc_info()[1]
    finally : 
        readermonitor.deleteObserver(readerobserver)

If the reader is connected before launching the program, it is well detected. But if it is connected / deconnected during the observer loop a ListReadersException is raised.

To avoid the problem, is there a way to force the reload of the entire pyscard module on demand ?

Cheers,

Thierry.

LudovicRousseau commented 8 years ago

Your sample code works fine for me on Mac OS X Yosemite.

I do not use Windows myself. If you can propose a patch for Windows that would help a lot.

Koudy commented 8 years ago

@mayito, hello. Did you find a solution?

LudovicRousseau commented 8 years ago

Maybe the change in #22 could help.

LudovicRousseau commented 7 years ago

I guess that #22 fixed the problem for you since I got no feedback. Closing.

leileigong commented 5 years ago

I use pyscard-1.9.17, but i got this problem too. I guess that #22 do not fixed the problem.

Traceback (most recent call last): File "C:\Users\lei.virtualenvs\TestTool-gkAj8wo0\lib\site-packages\smartcard\CardMonitoring.py", line 162, in run currentcards = self.cardrequest.waitforcardevent() File "C:\Users\lei.virtualenvs\TestTool-gkAj8wo0\lib\site-packages\smartcard\CardRequest.py", line 73, in waitforcardevent return self.pcsccardrequest.waitforcardevent() File "C:\Users\lei.virtualenvs\TestTool-gkAj8wo0\lib\site-packages\smartcard\pcsc\PCSCCardRequest.py", line 288, in waitforcardevent readernames = self.getReaderNames() File "C:\Users\lei.virtualenvs\TestTool-gkAj8wo0\lib\site-packages\smartcard\pcsc\PCSCCardRequest.py", line 90, in getReaderNames raise ListReadersException(hresult) ListReadersException: ('Failed to list readers', -2146435042L)