我想知道是否有办法在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 )
这不是一种巧妙的方法,但它很有效。