1

我正在使用用 c 编译的 libnfc 1.7.1 从 Raspberry Pi 上的 PN532 阅读器读取。目标是为 Node-RED 创建一个节点,该节点注入扫描卡的 UID 或传递有关库或读卡器的错误。我修改了这个例子,给我一张卡的 UID 作为唯一的正常输出。除了无法加载库时出现错误、无法连接读卡器时出现错误或卡的 UID 之外,我无法打印任何内容。我在 /etc/nfc/libnfc.conf 中将日志级别更改为 0,但我的程序仍在打印“pn53x_check_communication:输入/输出错误”(不需要)以及“错误:无法打开 NFC 设备”。(想要)我找不到任何方法来禁用 I/O 错误消息。我在图书馆里看了看,发现了这个返回 NFC_EIO ,这是我得到的 I/O 错误,但找不到它实际打印的任何地方。除了修改库之外,我找不到任何方法来禁用此打印。如果无能为力,我可以对节点进行编程以忽略此输出,但我宁愿消除它。我的代码如下:

#include <stdlib.h>
#include <nfc/nfc.h>

static void
print_long(const uint8_t *pbtData, const size_t szBytes)
{
  size_t  szPos;

  for (szPos = 0; szPos < szBytes; szPos++) {
    printf("%03lu", pbtData[szPos]);
  }
  printf("\n");
}

int
main(int argc, const char *argv[])
{
  nfc_device *pnd;
  nfc_target nt;

  // Allocate only a pointer to nfc_context
  nfc_context *context;

  // Initialize libnfc and set the nfc_context
  nfc_init(&context);
  if (context == NULL) {
    printf("Unable to init libnfc (malloc)\n");
    exit(EXIT_FAILURE);
  }

  // Open, using the first available NFC device which can be in order of selection:
  //   - default device specified using environment variable or
  //   - first specified device in libnfc.conf (/etc/nfc) or
  //   - first specified device in device-configuration directory (/etc/nfc/devices.d) or
  //   - first auto-detected (if feature is not disabled in libnfc.conf) device
  pnd = nfc_open(context, NULL);

  //Send error
  if (pnd == NULL) {
    printf("ERROR: %s\n", "Unable to open NFC device.");
    exit(EXIT_FAILURE);
  }

  // Set opened NFC device to initiator mode
  if (nfc_initiator_init(pnd) < 0) {
    nfc_perror(pnd, "nfc_initiator_init");
    exit(EXIT_FAILURE);
  }

  while(true){
    // Poll for a ISO14443A (MIFARE) tag
    const nfc_modulation nmMifare = {
      .nmt = NMT_ISO14443A,
      .nbr = NBR_106,
    };

    //Print decimal version of UID and wait until it's removed to scan again
    if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) > 0) {
      print_long(nt.nti.nai.abtUid, nt.nti.nai.szUidLen);
      while (0 == nfc_initiator_target_is_present(pnd, NULL)) {}
    }
  }
}
4

0 回答 0