如何使用 MFRC522 检测移除的卡 [英] How to detect removed card with MFRC522
问题描述
我正在处理需要读取 NFC 标签 ID,然后根据该 ID 执行一些操作的项目.到目前为止,我能够读取一次(带有标签的对象一直在阅读器上),但是当对象被取下时我还需要一些反馈.我正在使用带有 RPi 和 SimpleMFRC522 库的 MFRC522 阅读器.
I am working on project where I need to read ID of NFC tag, and then perform some action based on that ID. So far I am able to read it once (object with tag is on the reader whole time), but I also need to have some feedback when object is taken off. I am using MFRC522 reader with RPi and SimpleMFRC522 library.
我试过在其中研究代码,但没有成功.你有什么方向/想法吗?我有以下代码:
I've tried poking around code in that, but without any success. Do you have any directions / ideas? I have following code:
reader = SimpleMFRC522.SimpleMFRC522()
buffor = 0
while continue_reading:
try:
id, text = reader.read()
if(buffor != id):
print id
udp_send(id)
buffor = id
finally:
time.sleep(0.5)
推荐答案
根据您提供的链接中的代码,看起来 reader.read
方法阻塞,而没有要读取的 ID.相反,它只是继续尝试读取,直到看到 ID.
Based on the code at the link you provided, it looks like the reader.read
method blocks while there is no ID to read. Instead, it just continues to try to read until it sees an ID.
但是,还有另一种称为 reader.read_no_block
的方法,如果没有要读取的 ID,它似乎返回 None
,因此您可以改用它.
However, there is another method called reader.read_no_block
which seems to return None
if there is no ID to read, so you could use that instead.
reader = SimpleMFRC522.SimpleMFRC522()
last_id = None
def exit_event():
print('tag has left the building')
while continue_reading:
id, text = reader.read_no_block()
if id and id != last_id:
# process new tag here
last_id = id
continue
elif not id and last_id:
# if there's no id to read, and last_id is not None,
# then a tag has left the reader
exit_event()
last_id = None
您可能需要进行一些去抖动处理,以便在阅读器错误地错过对标签的读取时不会错误地调用 exit_event
.
You will probably want to do some debouncing so that exit_event
doesn't get called in error when the reader mistakenly misses a read on a tag.
这篇关于如何使用 MFRC522 检测移除的卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!