pinpong example nfc

Reading and Writing Card Information with NFC Near Field Communication Module IIC

Here are the Python codes to read and write card information with an NFC near field communication module connected to an Arduino main control board via I2C port (SCL and SDA).

Reading Card Information

# -*- coding: utf-8 -*-
# Experiment effect: NFC near field communication module IIC reads card information
# Wiring: Connect an Arduino main control board to a Windows or Linux computer, and connect the NFC near field communication module to the I2C port SCL and SDA
import time
from pinpong.board import Board
from pinpong.libs.dfrobot_pn532 import PN532

Board("uno").begin()  # Initialization, choose the board type and port number. If the port number is not entered, automatic recognition will be performed
#Board("uno","COM36").begin()  # Initialization with specified port on Windows
#Board("uno","/dev/ttyACM0").begin()   # Initialization with specified port on Linux
#Board("uno","/dev/cu.usbmodem14101").begin()   # Initialization with specified port on Mac

nfc = PN532()

while not nfc.begin():
  print("initial failure")
  time.sleep(1)
print("Please place the info card/tag on module..... ")

while True:
  if nfc.scan():
    info = nfc.get_information()
    if info != None:
      print("----------------NFC card/tag information-------------------")
      print("UID Length: %d"%info.length)
      print("UID: %x %x %x %x"%(info.uid[0],info.uid[1],info.uid[2],info.uid[3] ))
      print("AQTA: %x %x"%(info.AQTA[0], info.AQTA[0]))
      print("SAK: 0x%x"%(info.sak))
      print("Type: %s"%(info.types))
      print("Manufacturer: %s"%(info.manu))
      print("RF Technology: %s"%(info.RF))
      print("Memory Size: %d bytes(total)/%d bytes(available)"%(info.size_total, info.size_available))
      print("Block/Page Size: %d bytes"%(info.block))
      print("Number of Blocks/pages: %d"%(info.num_block))
  time.sleep(1)

Reading Card Internal Information

# -*- coding: utf-8 -*-
# Experiment effect: NFC near field communication module IIC reads card internal information
# Wiring: Connect an Arduino main control board to a Windows or Linux computer, and connect the NFC near field communication module to the I2C port SCL and SDA
import time
from pinpong.board import Board
from pinpong.libs.dfrobot_pn532 import PN532

Board("uno").begin()  # Initialization, choose the board type and port number. If the port number is not entered, automatic recognition will be performed
#Board("uno","COM36").begin()  # Initialization with specified port on Windows
#Board("uno","/dev/ttyACM0").begin()   # Initialization with specified port on Linux
#Board("uno","/dev/cu.usbmodem14101").begin()   # Initialization with specified port on Mac

nfc = PN532()

def print_data(block):
  value = nfc.read_data(block)
  if  value != None:
    for i in value:
      print("0x%x "%(i), end="")
    print("")
  else:
    print_data(block)

while not nfc.begin():
  print("initial failure")
  time.sleep(1)
print("Waiting for a card......")

while True:
  if nfc.scan():
    NFC = nfc.get_information()
    if NFC != None:
      if NFC.length == 0x02 or NFC.length == 0x04:
        print("----------------Here is the card information to read-------------------")
        for i in range(NFC.num_block):
          if i == 0:
            print("Block %d:UID0-UID3/MANUFACTURER--------->"%(i), end="")
            print_data(i);
          elif (i+1)%4==0 and i < 128:
            print("Block %d:KEYA/ACCESS/KEYB--------------->"%(i), end="")
            print_data(i)
          elif (i+1)%16==0 and i > 127:
            print("Block %d:KEYA/ACCESS/KEYB--------------->"%(i), end="")
            print_data(i)
          else:
            print("Block %d:DATA   ------------------------>"%(i), end="")
            print_data(i)
      else:
        print("The card type is not mifareclassic...")
  time.sleep(3)

Reading and Writing Card Information

# -*- coding: utf-8 -*-
# Experiment effect: NFC near field communication module IIC reads and writes card information
# Wiring: Connect an Arduino main control board to a Windows or Linux computer, and connect the NFC near field communication module to the I2C port SCL and SDA
import time
from pinpong.board import Board
from pinpong.libs.dfrobot_pn532 import PN532

Board("uno").begin()  # Initialization, choose the board type and port number. If the port number is not entered, automatic recognition will be performed
#Board("uno","COM36").begin()  # Initialization with specified port on Windows
#Board("uno","/dev/ttyACM0").begin()   # Initialization with specified port on Linux
#Board("uno","/dev/cu.usbmodem14101").begin()   # Initialization with specified port on Mac

nfc = PN532()

write_data = "DFRobot NFC"
#write_data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
#write_data = (10, 9, 8, 7, 6, 5, 4, 3, 2, 1)

block_num = 2

while not nfc.begin():
  print("initial failure")
  time.sleep(1)
print("Waiting for a card......")

def parse_data(read_data):
  if read_data != None:
    print("read success! data is ", end=" ")
    print(read_data)
  else:
    print("read failure!")

while True:
  if nfc.scan():
    info = nfc.get_information()
    if info != None:
      if info.length == 0x02 or info.length == 0x04:
        if not nfc.write_data(block_num, write_data):
          print("write failure!")
        else:
          print("write success! data is", end=" ")
          print(write_data)
        read_data= nfc.read_data(block_num)
        parse_data(read_data)
      else:
        print("The card type is not mifareclassic...")
  time.sleep(2)