/ examples / pn532_readwrite_ntag2xx.py
pn532_readwrite_ntag2xx.py
 1  # Example of detecting and reading a block from a MiFare NFC card.
 2  # Author: Tony DiCola & Roberto Laricchia
 3  # Copyright (c) 2015 Adafruit Industries
 4  #
 5  # Permission is hereby granted, free of charge, to any person obtaining a copy
 6  # of this software and associated documentation files (the "Software"), to deal
 7  # in the Software without restriction, including without limitation the rights
 8  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 9  # copies of the Software, and to permit persons to whom the Software is
10  # furnished to do so, subject to the following conditions:
11  #
12  # The above copyright notice and this permission notice shall be included in all
13  # copies or substantial portions of the Software.
14  #
15  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  # SOFTWARE.
22  
23  """
24  This example shows connecting to the PN532 and writing & reading an ntag2xx
25  type RFID tag
26  """
27  
28  import board
29  import busio
30  from digitalio import DigitalInOut
31  
32  #
33  # NOTE: pick the import that matches the interface being used
34  #
35  # from adafruit_pn532.i2c import PN532_I2C
36  from adafruit_pn532.spi import PN532_SPI
37  
38  # from adafruit_pn532.uart import PN532_UART
39  
40  # I2C connection:
41  # i2c = busio.I2C(board.SCL, board.SDA)
42  
43  # Non-hardware reset/request with I2C
44  # pn532 = PN532_I2C(i2c, debug=False)
45  
46  # With I2C, we recommend connecting RSTPD_N (reset) to a digital pin for manual
47  # harware reset
48  # reset_pin = DigitalInOut(board.D6)
49  # On Raspberry Pi, you must also connect a pin to P32 "H_Request" for hardware
50  # wakeup! this means we don't need to do the I2C clock-stretch thing
51  # req_pin = DigitalInOut(board.D12)
52  # pn532 = PN532_I2C(i2c, debug=False, reset=reset_pin, req=req_pin)
53  
54  # SPI connection:
55  spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
56  cs_pin = DigitalInOut(board.D5)
57  pn532 = PN532_SPI(spi, cs_pin, debug=False)
58  
59  # UART connection
60  # uart = busio.UART(board.TX, board.RX, baudrate=115200, timeout=100)
61  # pn532 = PN532_UART(uart, debug=False)
62  
63  ic, ver, rev, support = pn532.firmware_version
64  print("Found PN532 with firmware version: {0}.{1}".format(ver, rev))
65  
66  # Configure PN532 to communicate with MiFare cards
67  pn532.SAM_configuration()
68  
69  print("Waiting for RFID/NFC card to write to!")
70  while True:
71      # Check if a card is available to read
72      uid = pn532.read_passive_target(timeout=0.5)
73      print(".", end="")
74      # Try again if no card is available.
75      if uid is not None:
76          break
77  
78  print("")
79  print("Found card with UID:", [hex(i) for i in uid])
80  
81  # Set 4 bytes of block to 0xFEEDBEEF
82  data = bytearray(4)
83  data[0:4] = b"\xFE\xED\xBE\xEF"
84  # Write 4 byte block.
85  pn532.ntag2xx_write_block(6, data)
86  # Read block #6
87  print(
88      "Wrote to block 6, now trying to read that data:",
89      [hex(x) for x in pn532.ntag2xx_read_block(6)],
90  )