gpt4 book ai didi

nfc - Libnfc 如何读取NFC卡内容?

转载 作者:行者123 更新时间:2023-12-05 08:13:01 26 4
gpt4 key购买 nike

我正在尝试使用 ACR122 读卡器简单地读取 ISO 14443-2B ST SRx 卡内容。

我已经使用 Libnfc 和适当的阅读器驱动程序设置了我的环境。

现在我已经设法编写了一个代码,可以简单地读取卡 UID 并将其打印出来。根本没有文档,我完全不知道从哪里开始阅读卡片内容。任何线索或代码片段?

这是我所做的:

    // To compile this simple example:
// $ gcc -o quick_start_example1 quick_start_example1.c -lnfc
// ./quick_start_example1

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

void print_nfc_target(const nfc_target *pnt, bool verbose)
{
char *s;
str_nfc_target(&s, pnt, verbose);
printf("%s", s);
nfc_free(s);
}

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);
}

// Display libnfc version
const char *acLibnfcVersion = nfc_version();
(void)argc;
printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);

// Open, using the first available NFC device
pnd = nfc_open(context, NULL);

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);
}

printf("NFC reader: %s opened\n", nfc_device_get_name(pnd));

nfc_target ant[1];
nfc_modulation nm;
nm.nmt = NMT_ISO14443B;
nm.nbr = NBR_106;

nfc_initiator_list_passive_targets(pnd,nm,ant,1);
printf("%s\n",nfc_strerror(pnd)); // print Success

nfc_target ant2[1];
nfc_modulation nm2;
nm2.nmt = NMT_ISO14443B2SR;
nm2.nbr = NBR_106;

int res = 0;
int n = 0;
res = nfc_initiator_list_passive_targets(pnd, nm2, ant2, 1);
printf("%s\n",nfc_strerror(pnd)); // print Success

// printf("Cart identifier: %s\n", nt.nti.nsi.abtUID);
for (n = 0; n < res; n++) {
print_nfc_target(&ant2[n], true);
printf("\n");
}

// Close NFC device
nfc_close(pnd);
// Release the context
nfc_exit(context);
exit(EXIT_SUCCESS);
}

PS:由于已知错误 (http://www.libnfc.org/community/topic/1044/problem-with-init-of-iso14443b/),我列出了目标的 2 倍

最佳答案

我写了一个小帮助程序来使用 LibNFC 读取 ST SRx 标签:

https://github.com/Depau/nfc-st-srx/

它仍然需要一些更改来写入它们,但它确实成功地读取了它们。

关于nfc - Libnfc 如何读取NFC卡内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26875875/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com