Python ACR122U民意调查

时间:2016-04-09 01:59:54

标签: python-2.7 rfid acr122 pyscard

我想知道是否有办法在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()

1 个答案:

答案 0 :(得分:1)

经过一番搜索后发现它。

操作码使用 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 )

这不是一种干净利落的方式,但它有效。