Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch issue 69 #93

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions MFRC522.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ class MFRC522:
Reserved34 = 0x3F

serNum = []
device_0 = spi.openSPI(device="/dev/spidev0.0", mode=0, speed=1000000)

def __init__(self, dev='/dev/spidev0.0', spd=1000000):
spi.openSPI(device=dev,speed=spd)
spi.openSPI(device=dev,mode=0,speed=spd)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(self.NRSTPD, GPIO.OUT)
GPIO.output(self.NRSTPD, 1)
Expand All @@ -138,10 +139,12 @@ def MFRC522_Reset(self):
self.Write_MFRC522(self.CommandReg, self.PCD_RESETPHASE)

def Write_MFRC522(self, addr, val):
spi.transfer(((addr<<1)&0x7E,val))
#spi.transfer(((addr<<1)&0x7E,val)) # abandoned https://github.com/mxgxw/MFRC522-python/issues/69
spi.transfer(self.device_0, ((addr<<1)&0x7E,val))

def Read_MFRC522(self, addr):
val = spi.transfer((((addr<<1)&0x7E) | 0x80,0))
#val = spi.transfer((((addr<<1)&0x7E) | 0x80,0)) # abandoned https://github.com/mxgxw/MFRC522-python/issues/69
val = spi.transfer(self.device_0, (((addr<<1)&0x7E) | 0x80,0))
return val[1]

def SetBitMask(self, reg, mask):
Expand Down Expand Up @@ -307,7 +310,7 @@ def MFRC522_SelectTag(self, serNum):
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, buf)

if (status == self.MI_OK) and (backLen == 0x18):
print "Size: " + str(backData[0])
print("Size: " + str(backData[0]))
return backData[0]
else:
return 0
Expand Down Expand Up @@ -338,9 +341,9 @@ def MFRC522_Auth(self, authMode, BlockAddr, Sectorkey, serNum):

# Check if an error occurred
if not(status == self.MI_OK):
print "AUTH ERROR!!"
print("AUTH ERROR!!")
if not (self.Read_MFRC522(self.Status2Reg) & 0x08) != 0:
print "AUTH ERROR(status2reg & 0x08) != 0"
print("AUTH ERROR(status2reg & 0x08) != 0")

# Return the status
return status
Expand All @@ -357,10 +360,10 @@ def MFRC522_Read(self, blockAddr):
recvData.append(pOut[1])
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, recvData)
if not(status == self.MI_OK):
print "Error while reading!"
print("Error while reading!")
i = 0
if len(backData) == 16:
print "Sector "+str(blockAddr)+" "+str(backData)
print("Sector "+str(blockAddr)+" "+str(backData))

def MFRC522_Write(self, blockAddr, writeData):
buff = []
Expand All @@ -373,7 +376,7 @@ def MFRC522_Write(self, blockAddr, writeData):
if not(status == self.MI_OK) or not(backLen == 4) or not((backData[0] & 0x0F) == 0x0A):
status = self.MI_ERR

print "%s backdata &0x0F == 0x0A %s" % (backLen, backData[0]&0x0F)
print("%s backdata &0x0F == 0x0A %s" % (backLen, backData[0]&0x0F))
if status == self.MI_OK:
i = 0
buf = []
Expand All @@ -385,9 +388,9 @@ def MFRC522_Write(self, blockAddr, writeData):
buf.append(crc[1])
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE,buf)
if not(status == self.MI_OK) or not(backLen == 4) or not((backData[0] & 0x0F) == 0x0A):
print "Error while writing"
print("Error while writing")
if status == self.MI_OK:
print "Data written"
print("Data written")

def MFRC522_DumpClassic1K(self, key, uid):
i = 0
Expand All @@ -397,7 +400,7 @@ def MFRC522_DumpClassic1K(self, key, uid):
if status == self.MI_OK:
self.MFRC522_Read(i)
else:
print "Authentication error"
print("Authentication error")
i = i+1

def MFRC522_Init(self):
Expand Down
12 changes: 6 additions & 6 deletions Read.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
global continue_reading
print "Ctrl+C captured, ending read."
print("Ctrl+C captured, ending read.")
continue_reading = False
GPIO.cleanup()

Expand All @@ -41,8 +41,8 @@ def end_read(signal,frame):
MIFAREReader = MFRC522.MFRC522()

# Welcome message
print "Welcome to the MFRC522 data read example"
print "Press Ctrl-C to stop."
print("Welcome to the MFRC522 data read example")
print("Press Ctrl-C to stop.")

# This loop keeps checking for chips. If one is near it will get the UID and authenticate
while continue_reading:
Expand All @@ -52,7 +52,7 @@ def end_read(signal,frame):

# If a card is found
if status == MIFAREReader.MI_OK:
print "Card detected"
print("Card detected")

# Get the UID of the card
(status,uid) = MIFAREReader.MFRC522_Anticoll()
Expand All @@ -61,7 +61,7 @@ def end_read(signal,frame):
if status == MIFAREReader.MI_OK:

# Print UID
print "Card read UID: %s,%s,%s,%s" % (uid[0], uid[1], uid[2], uid[3])
print("Card read UID: %s,%s,%s,%s" % (uid[0], uid[1], uid[2], uid[3]))

# This is the default key for authentication
key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
Expand All @@ -77,5 +77,5 @@ def end_read(signal,frame):
MIFAREReader.MFRC522_Read(8)
MIFAREReader.MFRC522_StopCrypto1()
else:
print "Authentication error"
print("Authentication error")

30 changes: 15 additions & 15 deletions Write.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
global continue_reading
print "Ctrl+C captured, ending read."
print("Ctrl+C captured, ending read.")
continue_reading = False
GPIO.cleanup()

Expand All @@ -48,7 +48,7 @@ def end_read(signal,frame):

# If a card is found
if status == MIFAREReader.MI_OK:
print "Card detected"
print("Card detected")

# Get the UID of the card
(status,uid) = MIFAREReader.MFRC522_Anticoll()
Expand All @@ -57,7 +57,7 @@ def end_read(signal,frame):
if status == MIFAREReader.MI_OK:

# Print UID
print "Card read UID: %s,%s,%s,%s" % (uid[0], uid[1], uid[2], uid[3])
print("Card read UID: %s,%s,%s,%s" % (uid[0], uid[1], uid[2], uid[3]))

# This is the default key for authentication
key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
Expand All @@ -67,7 +67,7 @@ def end_read(signal,frame):

# Authenticate
status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)
print "\n"
print("\n")

# Check if authenticated
if status == MIFAREReader.MI_OK:
Expand All @@ -79,39 +79,39 @@ def end_read(signal,frame):
for x in range(0,16):
data.append(0xFF)

print "Sector 8 looked like this:"
print("Sector 8 looked like this:")
# Read block 8
MIFAREReader.MFRC522_Read(8)
print "\n"
print("\n")

print "Sector 8 will now be filled with 0xFF:"
print("Sector 8 will now be filled with 0xFF:")
# Write the data
MIFAREReader.MFRC522_Write(8, data)
print "\n"
print("\n")

print "It now looks like this:"
print("It now looks like this:")
# Check to see if it was written
MIFAREReader.MFRC522_Read(8)
print "\n"
print("\n")

data = []
# Fill the data with 0x00
for x in range(0,16):
data.append(0x00)

print "Now we fill it with 0x00:"
print("Now we fill it with 0x00:")
MIFAREReader.MFRC522_Write(8, data)
print "\n"
print("\n")

print "It is now empty:"
print("It is now empty:")
# Check to see if it was written
MIFAREReader.MFRC522_Read(8)
print "\n"
print("\n")

# Stop
MIFAREReader.MFRC522_StopCrypto1()

# Make sure to stop reading for cards
continue_reading = False
else:
print "Authentication error"
print("Authentication error")