Python Forum

Full Version: NFC reader code help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have the following python code which continuously listens for NFC chip presence and when detected it prints the UID (ident)

from nfc import ContactlessFrontend
from time import sleep

def connected(tag):
    ident = ''.join('{:02x}'.format(ord(c)) for c in tag.identifier)
    print(ident)
    return False

clf = ContactlessFrontend('usb')
while True:
    clf.connect(rdwr={'on-connect': connected})
    sleep(1)
This works well. However, the problem I face is that if a user holds the NFC tag down then the script reads the same "ident" value over and over again. I want to solve this by possibly temporarily storing the value of "ident" so that it doesn't print if the value is the same as last scanned for at least X seconds before it is allowed to re-print the same UID. Is someone able to help me modify the code above to do this please?

Thank you.
Either ignore a tag if it is the same as the last tag (report tag immediately) or once you identify a tag continue looping until there is no tag, then return the previous tag (report tag when removed). You may have to configure the device to timout instead of wait for a tag. I am not familiar with the nfc library. You'll probably want to decrease your wait. 1 second is a really long time.