For those who are in trouble because NFC is read infinitely when reading NFC with Python

I won't write much in detail, but a memo

For those who want to read NFC in Python

I was in trouble because the ʻon-connectfunction was executed all the time the NFC was in contact. It's not good to putSleep`, and the solution memo that I got by wandering the sea of the net

import nfc

def on_connect(tag):
    print(tag)

def on_release(tag):
    print("[*] released:")

def main():
    try:
        with nfc.ContactlessFrontend('usb') as clf:
            while clf.connect(rdwr={
                'on-connect': on_connect,
                'on-release': on_release,
            }):
                pass
    except IOError:
        print("NFC reader connection Error")
        sys.exit(0)

if __name__ == '__main__':
    main()

This will execute ʻon_connect () only once when the reader and NFC contact, and execute ʻon_release () when they are released.

I saw an infinite number of people who were in trouble if only ʻon-releasewas not executed, or if they didn't entersleep` and kept being called. I would be happy if I could rescue such a person

Recommended Posts

For those who are in trouble because NFC is read infinitely when reading NFC with Python
For those who are having trouble drawing graphs in python
For those who are in trouble with an error when pip install xg boost
Tips for those who are wondering how to use is and == in Python
Explanation for those who are having trouble with "command not found" in rbenv or pyenv
[Solved] I have a question for those who are familiar with Python mechanize.
Things to keep in mind when using Python for those who use MATLAB
5 Reasons Processing is Useful for Those Who Want to Get Started with Python
For those who want to write Python with vim
For Debian users who are having trouble with a bug in the Linux kernel 5.10
For those who are new to programming but have decided to analyze data with Python
Environment construction procedure for those who are not familiar with the python version control system
Behavior in each language when coroutines are reused with for
For those who are analyzing in atmosphere (Linear Regression Model 1)
If you are having trouble with timeouts when implementing Slack's SlashCommand in Python on AWS Lambda
A memo for those who use Python in Visual Studio (me)
A memo for when pip3 is installed with python2.7 for some reason
Read files in parallel with Python
[Comparison of PHP, Ruby, and Python description] For those who are wondering how the description method is different.
How to write what to do when an application is first displayed in Qt for Python with Designer
Read a Python # .txt file for a super beginner in Python with a working .py
Error due to UnicodeDecodeError when reading CSV file with Python [For beginners]
Inject is recommended for DDD in Python
Tips for dealing with binaries in Python
Process multiple lists with for in Python
[Python] Read images with OpenCV (for beginners)
Read text in images with python OCR
Execution order when multiple context managers are specified in the Python with statement
For those who want to learn Excel VBA and get started with Python
[For beginners] Unexpected behavior if "\" is included when setting the path in Python
Character strings placed in GCS with python are garbled when viewed with a browser
The first step for those who are amateurs of statistics but want to implement machine learning models in Python
Dart grammar for those who only know Python
Read table data in PDF file with Python
Precautions when dealing with control structures in Python 2.6
Character encoding when dealing with files in Python 3
What are you comparing with Python is and ==?
Materials to read when getting started with Python
Use pydantic when reading environment variables in Python
[Introduction for beginners] Working with MySQL in Python
What are you using when testing with Python?
Settings for getting started with MongoDB in python
The first step of machine learning ~ For those who want to implement with python ~
❤️ Bloggers ❤️ "Beloved BI" ❤️ Let's get started ❤️ (For those who can make charts with Python)
Environment construction for those who want to study python easily with VS Code (for Mac)
For those who should have installed janome properly with Python but get an error