2

I was wondering if there is a way to poll an ACR122U in python and if so how? My script below gets the UID of a card but continuously runs. I know it runs because of while1 but it shows what i want to achive

from smartcard.scard import *
from smartcard.util import     toHexString

def s():
 while 1:
  hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER)
  assert hresult==SCARD_S_SUCCESS
  hresult, readers = SCardListReaders(hcontext, [])
  assert len(readers)>0
  reader = readers[0]
  hresult, hcard, dwActiveProtocol = SCardConnect(
     hcontext,
     reader,
     SCARD_SHARE_SHARED,
     SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1)
  try:
   hresult, response = SCardTransmit(hcard,dwActiveProtocol,[0xFF,0xCA,0x00,0x00,0x04])
   uid = toHexString(response, format=0)
   print uid
  except SystemError:
   print "no card found"
s()
shaggs
  • 600
  • 7
  • 27
  • So what's your actual problem? – Michael Roland Apr 09 '16 at 09:55
  • Right now it just runs displaying not found or the uid over and over – shaggs Apr 09 '16 at 10:02
  • Yes, that's exactly what your code is supposed to do and this also pretty much matches the requirement that you specified ("*a way to poll an ACR122U in python*"). Thus, again, what's your actual problem? – Michael Roland Apr 09 '16 at 10:05
  • Aim is to scan card once and display UID then have it sit idel. Right now i scan card and get an endless display of uid once removed endless "no card found" so looking for a "if card present display uid" if not "sit and wait but check every x seccond" is that better? – shaggs Apr 09 '16 at 10:49

1 Answers1

1

Found it out after a bit more searching.

The op code is using PC/SC cammands and below uses APU commands.

cardmonitor = CardMonitor()
cardobserver = printout()
cardmonitor.addObserver(cardobserver)
#If no card in 20secs kill program (put in for testing)
sleep(20)
cardmonitor.deleteObserver(cardobserver)

So when blened together you get

class printobserver( CardObserver ):
    def update( self, observable, (addedcards, removedcards) ):
        for card in addedcards:
         if addedcards:
            hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER)
            assert hresult==SCARD_S_SUCCESS
            hresult, readers = SCardListReaders(hcontext, [])
            assert len(readers)>0
            reader = readers[0]
            hresult, hcard, dwActiveProtocol = SCardConnect(
             hcontext,
             reader,
             SCARD_SHARE_SHARED,
             SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1)
            hresult, response = SCardTransmit(hcard,dwActiveProtocol,[0xFF,0xCA,0x00,0x00,0x04])
            uid = toHexString(response, format=0)
            print response #cards ATR
            print uid #Cards UID

print "place card on reader"
while 1:
    cardmonitor = CardMonitor()
    cardobserver = printobserver()
    cardmonitor.addObserver( cardobserver )
    cardmonitor.deleteObserver( cardobserver )
    time.sleep( 2 )

Its not a neat way of doing it but it works.

shaggs
  • 600
  • 7
  • 27