Python ACR122U 投票

Python ACR122U Poll

我想知道是否有办法在 python 中轮询 ACR122U,如果有的话,怎么做?我下面的脚本获取卡的 UID 但持续运行。我知道它运行是因为 while1 但它显示了我想要实现的目标

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()

多找了几下才找到。

操作码使用PC/SC命令,下面使用APU命令。

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

所以当混合在一起时你得到

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 )

这不是一个很好的方法,但它确实有效。