diff --git a/Dump.py b/Dump.py
index 3b528e4..46f7db9 100644
--- a/Dump.py
+++ b/Dump.py
@@ -21,7 +21,6 @@
# along with MFRC522-Python. If not, see .
#
-import RPi.GPIO as GPIO
import MFRC522
import signal
@@ -30,9 +29,8 @@
# 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()
# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)
@@ -42,14 +40,14 @@ def end_read(signal,frame):
# This loop keeps checking for chips. If one is near it will get the UID and authenticate
while continue_reading:
-
- # Scan for cards
+
+ # Scan for cards
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
# 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()
@@ -57,11 +55,11 @@ 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]
-
+
# Select the scanned tag
MIFAREReader.MFRC522_SelectTag(uid)
diff --git a/MFRC522.py b/MFRC522.py
index d1ee8b6..f490fb4 100644
--- a/MFRC522.py
+++ b/MFRC522.py
@@ -1,416 +1,412 @@
-#!/usr/bin/env python
-# -*- coding: utf8 -*-
-#
-# Copyright 2014,2018 Mario Gomez
-#
-# This file is part of MFRC522-Python
-# MFRC522-Python is a simple Python implementation for
-# the MFRC522 NFC Card Reader for the Raspberry Pi.
-#
-# MFRC522-Python is free software: you can redistribute it and/or modify
-# it under the terms of the GNU Lesser General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# MFRC522-Python is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU Lesser General Public License for more details.
-#
-# You should have received a copy of the GNU Lesser General Public License
-# along with MFRC522-Python. If not, see .
-#
-
-import RPi.GPIO as GPIO
-import spi
-import signal
-import time
-
-class MFRC522:
- NRSTPD = 22
-
- MAX_LEN = 16
-
- PCD_IDLE = 0x00
- PCD_AUTHENT = 0x0E
- PCD_RECEIVE = 0x08
- PCD_TRANSMIT = 0x04
- PCD_TRANSCEIVE = 0x0C
- PCD_RESETPHASE = 0x0F
- PCD_CALCCRC = 0x03
-
- PICC_REQIDL = 0x26
- PICC_REQALL = 0x52
- PICC_ANTICOLL = 0x93
- PICC_SElECTTAG = 0x93
- PICC_AUTHENT1A = 0x60
- PICC_AUTHENT1B = 0x61
- PICC_READ = 0x30
- PICC_WRITE = 0xA0
- PICC_DECREMENT = 0xC0
- PICC_INCREMENT = 0xC1
- PICC_RESTORE = 0xC2
- PICC_TRANSFER = 0xB0
- PICC_HALT = 0x50
-
- MI_OK = 0
- MI_NOTAGERR = 1
- MI_ERR = 2
-
- Reserved00 = 0x00
- CommandReg = 0x01
- CommIEnReg = 0x02
- DivlEnReg = 0x03
- CommIrqReg = 0x04
- DivIrqReg = 0x05
- ErrorReg = 0x06
- Status1Reg = 0x07
- Status2Reg = 0x08
- FIFODataReg = 0x09
- FIFOLevelReg = 0x0A
- WaterLevelReg = 0x0B
- ControlReg = 0x0C
- BitFramingReg = 0x0D
- CollReg = 0x0E
- Reserved01 = 0x0F
-
- Reserved10 = 0x10
- ModeReg = 0x11
- TxModeReg = 0x12
- RxModeReg = 0x13
- TxControlReg = 0x14
- TxAutoReg = 0x15
- TxSelReg = 0x16
- RxSelReg = 0x17
- RxThresholdReg = 0x18
- DemodReg = 0x19
- Reserved11 = 0x1A
- Reserved12 = 0x1B
- MifareReg = 0x1C
- Reserved13 = 0x1D
- Reserved14 = 0x1E
- SerialSpeedReg = 0x1F
-
- Reserved20 = 0x20
- CRCResultRegM = 0x21
- CRCResultRegL = 0x22
- Reserved21 = 0x23
- ModWidthReg = 0x24
- Reserved22 = 0x25
- RFCfgReg = 0x26
- GsNReg = 0x27
- CWGsPReg = 0x28
- ModGsPReg = 0x29
- TModeReg = 0x2A
- TPrescalerReg = 0x2B
- TReloadRegH = 0x2C
- TReloadRegL = 0x2D
- TCounterValueRegH = 0x2E
- TCounterValueRegL = 0x2F
-
- Reserved30 = 0x30
- TestSel1Reg = 0x31
- TestSel2Reg = 0x32
- TestPinEnReg = 0x33
- TestPinValueReg = 0x34
- TestBusReg = 0x35
- AutoTestReg = 0x36
- VersionReg = 0x37
- AnalogTestReg = 0x38
- TestDAC1Reg = 0x39
- TestDAC2Reg = 0x3A
- TestADCReg = 0x3B
- Reserved31 = 0x3C
- Reserved32 = 0x3D
- Reserved33 = 0x3E
- Reserved34 = 0x3F
-
- serNum = []
-
- def __init__(self, dev='/dev/spidev0.0', spd=1000000):
- spi.openSPI(device=dev,speed=spd)
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(self.NRSTPD, GPIO.OUT)
- GPIO.output(self.NRSTPD, 1)
- self.MFRC522_Init()
-
- def MFRC522_Reset(self):
- self.Write_MFRC522(self.CommandReg, self.PCD_RESETPHASE)
-
- def Write_MFRC522(self, addr, val):
- spi.transfer(((addr<<1)&0x7E,val))
-
- def Read_MFRC522(self, addr):
- val = spi.transfer((((addr<<1)&0x7E) | 0x80,0))
- return val[1]
-
- def SetBitMask(self, reg, mask):
- tmp = self.Read_MFRC522(reg)
- self.Write_MFRC522(reg, tmp | mask)
-
- def ClearBitMask(self, reg, mask):
- tmp = self.Read_MFRC522(reg);
- self.Write_MFRC522(reg, tmp & (~mask))
-
- def AntennaOn(self):
- temp = self.Read_MFRC522(self.TxControlReg)
- if(~(temp & 0x03)):
- self.SetBitMask(self.TxControlReg, 0x03)
-
- def AntennaOff(self):
- self.ClearBitMask(self.TxControlReg, 0x03)
-
- def MFRC522_ToCard(self,command,sendData):
- backData = []
- backLen = 0
- status = self.MI_ERR
- irqEn = 0x00
- waitIRq = 0x00
- lastBits = None
- n = 0
- i = 0
-
- if command == self.PCD_AUTHENT:
- irqEn = 0x12
- waitIRq = 0x10
- if command == self.PCD_TRANSCEIVE:
- irqEn = 0x77
- waitIRq = 0x30
-
- self.Write_MFRC522(self.CommIEnReg, irqEn|0x80)
- self.ClearBitMask(self.CommIrqReg, 0x80)
- self.SetBitMask(self.FIFOLevelReg, 0x80)
-
- self.Write_MFRC522(self.CommandReg, self.PCD_IDLE);
-
- while(i self.MAX_LEN:
- n = self.MAX_LEN
-
- i = 0
- while i
+#
+# This file is part of MFRC522-Python
+# MFRC522-Python is a simple Python implementation for
+# the MFRC522 NFC Card Reader for the Raspberry Pi.
+#
+# MFRC522-Python is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# MFRC522-Python is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with MFRC522-Python. If not, see .
+#
+
+import RPi.GPIO as GPIO
+import spi
+
+class MFRC522:
+ NRSTPD = 22
+
+ MAX_LEN = 16
+
+ PCD_IDLE = 0x00
+ PCD_AUTHENT = 0x0E
+ PCD_RECEIVE = 0x08
+ PCD_TRANSMIT = 0x04
+ PCD_TRANSCEIVE = 0x0C
+ PCD_RESETPHASE = 0x0F
+ PCD_CALCCRC = 0x03
+
+ PICC_REQIDL = 0x26
+ PICC_REQALL = 0x52
+ PICC_ANTICOLL = 0x93
+ PICC_SElECTTAG = 0x93
+ PICC_AUTHENT1A = 0x60
+ PICC_AUTHENT1B = 0x61
+ PICC_READ = 0x30
+ PICC_WRITE = 0xA0
+ PICC_DECREMENT = 0xC0
+ PICC_INCREMENT = 0xC1
+ PICC_RESTORE = 0xC2
+ PICC_TRANSFER = 0xB0
+ PICC_HALT = 0x50
+
+ MI_OK = 0
+ MI_NOTAGERR = 1
+ MI_ERR = 2
+
+ Reserved00 = 0x00
+ CommandReg = 0x01
+ CommIEnReg = 0x02
+ DivlEnReg = 0x03
+ CommIrqReg = 0x04
+ DivIrqReg = 0x05
+ ErrorReg = 0x06
+ Status1Reg = 0x07
+ Status2Reg = 0x08
+ FIFODataReg = 0x09
+ FIFOLevelReg = 0x0A
+ WaterLevelReg = 0x0B
+ ControlReg = 0x0C
+ BitFramingReg = 0x0D
+ CollReg = 0x0E
+ Reserved01 = 0x0F
+
+ Reserved10 = 0x10
+ ModeReg = 0x11
+ TxModeReg = 0x12
+ RxModeReg = 0x13
+ TxControlReg = 0x14
+ TxAutoReg = 0x15
+ TxSelReg = 0x16
+ RxSelReg = 0x17
+ RxThresholdReg = 0x18
+ DemodReg = 0x19
+ Reserved11 = 0x1A
+ Reserved12 = 0x1B
+ MifareReg = 0x1C
+ Reserved13 = 0x1D
+ Reserved14 = 0x1E
+ SerialSpeedReg = 0x1F
+
+ Reserved20 = 0x20
+ CRCResultRegM = 0x21
+ CRCResultRegL = 0x22
+ Reserved21 = 0x23
+ ModWidthReg = 0x24
+ Reserved22 = 0x25
+ RFCfgReg = 0x26
+ GsNReg = 0x27
+ CWGsPReg = 0x28
+ ModGsPReg = 0x29
+ TModeReg = 0x2A
+ TPrescalerReg = 0x2B
+ TReloadRegH = 0x2C
+ TReloadRegL = 0x2D
+ TCounterValueRegH = 0x2E
+ TCounterValueRegL = 0x2F
+
+ Reserved30 = 0x30
+ TestSel1Reg = 0x31
+ TestSel2Reg = 0x32
+ TestPinEnReg = 0x33
+ TestPinValueReg = 0x34
+ TestBusReg = 0x35
+ AutoTestReg = 0x36
+ VersionReg = 0x37
+ AnalogTestReg = 0x38
+ TestDAC1Reg = 0x39
+ TestDAC2Reg = 0x3A
+ TestADCReg = 0x3B
+ Reserved31 = 0x3C
+ Reserved32 = 0x3D
+ Reserved33 = 0x3E
+ Reserved34 = 0x3F
+
+ serNum = []
+
+ def __init__(self, dev='/dev/spidev0.0', spd=1000000):
+ spi.openSPI(device=dev,speed=spd)
+ GPIO.setmode(GPIO.BOARD)
+ GPIO.setup(self.NRSTPD, GPIO.OUT, initial=GPIO.HIGH)
+
+ self.MFRC522_Reset()
+
+ self.Write_MFRC522(self.TModeReg, 0x8D)
+ self.Write_MFRC522(self.TPrescalerReg, 0x3E)
+ self.Write_MFRC522(self.TReloadRegL, 30)
+ self.Write_MFRC522(self.TReloadRegH, 0)
+
+ self.Write_MFRC522(self.TxAutoReg, 0x40)
+ self.Write_MFRC522(self.ModeReg, 0x3D)
+ self.AntennaOn()
+
+ def __del__(self):
+ GPIO.cleanup([self.NRSTPD])
+
+ def MFRC522_Reset(self):
+ self.Write_MFRC522(self.CommandReg, self.PCD_RESETPHASE)
+
+ def Write_MFRC522(self, addr, val):
+ spi.transfer(((addr<<1)&0x7E,val))
+
+ def Read_MFRC522(self, addr):
+ val = spi.transfer((((addr<<1)&0x7E) | 0x80,0))
+ return val[1]
+
+ def SetBitMask(self, reg, mask):
+ tmp = self.Read_MFRC522(reg)
+ self.Write_MFRC522(reg, tmp | mask)
+
+ def ClearBitMask(self, reg, mask):
+ tmp = self.Read_MFRC522(reg)
+ self.Write_MFRC522(reg, tmp & (~mask))
+
+ def AntennaOn(self):
+ temp = self.Read_MFRC522(self.TxControlReg)
+ if(~(temp & 0x03)):
+ self.SetBitMask(self.TxControlReg, 0x03)
+
+ def AntennaOff(self):
+ self.ClearBitMask(self.TxControlReg, 0x03)
+
+ def MFRC522_ToCard(self,command,sendData):
+ backData = []
+ backLen = 0
+ status = self.MI_ERR
+ irqEn = 0x00
+ waitIRq = 0x00
+ lastBits = None
+ n = 0
+ i = 0
+
+ if command == self.PCD_AUTHENT:
+ irqEn = 0x12
+ waitIRq = 0x10
+ if command == self.PCD_TRANSCEIVE:
+ irqEn = 0x77
+ waitIRq = 0x30
+
+ self.Write_MFRC522(self.CommIEnReg, irqEn|0x80)
+ self.ClearBitMask(self.CommIrqReg, 0x80)
+ self.SetBitMask(self.FIFOLevelReg, 0x80)
+
+ self.Write_MFRC522(self.CommandReg, self.PCD_IDLE)
+
+ while(i self.MAX_LEN:
+ n = self.MAX_LEN
+
+ i = 0
+ while i.
#
-import RPi.GPIO as GPIO
import MFRC522
import signal
@@ -30,9 +29,8 @@
# 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()
# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)
@@ -41,19 +39,19 @@ 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:
-
- # Scan for cards
+
+ # Scan for cards
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
# 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()
@@ -61,11 +59,11 @@ 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]
-
+
# Select the scanned tag
MIFAREReader.MFRC522_SelectTag(uid)
@@ -77,5 +75,5 @@ def end_read(signal,frame):
MIFAREReader.MFRC522_Read(8)
MIFAREReader.MFRC522_StopCrypto1()
else:
- print "Authentication error"
+ print("Authentication error")
diff --git a/Write.py b/Write.py
index 237e268..a04d055 100644
--- a/Write.py
+++ b/Write.py
@@ -21,7 +21,6 @@
# along with MFRC522-Python. If not, see .
#
-import RPi.GPIO as GPIO
import MFRC522
import signal
@@ -32,7 +31,6 @@ def end_read(signal,frame):
global continue_reading
print "Ctrl+C captured, ending read."
continue_reading = False
- GPIO.cleanup()
# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)
@@ -42,14 +40,14 @@ def end_read(signal,frame):
# This loop keeps checking for chips. If one is near it will get the UID and authenticate
while continue_reading:
-
- # Scan for cards
+
+ # Scan for cards
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
# 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()
@@ -57,17 +55,17 @@ 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]
-
+
# Select the scanned tag
MIFAREReader.MFRC522_SelectTag(uid)
# Authenticate
status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)
- print "\n"
+ print("\n")
# Check if authenticated
if status == MIFAREReader.MI_OK:
@@ -79,34 +77,34 @@ 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
- 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()