讀取 RFID 資料的實驗
import serial from functools import reduce def _uint8_add(a, b): """Add add two values as unsigned 8-bit values.""" return ((a & 0xFF) + (b & 0xFF)) & 0xFF def lcs(length): return _uint8_add(~length, 1) def dcs(data): return ~reduce(_uint8_add, data, 0xFF)& 0xFF def read(): result = [] length = 0 # hardcode 12 for reading the bytes for i in range(0, 12): currentByte = ser.read(1) result.append(currentByte) # position 9 is the length if i == 9: length = int(currentByte.encode('hex'), 16) for i in range(0, length + 1): currentByte = ser.read(1) result.append(currentByte) return result def wakeup(): ser.write([ 0x55, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]) def write(data): length = len(data) ser.write([0x00, 0x00, 0xFF, length, lcs(length)] + data + [dcs(data), 0x00]) def printByteArray(data): print [elem.encode("hex") for elem in data] # open port with speed 115200 ser = serial.Serial('COM3', 115200, timeout=5) print("opened {0}" . format(ser.name)) # wake up wakeup() write([0xD4,0x14,0x01]) result = read() printByteArray(result) # get firmware version write([0xD4,0x02]) result = read() printByteArray(result) # get card info write([0xD4,0x4A,0x01,0x00]) result = read() printByteArray(result)
以下是程式執行效果,當有 MIFARE tag 靠近實驗板的時候,就會讀到 MIFARE tag 的資料,然後程式就會完成執行:
參考資料:
NXP NFC Controller Solutions
Adafruit_Python_PN532
PN532
本文連結