Python Forum
NFC reader code help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: NFC reader code help (/thread-34157.html)



NFC reader code help - johnroberts2k - Jul-01-2021

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.


RE: NFC reader code help - deanhystad - Jul-02-2021

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.