2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PN532 NFC RFID module + RaspberryPi で FeliCa と Mifare を判別する

Last updated at Posted at 2022-03-16

(Felica/Mifare/NFC チャレンジシリーズ) その他の記事はこちら 「FeliCa/Mifare/NFC でいろいろ実験」
https://qiita.com/nanbuwks/items/1f416d6e45a87250ee0a


「PN532 NFC RFID module + Arduino で FeliCa と Mifare を判別する」
https://qiita.com/nanbuwks/items/a264201dcc0b4e12e72d

では Arduino でやっていましたが、これを RaspberryPi でやってみます。

環境

image.png

  • PN532 NFC RFID module
  • Raspberry Pi Model B+ V1.2
    • Raspberry Pi OS Lite armhf-2020-12-04
  • Python 3.7.3
  • 1.0.4 version of nfcpy

接続とインストール

この記事の通りに行っています。
「Raspberry Pi で nfcpy + PN532 NFC RFID module」
https://qiita.com/nanbuwks/items/e14d8fdd6f863d5b68fe

サンプルプログラムで判別する

単にサンプルプログラムの tagtool.py を実行するだけで、以下のように判別できます。

FeliCa の場合


$ python3 tagtool.py --device tty:AMA0
[nfc.clf] searching for reader on path tty:AMA0
[nfc.clf] using PN532v1.6 at /dev/ttyAMA0
** waiting for a tag **
Type3Tag 'FeliCa Standard (RC-S960)' ID=0114B4276B115A23 PMM=0F0D23042F7783FF SYS=8B61

Mifare の場合


$ python3 tagtool.py --device tty:AMA0
[nfc.clf] searching for reader on path tty:AMA0
[nfc.clf] using PN532v1.6 at /dev/ttyAMA0
** waiting for a tag **
Type2Tag ID=C5B20FAD

コードを書く

判別のためのコードを作ってみます。

# coding:utf-8
import pprint
import nfc

print('please touch')
clf = nfc.ContactlessFrontend('tty:AMA0')
try:
    tag = clf.connect(rdwr={'on-connect': lambda tag: False})
finally:
    clf.close()
if ('FeliCa' in str(tag)):
  print("This card is FeliCa")
elif ('Type2Tag' in str(tag)):
  print("This card is Mifare")


# print(str(tag))
# pprint.pprint(vars(tag))
# pprint.pprint(vars(tag._clf))
# pprint.pprint(vars(tag._target))

手持ちの nanaco カードを読ませた場合


$ python3 checkcard.py
please touch
This card is FeliCa

$ python3 checkcard.py
please touch
This card is FeliCa

動作内容

サンプルコードの


# print(str(tag))

を有効にした場合、以下のように返ってきます。

FeliCaの場合


$ python3 checkcard.py
please touch
This card is FeliCa
Type3Tag 'FeliCa Standard (RC-S960)' ID=0114B4276B115A23 PMM=0F0D23042F7783FF SYS=8B61


Mifare の場合

$ python3 checkcard.py
please touch
This card is Mifare
Type2Tag ID=C5B20FAD

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?