st-ten-1/src/test/rfid.py

46 lines
1.7 KiB
Python
Raw Normal View History

2023-05-16 17:39:30 +00:00
import time
import ndef
import nfc
from nfc.clf import RemoteTarget
clf = nfc.ContactlessFrontend()
2023-05-18 08:40:16 +00:00
connected=False
2023-05-16 17:39:30 +00:00
while True:
2023-05-18 08:40:16 +00:00
try:
if not connected:
connected=clf.open('tty:USB0:pn532')
if connected:
target = clf.sense(RemoteTarget('106A'), RemoteTarget('106B'), RemoteTarget('212F'))
if target is not None:
tag = nfc.tag.activate(clf, target)
if tag is not None:
print(tag)
if tag.ndef is not None:
if not len(tag.ndef.records):
print("EMPTY TAG - WRITING...")
if not tag.ndef.is_writeable:
print("This Tag is not writeable.")
break
else:
tag.format()
#tag.ndef.records = [ndef.TextRecord("Errecinque"),ndef.TextRecord("5812345678")]
record=ndef.TextRecord("Errecinque,fixture,5812345678")
tag.ndef.records = [record]
if tag.ndef.has_changed:
print("WRITE ERROR")
else:
print("WRITE OK")
2023-05-16 17:39:30 +00:00
2023-05-18 08:40:16 +00:00
for record in tag.ndef.records:
print(record)
else:
print("ERROR NOT NDEF")
2023-05-16 17:39:30 +00:00
else:
2023-05-18 08:40:16 +00:00
print("NO TARGET")
except Exception as e:
print(f"EXCEPTION {e}")
connected=False
time.sleep(0.3)
2023-05-16 17:39:30 +00:00
print("EXITING")
clf.close()