我正在尝试使用 ACR122u NFC 阅读器与 NFC 标签进行通信。该程序启动良好,当一个标签连接时,它能够读取并执行所有所需的操作。但是,一旦 NFC 标签被移除,阅读器就会关闭,并且在程序重新启动之前不会打开。这是我用于轮询读者的代码:
def CheckNFCCard():
getUIDCommand = [0xFF, 0xCA, 0x00, 0x00, 0x00]
turnOffBeepCommand = [0xFF, 0x00, 0x52, 0x00, 0x00]
getDataCommand = [0xFF, 0xB0, 0x00, 0x07, 0x10]
updateBlockCommandFirst=[0xFF, 0xD6, 0x00, 0x07, 0x04, 0x65, 0x6E, 0x75, 0x73]
updateBlockCommandSecond=[0xFF, 0xD6, 0x00, 0x08, 0x04, 0x65, 0x64, 0xFE, 0x00]
#get NFC reader
r = readers()
reader = r[0]
#select reader, connect
conn = reader.createConnection()
while(True):
print('NFC routine restart')
try:
conn.connect()
except:
time.sleep(1)
continue
#send hex command to turn off beeps on scan.
conn.transmit(turnOffBeepCommand)
#get data encoded on tag.
data, sw1, sw2 = conn.transmit(getDataCommand)
if (sw1, sw2) == (0x90, 0x0):
print("Status: The operation completed.")
#Turn decimal numbers into characters
stringList = [chr(x) for x in data]
#
identifierString = ''.join(stringList)[2:6]
if (identifierString == "used"):
pub.sendMessage('NFCDetected', arg1='used')
elif (identifierString == "new_"):
data, sw1, sw2 = conn.transmit(updateBlockCommandFirst)
if (sw1, sw2) == (0x90, 0x0):
conn.transmit(updateBlockCommandSecond)
Thread(target=pub.sendMessage, args=('NFCDetected',), kwargs={'new' : arg1}).Start()
time.sleep(1)
continue
循环继续按预期运行,但阅读器已关闭并且不会再次连接。
任何帮助将非常感激。