st-ten-1/src/test/rfid.py
2023-05-18 10:40:16 +02:00

46 lines
1.7 KiB
Python

import time
import ndef
import nfc
from nfc.clf import RemoteTarget
clf = nfc.ContactlessFrontend()
connected=False
while True:
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")
for record in tag.ndef.records:
print(record)
else:
print("ERROR NOT NDEF")
else:
print("NO TARGET")
except Exception as e:
print(f"EXCEPTION {e}")
connected=False
time.sleep(0.3)
print("EXITING")
clf.close()