libnfc是GNU公共许可正下公布的第一个免费的底层的NFC开发包和编程API,可以作为了解PN532使用流程的一个很好的途径。下面先从libnfc的安装说起。源码需自行从官网下载。
环境
Ubuntu18.04+libusb-latest+libnfc-latest
安装依赖
sudo apt install libudev-dev autoconf automake libtool libusb-dev dh-autoreconf libusb-0.1.4 pkg-config
编译
// libnfc source root
./autoreconf -Wall -vis
./configure --with-drivers=pn532_uart --enable-serial-autoprobe --prefix=/usr/local/
make
sudo make install
使用
1、获取nfc设备列表
./nfc-list
# 注意,libnfc使用需要root权限,除非赋予当前用户/dev/ttyUSB0的读写权限
如报错需要将/usr/local/lib加入LD_LIBRARY_PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
2、修改libnfc配置文件,打印串口数据和PN532帧信息
# Allow device auto-detection (default: true)
# Note: if this auto-detection is disabled, user has to set manually 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, user should prefer to add manually his 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"
###################### hear #################################
log_level = 3
# Manually set default device (no default)
# To set a default device, you must set both name and connstring for your device
# Note: if autoscan is enabled, default device will be the first device available in device list.
device.name = "microBuilder.eu"
device.connstring = "pn532_uart:/dev/ttyUSB0"
PN532输出如下,就可以根据PN532的输出进行流程分析了。
debug libnfc.chip.pn53x SetParameters
debug libnfc.bus.uart TX: 00 00 ff 03 fd d4 12 14 06 00
debug libnfc.bus.uart RX: 00 00 ff 00 ff 00
debug libnfc.chip.pn53x PN53x ACKed
debug libnfc.bus.uart RX: 00 00 ff 02 fe
debug libnfc.bus.uart RX: d5 13
debug libnfc.bus.uart RX: 18 00
debug libnfc.chip.pn53x WriteRegister
debug libnfc.bus.uart TX: 00 00 ff 08 f8 d4 08 63 02 80 63 03 80 59 00
debug libnfc.bus.uart RX: 00 00 ff 00 ff 00
debug libnfc.chip.pn53x PN53x ACKed
debug libnfc.bus.uart RX: 00 00 ff 02 fe
debug libnfc.bus.uart RX: d5 09
debug libnfc.bus.uart RX: 22 00