如何配置libnfc使用通过udev规则固定命名的NFC设备?
Great job setting up those udev rules for fixed device names—this is exactly the right approach to avoid the frustrating device number shuffle! Let’s get libnfc hooked up to your /dev/nfc_a and /dev/nfc_b devices with these straightforward steps:
1. Locate the libnfc Configuration File
First, find where libnfc stores its config. Most systems use either:
- System-wide config:
/etc/nfc/libnfc.conf(create the/etc/nfcdirectory if it doesn’t exist) - User-specific config:
~/.config/nfc/libnfc.conf(ideal for per-user customizations)
2. Option 1: Set a Default Fixed Device
If you want one reader to be the default for all libnfc commands, edit the config file and adjust the device.name line:
# Comment out any existing device.name lines # device.name = "usb:072f:2200" # Add your fixed device path device.name = "/dev/nfc_a"
To switch to the second reader temporarily, you’ll just pass the device path as a command-line argument (more on that below).
3. Option 2: Configure Named Device Aliases (More Flexible)
For easy switching between readers without typing full paths, define aliases in the config file. First, you’ll need to know your reader’s connection string prefix (e.g., pn532_uart for UART-connected readers, pn532_usb for USB). You can get this by running nfc-list when the reader is connected (look for lines like connstring: pn532_uart:/dev/ttyUSB0).
Add these blocks to your config:
[nfc_a] name = "Top-Left NFC Reader" connstring = "pn532_uart:/dev/nfc_a" [nfc_b] name = "Top-Right NFC Reader" connstring = "pn532_uart:/dev/nfc_b"
Now you can reference them by alias in commands:
nfc-list -d nfc_a nfc-poll -d nfc_b
4. Directly Specify the Device in Commands or Code
If you don’t want to edit the config file, you can always pass the fixed device path directly to libnfc tools:
# Check the left reader nfc-list -d /dev/nfc_a # Poll the right reader for tags nfc-poll -d /dev/nfc_b
For custom code using the libnfc C API, pass the device path or alias to nfc_open():
nfc_device *pnd; nfc_context *context; nfc_init(&context); // Open the fixed device pnd = nfc_open(context, "/dev/nfc_a"); // Or open by alias // pnd = nfc_open(context, "nfc_b");
5. Verify Permissions (Common Gotcha!)
Make sure your user account has access to the /dev/nfc_a and /dev/nfc_b devices. Typically, these devices belong to the dialout or plugdev group. Add yourself to the correct group with:
sudo usermod -aG dialout $USER
Log out and back in for the group change to take effect.
Test It Out
Run nfc-list with your preferred device specifier—you should see the reader’s info without any "device not found" errors. If something’s off, double-check your udev rules are creating the symlinks correctly (run ls -l /dev/nfc* to confirm) and that your connstring prefix matches your reader type.
内容的提问来源于stack exchange,提问作者Rene Auberger

