Fixed uid output. Also fixed line endings (sorry).

This commit is contained in:
2024-11-26 19:58:41 +01:00
parent 62b93de024
commit 553bbe95b2

View File

@@ -1,44 +1,45 @@
from mfrc522 import MFRC522
import utime
def uidToString(uid):
mystring = ""
for i in uid:
mystring = "%02X" % i + mystring
return mystring
def main():
reader = MFRC522(spi_id=1,sck=10,miso=12,mosi=11,cs=13,rst=9)
print("")
print("Please place card on reader")
print("")
PreviousCard = [0]
try:
while True:
reader.init()
(stat, tag_type) = reader.request(reader.REQIDL)
#print('request stat:',stat,' tag_type:',tag_type)
if stat == reader.OK:
(stat, uid) = reader.SelectTagSN()
if uid == PreviousCard:
continue
if stat == reader.OK:
print("Card detected {} uid={}".format(hex(int.from_bytes(bytes(uid),"little",False)).upper(),reader.tohexstring(uid)))
PreviousCard = uid
else:
pass
else:
PreviousCard=[0]
utime.sleep_ms(50)
except KeyboardInterrupt:
pass
if __name__ == "__main__":
main()
from mfrc522 import MFRC522
import utime
def uid_to_string(uid):
mystring = "0x"
# The mfrc522 lib returns all tested uids in inverse order
for i in reversed(uid):
mystring += "%02X" % i
return mystring
def main():
reader = MFRC522(spi_id=1,sck=10,miso=12,mosi=11,cs=13,rst=9)
print("")
print("Please place card on reader")
print("")
previous_uid = [0]
try:
while True:
reader.init()
# For now we omit the tag type
(stat, _) = reader.request(reader.REQIDL)
if stat == reader.OK:
(stat, uid) = reader.SelectTagSN()
if uid == previous_uid:
continue
if stat == reader.OK:
print(uid_to_string(uid))
previous_uid = uid
else:
pass
else:
previous_uid=[0]
utime.sleep_ms(50)
except KeyboardInterrupt:
pass
if __name__ == "__main__":
main()