rfid write tag ok in dev tbt production
This commit is contained in:
parent
5a6dc82a9b
commit
b6b4e13869
@ -87,3 +87,37 @@ class RFID_PN532(Component):
|
||||
|
||||
def write_tag(self, data):
|
||||
self.data_to_write = copy.deepcopy(data)
|
||||
if not self.simulate:
|
||||
if not self.connected:
|
||||
self.open_device()
|
||||
|
||||
if not self.connected:
|
||||
self.log.error("Failed to connect to device")
|
||||
return
|
||||
|
||||
if self.mutex.tryLock():
|
||||
try:
|
||||
target = self.clf.sense(RemoteTarget('106A'), RemoteTarget('106B'), RemoteTarget('212F'))
|
||||
if target is not None:
|
||||
tag = src.lib.nfc.tag.activate(self.clf, target)
|
||||
if tag is not None and tag.ndef is not None:
|
||||
self.log.info("Detected NFC tag, writing data...")
|
||||
ndef_record = ndef.TextRecord(self.data_to_write)
|
||||
|
||||
# IMPORTANT: DON'T CLOSE DEVICE BEFORE WRITING
|
||||
tag.ndef.records = [ndef_record] # Write the record to the tag
|
||||
self.log.info(f"Data '{self.data_to_write}' written successfully to tag.")
|
||||
else:
|
||||
self.log.error("Tag is not NDEF-compatible or invalid")
|
||||
else:
|
||||
self.log.debug("No tag present for writing")
|
||||
except Exception as e:
|
||||
self.log.error(f"Error during tag writing: {e}")
|
||||
finally:
|
||||
# Move closing outside the try block
|
||||
self.mutex.unlock() # Unlock mutex always
|
||||
|
||||
# Close AFTER writing and mutex release
|
||||
if self.connected:
|
||||
self.close_device() # close_device can raise exception
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user