在RFID硬件里,最常见的就是PN532, Proxmark3, ACR122u三种了,在管理Java Card的时候,通常使用的是acr122u,pcsc和gp一般会直接认出, 但因其价格较贵所以经常劝退,实际上ACR122u的主控是PN533,那么名字差不多的PN532是否可以呢,请看下文


实际上,PN532从各种角度来看,他和PN533差不多是一种芯片,只不过区别在于,PN533是走USB,而PN532走的是SPI/UART,网上卖的PN532读卡器实际里面还塞了个串口转USB的芯片,只有这样,PN532才能正常使用。以下是nfc-tools项目网站对于PN53x系列的概括:

PN531 - ISO14443-A, MIFARE, FeliCa RF, UART, SPI, I2C, USB 2.0 Historically the first and not supported by NXP anymore
PN532 - ISO14443-A/B, MIFARE, FeliCa RF, UART, SPI, I2C, low-battery mode
PN533 - ISO14443-A/B, MIFARE, FeliCa RF, UART, USB 2.0


我们了解到了PN532和PN533的差异和一致性,那么使用肯定也是有差别的,PN533可以直接被GP和PCSC识别,但是PN532不行,那么这里就有一种办法可以让PCSC和GP识别PN532, 他就是 ifdnfc
ifdnfc是一个为所有libnfc支持的设备提供PC/SC支持的项目,我们需要借助他来达到目的
他的仓库是:https://github.com/nfc-tools/ifdnfc
他的说明文件是:https://github.com/nfc-tools/ifdnfc/blob/master/README


前置准备(以openSUSE为例)

安装编译需要的环境

zypper in autoconf automake libtool pcsc-lite-devel libnfc-devel

clone项目并且编译

   git clone https://github.com/nfc-tools/ifdnfc
   cd ifdnfc
   autoreconf -vis
   ./configure
   make
   sudo make install

这样就准备好了

配置libnfc

通过dmesg查看当前连接的pn532端口号(例如ttyUSB0)

编辑 /etc/nfc/libnfc.conf

vi /etc/nfc/libnfc.conf
##粘贴如下配置
# 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"

log_level = 1

# 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 = "IFD-NFC"
##如果你使用的是USB转串口连接的PN532, 写这条,后面对应修改你的设备路径
device.connstring = "pn532_uart:/dev/ttyUSB0"
##如果你使用的是SPI连接的PN532, 写这条,后面对应修改你的设备路径
device.connstring = "pn532_i2c:/dev/i2c-1"

保存,然后输入nfc-scan-device测试,出现如下就说明配置成功了

nfc-scan-device uses libnfc 1.8.0
1 NFC device(s) found:
- IFD-NFC:
    pn532_uart:/dev/ttyUSB0

配置ifdnfc

首先整一个配置文件

vi /etc/reader.conf.d/reader.conf

然后输入如下参数

#设备名称
FRIENDLYNAME      "IFD-NFC"  
#运行库路径
LIBPATH           /usr/local/lib64/libifdnfc.so
#设备路径
DEVICENAME        /dev/ttyUSB0
#频道id, 根据你的设备路径最后的数字确定
CHANNELID         0

然后就可以测试了!

测试

启动pcscd

sudo pcscd

然后在另外一个终端窗口里,激活ifdnfc

ifdnfc-activate

如果一切正常,你会看到:

Activating ifdnfc with "pn532_uart:/dev/ttyUSB0"...
IFD-NFC is inactive.

然后你的读卡器的灯会有规律的闪烁

启动pcsc

pcsc_scan

如果一切正常,应该显示如下, 并且放上卡片会有进一步的消息,那么就基本大功告成了!

  Using reader plug'n play mechanism
  Scanning present readers...
  0: IFD-NFC 00 00
 
  Sun Mar  7 03:11:38 2021
   Reader 0: IFD-NFC 00 00
    Event number: 64
    Card state: Card removed,

如果使用完毕了,那么请务必先执行ifdnfc-activate no,显示如下后再拔掉设备

  IFD-NFC is inactive.