Search code examples
pythonsmartcardrfidsmartcard-readercontactless-smartcard

Read an unique ID from RFID card in Python


I have an OmniKey 5321 SmartCard reader, and a PC with Debian. The following Python code reads the cards:

from smartcard.CardMonitoring import CardMonitor, CardObserver
from smartcard.util import *
import sys

class printobserver( CardObserver ):
    def update( self, observable, (addedcards, removedcards) ):
        for card in addedcards:
            print "+Inserted: ", toHexString( card.atr )
        for card in removedcards:
            print "-Removed:  ", toHexString( card.atr )

try:
    print "Insert or remove a smartcard in the system."
    print ""
    cardmonitor = CardMonitor()
    cardobserver = printobserver()
    cardmonitor.addObserver( cardobserver )
    raw_input('Press Enter to exit\n')
except:
    print "Unexpected error: ", sys.exc_info()[0]

The Python code runs fine, but gives me back the same atr tag for many cards. The given atr is:

3B 8F 80 01 80 4F 0C A0 00 00 03 06 03 00 01 00 00 00 00 6A

How can I read something uniqe from the cards? I've understand that I have to send some commands, and the replies will contains the UID, but how can I find the commands? How will I send them?

Thanks!


Solution

  • Once you connected to the card (or actually the reader), you can send the PC/SC standardized command for getting the card UID from the reader:

    FF CA 00 00 00