MIFARE Card Data Reader

  • Share this:

Code introduction


This function is used to read the data from a MIFARE card. It establishes a connection with the NFC device using the PyNFC library and reads data from a specified block.


Technology Stack : PyNFC

Code Type : Function

Code Difficulty : Intermediate


                
                    
def read_mifare_card(nfc_dev):
    import nfc
    import sys

    # Create an NFC context
    nfc_context = nfc.ContactlessFrontend('pietron:nfc')

    # Open a connection to the NFC device
    try:
        nfc_dev.connect()
        print("Connected to NFC device.")
    except Exception as e:
        print("Failed to connect to NFC device:", e)
        sys.exit(1)

    # Read MIFARE card data
    try:
        card = nfc_context.connect(target=nfc.clf.MifareTagTarget())
        print("Connected to MIFARE card.")
        response = card.read_block(4)
        print("Block 4 data:", response)
    except Exception as e:
        print("Failed to read MIFARE card data:", e)
    finally:
        nfc_dev.close()

    return response                
              
Tags: