add write example

This commit is contained in:
danjperron
2021-02-15 12:09:53 -05:00
parent 4425620918
commit 6dd14764b9
2 changed files with 282 additions and 220 deletions

48
examples/Pico_write.py Normal file
View File

@@ -0,0 +1,48 @@
from mfrc522 import MFRC522
def uidToString(uid):
mystring = ""
for i in uid:
mystring = "%02X" % i + mystring
return mystring
reader = MFRC522(spi_id=0,sck=2,miso=4,mosi=3,cs=1,rst=0)
print("")
print("Place card before reader to read from address 0x08")
print("")
key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
try:
while True:
(stat, tag_type) = reader.request(reader.REQIDL)
if stat == reader.OK:
(stat, uid) = reader.SelectTagSN()
if stat == reader.OK:
print(uid)
print("Card detected %s" % uidToString(uid))
reader.MFRC522_DumpClassic1K(key, uid)
print("Test ! writing sector 2, block 0 (absolute block(8)")
print("with [ 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 ]")
absoluteBlock=8
value=[]
for i in range(16):
value.append(i)
status = reader.auth(reader.AUTHENT1A, absoluteBlock, key, uid)
if status == reader.OK:
status = reader.write(absoluteBlock,value)
if status == reader.OK:
reader.MFRC522_DumpClassic1K(key, uid)
else:
print("unable to write")
else:
print("Authentication error for writing")
break
except KeyboardInterrupt:
print("Bye")

View File

@@ -225,7 +225,7 @@ class MFRC522:
if self.DEBUG: print("anticol(1) {}".format(uid))
if self.PcdSelect(uid,self.PICC_ANTICOLL1) == 0:
return (self.ERR,[])
return (self.MI_ERR,[])
if self.DEBUG: print("pcdSelect(1) {}".format(uid))
#check if first byte is 0x88
@@ -251,7 +251,7 @@ class MFRC522:
if self.MFRC522_PcdSelect(uid,self.PICC_ANTICOLL3) == 0:
return (self.ERR,[])
if self.DEBUG: print("PcdSelect(3) {}".format(uid))
valid_uid.extend(uid[0:4])
valid_uid.extend(uid[0:5])
return (self.OK , valid_uid)
@@ -291,5 +291,19 @@ class MFRC522:
return stat
def MFRC522_DumpClassic1K(self, key, uid):
for i in range(64):
status = self.auth(self.AUTHENT1A, i, key, uid)
# Check if authenticated
print("{:02d}: ".format(i),end="")
if status == self.OK:
block = self.read(i)
if block is None:
print("-- ")
else:
for value in block:
print("{:02X} ".format(value),end="")
print("")
else:
print("Authentication error")