I'm working on a program in Python that recognizes the Mifare Card with tkinter and writes the value to the card.
My current program is scarce but works well. However, it fails intermittently when updating the binary block on the card. After failure, all binary blocks are changed to 0 or 255.
I can not understand the phenomenon.
The reader has failed but the card has already changed.
I want to remove the cause of the symptom and the symptom. Help.
Attach the source.
- At the time of failure
connecting to ACS ACR122U 00 00
FF CA 00 00 04 - uid get
DB 96 AE 52 90 00
FF 82 00 00 06 05 00 00 00 00 00 - load key
[] 90 00 - success load key
FF 86 00 00 05 01 00 00 60 00 - authentication
[] 90 00 - success authentication
FF B0 00 01 10 - number 1 binary block call
00 00 00 00 40 00 00 00 AA 55 00 00 00 40 00 00 90 00 - get number 1 binary block
------------------------ before update binary block------------------------
before binary block >> [0, 0, 0, 0, 64, 0, 0, 0, 170, 85, 0, 0, 0, 64, 0, 0]
before 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 - number 1 binary block update
[] 63 00 - failed
------------------------update fail binary block------------------------
send byte>>[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 buzzer controll
[] 90 00 - buzzer control success
FF 00 40 00 04 01 00 01 01 buzzer controll
[] 90 00 - buzzer control success
FF B0 00 01 10 - number 1 binary block call
[] 63 00 - failed
disconnecting from ACS ACR122U 00 00
- After failure
connecting to ACS ACR122U 00 00
FF CA 00 00 04 - uid get
DB 96 AE 52 90 00
FF 82 00 00 06 05 00 00 00 00 00 - load key
[] 90 00 - success load key
FF 86 00 00 05 01 00 00 60 00 - authentication
[] 90 00 - authentication success
FF B0 00 01 10 - number 1 binary block call
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 90 00 - get number 1 binary block
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))
The expected result should be unchanged after the update block but intermittently changed to 0 or 255.