这是一篇介绍如何用树莓派使用PN532的随笔,介绍了具体的使用步骤。git
首先介绍一下:github
①、IC卡是非接触式的智能卡,里面通常是一个方形线圈和一个小芯片(用强光照着能够看到)。M1卡是IC卡的一种,通常水卡、公交卡都是这种。UID卡是M1的复制子卡,与M1彻底兼容。M1卡0扇区的内容可读不可写,UID卡全部扇区都可读可写,因此M1卡的数据能复制到UID卡中,而不能复制到M1卡中。bash
日常用的M1卡有16个扇区,一个扇区4个块,一个快16个字节,一共1K数据。每一个扇区的前三个块是数据区,最后一个块是keyA、控制段、keyB的存储区域,分别是6个字节,4个字节,6个字节。第0扇区的第0块记录了制卡厂家的或者卡的ID信息,只可读,不可写。测试
②、ITEAD PN532是为嵌入式设计的PN532板子,能够用树莓派控制(我用window读取不出来),它有2种数据传输模式,SPI和I2C。this
操做步骤:加密
用的是I2C接口传输数据,SET0-->H,
SET1-->L
spa
连线方法:debug
树莓派<----->PN532设计
4口 <-----> VCC3d
6口 <-----> 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卡,就没办法将数据写入空卡里。