from smartcard.CardRequest import CardRequest from smartcard.util import toHexString from smartcard.CardConnection import CardConnection def read_page(connection, page): apdu = [0xFF, 0xB0, 0x00, page, 0x04] data, sw1, sw2 = connection.transmit(apdu) if sw1 == 0x90 and sw2 == 0x00: return data[:4] return None def main(): c = input("1 = read, 2 = write") print("Waiting for NTAG...") card_request = CardRequest(timeout=None) card_service = card_request.waitforcard() connection = card_service.connection connection.connect(CardConnection.T1_protocol) if c == "1": # UID GET_UID = [0xFF, 0xCA, 0x00, 0x00, 0x00] uid, _, _ = connection.transmit(GET_UID) print("UID:", toHexString(uid)) # Read pages 4–15 raw = [] for page in range(4, 16): data = read_page(connection, page) if data: raw.extend(data) print("\nRaw HEX:") print(toHexString(raw)) # Parse NDEF if raw[0] == 0x03: # NDEF TLV length = raw[1] ndef = raw[2:2+length] if ndef[0] == 0xD1 and ndef[3] == 0x54: # Text record lang_len = ndef[4] text = bytes(ndef[5+lang_len:]).decode("utf-8") print("\nDecoded NDEF Text:") print(text) else: print("\nUnsupported NDEF record type") else: print("\nNo NDEF message found") if c == "2": write_ndef_text(connection, "EZLAN00000123") if __name__ == "__main__": main()