2016-04-09 134 views
2

我想知道是否有方法來輪詢ACR122U在Python中,如果是的話如何?下面我的腳本獲取一張卡的UID,但不斷運行。我知道它運行,因爲while1但它說明了什麼,我想才達到Python ACR122U投票

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

那麼你的實際問題是什麼? –

+0

現在它只是運行顯示找不到或uid一遍又一遍 – shaggs

+0

是的,這正是你的代碼應該做的,這也非常符合你指定的要求(「*在Python *中輪詢ACR122U的方法* 「)。因此,再一次,你的實際問題是什麼? –

回答

1

發現了這件事後,多一點搜索。

該操作碼正在使用PC/SC cammands及以下使用APU命令。

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

所以當blened在一起你

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) 

它沒有這樣做的一個整潔的方式,但它的工作原理。