Raspberry Piとnfcpyを使ってカードリーダーRC-S380(SONY)からIDを読むPythonプログラム

カードリーダーRC-S380(SONY)を使うPythonプログラム

Raspberry Piとnfcpyを使って、カードを読む

最終的にとっても参考になったのがこのサイトです

lsusbでUSBのディバイスがどうなっているか表示される

nfcpyはここGitHubにある。さらに、詳しい使い方は、そこの下の方Read the Docsにを読めばいい。

実際のnfcpyのインストールは

sudo pip3 install -U nfcpyでできる。-Uはアップデートも含めて

 再インストールは

sudo pip3 install -I nfcpyでできる。-Iが(アイ)再インストール

 

結構これがうまくいかなかった。

普通にPython2と3がインストールされているので、3に入れるときはpip3とするとPython3にインストールすることができる。nfcpyがPython3で動かなかったようだが、最近対応するようになったようだ。

このあと、nfcpyの中にある、examplesフォルダにある、tagtools.pyを実行すると試しに使えてカードのIDを読むことができた。

ただ、sudo python3 tagtools.pyで動かさないとエラーがでる。

これを回避しないと、他のプログラミング環境で動かない。(RaspberryPiに入っていたTommyを使ってみた) その方法は、

$ python3 -m nfc

これを実行すると

> This is the 1.0.3 version of nfcpy run in Python 3.7.3
> on Linux-4.19.57-v7+-armv7l-with-debian-10.0
> I'm now searching your system for contactless devices
> ** found usb:054c:06c3 at usb:001:004 but access is denied
> -- the device is owned by 'root' but you are 'pi'
> -- also members of the 'root' group would be permitted
> -- you could use 'sudo' but this is not recommended
> -- better assign the device to the 'plugdev' group
> sudo sh -c 'echo SUBSYSTEM==\"usb\", ACTION==\"add\", ATTRS{idVendor}==\"054c\", ATTRS{idProduct}==\"06c3\", GROUP=\"plugdev\" >> /etc/udev/rules.d/nfcdev.rules'
> sudo udevadm control -R # then re-attach device
> I'm not trying serial devices because you haven't told me
> -- add the option '--search-tty' to have me looking
> -- but beware that this may break other serial devs
> Sorry, but I couldn't find any contactless device

/etc/udev/rules.d/nfcdev.rulesのこの場所に、better assign the device to the 'plugdev' groupする事を書いてやるようだ。

その後は

sudo reboot

で再起動するとsudoしなくてもエラーが出なくなる。これで、Tommyでも動くようになった。