I’m trying to write a value into a RFID card using the Phidget RFID SUT 1024. Unfortunately, I wasn’t able to find a working code for this reader and chatGPT didn’t give me a working code either. The only thing I was able to do is to read the card.
Here’s code I have right now:
import time
from Phidget22.Devices.RFID import RFIDTag
from Phidget22.Devices.RFID import RFIDTextRecord
from Phidget22.Devices.RFID import *
from Phidget22.PhidgetException import *
from Phidget22.Phidget import *
from Phidget22.PhidgetException import *
from Phidget22.Net import *
def on_tag(self, tag, protocol):
print("Tag value: ", tag)
def on_lost(self, tag, protocol):
print("Tag lost")
def write_number(rfid, number):
rfid.setAntennaEnabled(True)
message = RFIDTag()
text_record = RFIDTextRecord()
text_record.Text = str(number)
message.Records.append(text_record)
rfid.writeNDEFMessage(message)
rfid.setAntennaEnabled(False)
def read_number(rfid):
tag = rfid.getTag()
tag.setProtocol(RFIDProtocol.PHIDGETS)
data = tag.readTag().decode()
if data:
return int(data)
else:
return None
def subtract_number(rfid, number):
current_number = read_number(rfid)
if current_number is not None:
new_number = max(current_number - number, 0)
write_number(rfid, new_number)
return new_number
else:
return None
def main():
try:
rfid = RFID()
rfid.setOnTagHandler(on_tag)
rfid.setOnTagLostHandler(on_lost)
rfid.openWaitForAttachment(5000)
while True:
print('1. Write a number to the tag')
print('2. Read the previously written number on the tag')
print('3. Subtract a number from the previously written number on the tag')
choice = input('Enter your choice: ')
try:
if choice == '1':
number = int(input('Enter a number to write to the tag: '))
write_number(rfid, number)
print('Number {} written to tag'.format(number))
elif choice == '2':
number = read_number(rfid)
if number is not None:
print('Number on tag: {}'.format(number))
else:
print('No number found on tag')
elif choice == '3':
number = int(input('Enter a number to subtract: '))
new_number = subtract_number(rfid, number)
if new_number is not None:
print('Number subtracted from tag. New number: {}'.format(new_number))
else:
print('No number found on tag')
else:
print('Invalid choice')
except Exception as e:
print('Error: {}'.format(str(e)))
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
if __name__ == '__main__':
main()
I would like some assistance with my problem.
Thanks,