python-2.7smartcard-readercontactless-smartcardpyscard

How to get ATR of a smard card from HID omnilkey


I want to get ATR of the smartcard. I am using HID omnikey 5321. I am following this link "http://pyscard.sourceforge.net/user-guide.html#requesting-any-card"

so far i have tried:

>>>from smartcard.CardType import AnyCardType
>>>from smartcard.CardRequest import CardRequest
>>>from smartcard.util import toHexString
>>>
>>> cardtype = AnyCardType()
>>> cardrequest = CardRequest( timeout=1, cardType=cardtype )
>>> cardservice = cardrequest.waitforcard() 
>>>
>>>>>> cardservice.connection.connect()

i am getting error at the

cardservice.connection.connect() 

error like:

raise CardConnectionException('Unable to connect with ptotocol[pcscprotocol] + . '+ScardGetErrorMessage(hresult) 
CardConnectionException: Unable to conenct the card with T0 or T1 . Card is not responding to reset.

Solution

  • Because You dont specify the reader to connect:

    r=readers()
    #r[Number of reader list].
    cardservice.connection = r[0].createConnection()
    cardservice.connection.connect()
    

    A simple Example:

    from __future__ import print_function
    from smartcard.Exceptions import NoCardException
    from smartcard.System import readers
    from smartcard.util import toHexString
    
    for reader in readers():
        try:
             connection = reader.createConnection()
             connection.connect()
             print(reader, toHexString(connection.getATR()))
        except NoCardException:
             print(reader, 'no card inserted')
    
    import sys
    if 'win32' == sys.platform:
        print('press Enter to continue')
        sys.stdin.read(1)
    

    -Another Selecting Reader:

    from __future__ import print_function
    from smartcard.Exceptions import NoCardException
    from smartcard.System import readers
    from smartcard.util import toHexString
    from smartcard.CardType import AnyCardType
    from smartcard.CardRequest import CardRequest
    
    cardtype = AnyCardType()
    r=readers()
    cardrequest = CardRequest( timeout=10, cardType=cardtype )
    cardservice = cardrequest.waitforcard()
    print('Available Readers:')
    for i in range(len(readers())):
        print('[',i+1,']',r[i])
    if(len(readers()) < 1):
        print("\nNO AVAILABLE READERS!\n")
    else:
        print("Select you Reader: (Ctrl+C to Exit)")
        my_input = input()
        selectReader = clamp(int(my_input)-1,0,len(readers()))
        print('Selected: ',r[selectReader])
        cardservice.connection = r[selectReader].createConnection()
        cardservice.connection.connect()
        try:
            print('Card ATR:',toHexString(cardservice.connection.getATR()),file=f)
        except:
            print("Cant not Get ATR")
    

    .

    Full Information:

    https://pyscard.sourceforge.io/pyscard-framework.html#framework-samples

    https://github.com/LudovicRousseau/pyscard

    https://pyscard.sourceforge.io/user-guide.html