這是一篇介紹如何用樹莓派使用PN532的隨筆,介紹了具體的使用步驟。 首先介紹一下: ①、IC卡是非接觸式的智能卡,裡面一般是一個方形線圈和一個小晶元(用強光照著可以看到)。M1卡是IC卡的一種,一般水卡、公交卡都是這種。UID卡是M1的複製子卡,與M1完全相容。M1卡0扇區的內容可讀不可寫,UID ...
這是一篇介紹如何用樹莓派使用PN532的隨筆,介紹了具體的使用步驟。
首先介紹一下:
①、IC卡是非接觸式的智能卡,裡面一般是一個方形線圈和一個小晶元(用強光照著可以看到)。M1卡是IC卡的一種,一般水卡、公交卡都是這種。UID卡是M1的複製子卡,與M1完全相容。M1卡0扇區的內容可讀不可寫,UID卡所有扇區均可讀可寫,所以M1卡的數據能複製到UID卡中,而不能複製到M1卡中。
平常用的M1卡有16個扇區,一個扇區4個塊,一個快16個位元組,一共1K數據。每個扇區的前三個塊是數據區,最後一個塊是keyA、控制段、keyB的存儲區域,分別是6個位元組,4個位元組,6個位元組。第0扇區的第0塊記錄了制卡廠家的或者卡的ID信息,只可讀,不可寫。
②、ITEAD PN532是為嵌入式設計的PN532板子,可以用樹莓派控制(我用window讀取不出來),它有2種數據傳輸模式,SPI和I2C。
操作步驟:
用的是I2C介面傳輸數據,SET0-->H,
SET1-->L
連線方法:
樹莓派<----->PN532
4口 <-----> VCC
5口 <-----> GND
3口 <-----> SDA/TX
5口 <-----> SCL/RX
另附一張樹莓派GPIO圖:
①、在樹莓派上安裝必要的庫:
sudo apt-get install libusb-dev libpcsclite-dev //這是libnfc依賴的庫
sudo apt-get install automake autoconf //這是編譯時用到的
如果安裝libusb-dev和libpcsclite-dev報版本錯誤安裝失敗“ Unable to correct problems, you have held broken packages.”,
可執行命令:aptitude install libusb-dev ,然後依次輸n、y、y 即可。
②、安裝nfc操作模塊:
libnfc(操作nfc):
wget http://dl.bintray.com/nfc-tools/sources/libnfc-1.7.1.tar.bz2 tar -xf libnfc-1.7.1.tar.bz2 cd libnfc-1.7.1 ./configure --prefix=/usr --sysconfdir=/etc make sudo make install
mfoc(破解key,讀出數據到文件):https://github.com/nfc-tools/mfoc
mfuck(破解全加密數據):https://github.com/nfc-tools/mfcuk
後兩個下載解壓後切換到目錄里執行:
automake autoconf autoreconf -is ./configure make make install
③修改配置文件:
cd /etc sudo mkdir nfc sudo nano /etc/nfc/libnfc.conf
添加以下內容:
# Allow device auto-detection (default: true) # Note: if this auto-detection is disabled, user has to manually set a device # configuration using file or environment variable allow_autoscan = true # Allow intrusive auto-detection (default: false) # Warning: intrusive auto-detection can seriously disturb other devices # This option is not recommended, so user should prefer to add manually his/her device. allow_intrusive_scan = false # Set log level (default: error) # Valid log levels are (in order of verbosity): 0 (none), 1 (error), 2 (info), 3 (debug) # Note: if you compiled with --enable-debug option, the default log level is "debug" log_level = 1 # Manually set default device (no default) # To set a default device, users must set both name and connstring for their device # Note: if autoscan is enabled, default device will be the first device available in device list. device.name = "Itead_PN532_I2C" device.connstring = "pn532_i2c:/dev/i2c-1"
④、開啟樹莓派i2c:
執行 sudo raspi-config ,在第5項里打開i2c。
⑤、測試
重啟一下,看看有沒有i2c設備:ls /dev 或者 lsmod
執行 i2cdetect -y 1 ,如果出現的不全是橫杠,就代表連接成功了,如下圖:
如果全是橫杠的話,撥一下pn532的vcc線再插上試試。ps:我的也是死活顯示沒有,然後重插一下就有了。
放上一張卡執行 nfc-list,如果正常顯示下麵內容就表示讀取到卡了。
如果提示closed,就是板子沒正常接通。
⑤、讀寫數據:
mfoc -O output.mfd // 讀出卡中的數據保存為文件output.mfd
mfoc 是讀取數據,如果有加密就自動破解,如果全加密,就沒法讀取,可用mfuck命令破解。
nfc-mfclassic w a output.mfd output.mfd // 寫入數據,w小寫,如果大寫是強寫0扇區
由於每張卡的0扇區信息(UID)不一樣,0扇區又不可寫,不同的卡沒法互寫。但是可寫入0扇區可寫的UID卡。
我把讀出來的數據寫入它自己里可以寫入,暫時沒有UID卡,就沒辦法將數據寫入空卡裡。