我正在使用 Python 编写一个程序,该程序使用 tkinter 识别 Mifare Card 并将值写入卡。
我目前的程序很少,但效果很好。但是,在更新卡上的二进制块时,它会间歇性地失败。失败后,所有二进制块都更改为 0 或 255。
我无法理解这种现象。
读卡器出现故障,但卡已更换。
我想消除症状的原因和症状。帮助。
附上源码。
- 失败时
连接到 ACS ACR122U 00 00
FF CA 00 00 04 - uid 获取
DB 96 AE 52 90 00
FF 82 00 00 06 05 00 00 00 00 00 - 加载密钥
[] 90 00 - 成功加载密钥
FF 86 00 00 05 01 00 00 60 00 - 认证
[] 90 00 - 认证成功
FF B0 00 01 10 - 数字 1 二进制块调用
00 00 00 00 40 00 00 00 AA 55 00 00 00 40 00 00 90 00 - 获取数字 1 二进制块
---------------------- 更新二进制块之前------------ --
二进制块前 >> [0, 0, 0, 0, 64, 0, 0, 0, 170, 85, 0, 0, 0, 64, 0, 0]
在 send_byte >> [0, 0, 0, 0, 64, 0, 0, 0, 170, 85, 0, 0, 0, 64, 0, 0] 之前
FF D6 00 01 10 00 00 00 00 40 00 00 00 AA 55 00 00 00 40 00 00 - 数字 1 二进制块更新
[] 63 00 - 失败
----------------------更新失败二进制块---------- --
发送字节>>[0, 0, 0, 0, 64, 0, 0, 0, 170, 85, 0, 0, 0, 64, 0, 0]
FF 00 40 00 04 01 00 01 01 蜂鸣器控制
[] 90 00 - 蜂鸣器控制成功
FF 00 40 00 04 01 00 01 01 蜂鸣器控制
[] 90 00 - 蜂鸣器控制成功
FF B0 00 01 10 - 数字 1 二进制块调用
[] 63 00 - 失败
从 ACS ACR122U 断开 00 00
- 失败后
连接到 ACS ACR122U 00 00
FF CA 00 00 04 - uid 获取
DB 96 AE 52 90 00
FF 82 00 00 06 05 00 00 00 00 00 - 加载密钥
[] 90 00 - 成功加载密钥
FF 86 00 00 05 01 00 00 60 00 - 认证
[] 90 00 - 认证成功
FF B0 00 01 10 - 数字 1 二进制块调用
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 90 00 - 获取数字 1 二进制块
def rf_thread(self, second=1.0):
rf_test()
_rf_thread = threading.Timer(second, rf_thread)
_rf_thread.daemon = True
_rf_thread.start()
def rf_test():
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)
# get uid
try:
hresult, uid = SCardTransmit(hcard, dwActiveProtocol, [0xFF, 0xCA, 0x00, 0x00, 0x04])
key = "00000000"
#load key
try:
SELECT = [0xFF, 0x82, 0x00, 0x00, 0x06]
DF_TELECOM = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
hresult, response = SCardTransmit(hcard, SCARD_PCI_T1, SELECT + DF_TELECOM)
if hresult != SCARD_S_SUCCESS:
print(str(SCardGetErrorMessage(hresult))
return []
SELECT = [0xFF, 0x86, 0x00, 0x00, 0x05]
DF_TELECOM = [0x01, 0x00, 0x00, 0x60, 0x00]
#authentication
try:
hresult, response = SCardTransmit(hcard, SCARD_PCI_T1, SELECT + DF_TELECOM)
if hresult != SCARD_S_SUCCESS:
return []
# Number 1 binary block get
req_byte = [0xFF, 0xB0, 0x00, 0x01, 0x10]
hresult, result_money = SCardControl(hcard, SCARD_CTL_CODE(3500), req_byte)
# Number 1 binary block update
SELECT = [0xFF, 0xD6, 0x00, 0x01, 0x10]
DF_TELECOM = [0, 0, 0, 0, 64, 0, 0, 0, 170, 85, 0, 0, 0, 64, 0, 0]
hresult, response = SCardTransmit(hcard, SCARD_PCI_T1,
SELECT + DF_TELECOM)
if hresult == SCARD_S_SUCCESS:
if response[0] == 144:
# some success code
else:
print("------------------------update fail binary block------------------------")
print("send byte>>" + str(SELECT + DF_TELECOM))
print("-----------------------------------------------------------")
try:
req_byte = [0xFF, 0x00, 0x40, 0x00, 0x04, 0x01, 0x00, 0x01,0x01]
hresult, response = SCardControl(hcard, SCARD_CTL_CODE(3500), req_byte)
hresult, response = SCardControl(hcard, SCARD_CTL_CODE(3500),req_byte)
except Exception as e:
print(e)
# Number 1 binary block get
res_req_byte = [0xFF, 0xB0, 0x00, 0x01, 0x10]
try:
hresult, response1 = SCardControl(hcard, SCARD_CTL_CODE(3500), res_req_byte)
print("after binary >>" + str(response1))
except Exception as e:
print(e)
except Exception as e:
print("authentication except >>" + str(e))
except Exception as e:
print("load key except >> " + str(e))
except SystemError:
print("no card found")
except Exception as e:
print("uid err" + str(e))
finally:
hresult = SCardReleaseContext(hcontext)
if hresult != SCARD_S_SUCCESS:
print('Failed to release context: ' +SCardGetErrorMessage(hresult))
更新块后预期的结果应该没有变化,但会间歇性地更改为 0 或 255。