使用mxgxw / MFRC522-python库编写ntag213(Mifare Ultralight C)和Raspi 2上的MFRC522阅读器

时间:2016-04-28 12:52:01

标签: python raspberry-pi rfid mifare write-error

对于我们正试图在ntag213(Mifare Ultralight C 180Bytes)上写的学校项目,我们无法写上它。除此之外,图书馆正在阅读Ultralight C / ntag213。

相同的设置,库和硬件在Mifare 1K标签1024kB上运行良好。

是否有人在PYTHON中使用MFRC522阅读器和Mifare Ultralight C标签(ntag213)对此库进行了此操作?

链接:
https://github.com/mxgxw/MFRC522-python http://cache.nxp.com/documents/data_sheet/NTAG213_215_216.pdf?pspll=1

def MFRC522_Write(self, blockAddr, writeData):
buff = []
buff.append(self.PICC_WRITE)
buff.append(blockAddr)
crc = self.CalulateCRC(buff)
buff.append(crc[0])
buff.append(crc[1])
(status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, buff)

#because of a timeout in function ToCard, backdata after this is empty!!!!!!!!!

if not(status == self.MI_OK) or not(backLen == 4) or not((backData[0] & 0x0F) == 0x0A):
    status = self.MI_ERR

print str(backLen)+" backdata &0x0F == 0x0A "+str(backData[0]&0x0F)
if status == self.MI_OK:
    i = 0
    buf = []
    while i < 16:
        buf.append(writeData[i])
        i = i + 1
    crc = self.CalulateCRC(buf)
    buf.append(crc[0])
    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"
    if status == self.MI_OK:
        print "Data written"

所以我们正在调查ToCard函数来搜索错误:

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<len(sendData)):
  self.Write_MFRC522(self.FIFODataReg, sendData[i])
  i = i+1

self.Write_MFRC522(self.CommandReg, command)

if command == self.PCD_TRANSCEIVE:
  self.SetBitMask(self.BitFramingReg, 0x80)
#!!!!!!!!!!!!!!!!!!!!!!!!!!!
# as we saw in the data sheet we figured out that until here everything looks correctly.
# despite of this fact self.Read_MFRC522(self.CommIrqReg) never gets an ACK
#!!!!!!!!!!!!!!!!!!!!!!!!!!!
i = 2000
while True:
  n = self.Read_MFRC522(self.CommIrqReg)
  i = i - 1
  if ~((i!=0) and ~(n&0x01) and ~(n&waitIRq)):
    break

self.ClearBitMask(self.BitFramingReg, 0x80)

if i != 0:
  if (self.Read_MFRC522(self.ErrorReg) & 0x1B)==0x00:
    status = self.MI_OK

    if n & irqEn & 0x01:
      status = self.MI_NOTAGERR

    if command == self.PCD_TRANSCEIVE:
      n = self.Read_MFRC522(self.FIFOLevelReg)
      lastBits = self.Read_MFRC522(self.ControlReg) & 0x07
      if lastBits != 0:
        backLen = (n-1)*8 + lastBits
      else:
        backLen = n*8

      if n == 0:
        n = 1
      if n > self.MAX_LEN:
        n = self.MAX_LEN

      i = 0
      while i<n:
        backData.append(self.Read_MFRC522(self.FIFODataReg))
        i = i + 1;
  else:
    status = self.MI_ERR

return (status,backData,backLen)

1 个答案:

答案 0 :(得分:1)

我正在尝试做同样的事情,它可能与超轻卡不需要授权这一事实有关。 至于您突出显示的代码位置,我suggest要重写它

        while True:
        n = self.dev_read(0x04)
        if n == 0:
            continue  # Too fast
        if n & irq_wait:
            break  # Got it!
        if n & 0x01:
            error = True
            break  # The timer decrements the timer value in register TCounterValReg to zero