NFC Tag Reader with PyNFC Library

  • Share this:

Code introduction


This function reads data from an NFC tag using the PyNFC library, supporting different types of NFC tags such as ISO14443A, ISO14443B, etc.


Technology Stack : PyNFC, NFC tag

Code Type : Function

Code Difficulty : Intermediate


                
                    
def read_tag(tag_type='ISO14443A', timeout=1000):
    """
    This function reads data from a NFC tag using the PyNFC library.
    It supports different types of NFC tags like ISO14443A, ISO14443B, etc.
    
    Args:
        tag_type (str): The type of NFC tag to read from. Default is 'ISO14443A'.
        timeout (int): The timeout value in milliseconds. Default is 1000.
    
    Returns:
        bytes: The data read from the NFC tag.
    """
    import nfc
    # Create a new NFC reader
    reader = nfc.ContactlessFrontend('usb')
    # Connect to the NFC tag
    tag = reader.connect(tag_type)
    # Read data from the NFC tag
    data = tag.read()
    # Disconnect from the NFC tag
    tag.ndef.close()
    return data                
              
Tags: