|
功能效果: 本实例达到的效果是:
一、修改libNFC的接口修改LIBNFC,指定到某个UART口,例如COM3,请看另一个文章:
二、C程序代码
发送端代码 - #ifdef HAVE_CONFIG_H
- # include "config.h"
- #endif // HAVE_CONFIG_H
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <signal.h>
- #include <nfc/nfc.h>
- #include "utils/nfc-utils.h"
- #define MAX_FRAME_LEN 264
- static nfc_device *pnd;
- static void stop_dep_communication(int sig)
- {
- (void) sig;
- if (pnd)
- nfc_abort_command(pnd);
- else
- exit(EXIT_FAILURE);
- }
- int
- main(int argc, const char *argv[])
- {
- nfc_target nt;
- uint8_t abtRx[MAX_FRAME_LEN];
- uint8_t abtTx[] = "P2P transmit Sample--SmartFire.cn";//发送的数据 --风火轮团队
- if (argc > 1) {
- printf("Usage: %s\n", argv[0]);
- return EXIT_FAILURE;
- }
- nfc_context *context;
- nfc_init(&context);
- pnd = nfc_open(context, NULL);
- if (!pnd) {
- printf("Unable to open NFC device.\n");
- return EXIT_FAILURE;
- }
- printf("NFC device: %s\n opened", nfc_device_get_name(pnd));
- signal(SIGINT, stop_dep_communication);
- if (nfc_initiator_init(pnd) < 0) {
- nfc_perror(pnd, "nfc_initiator_init");
- goto error;
- }
- if (nfc_initiator_select_dep_target(pnd, NDM_PASSIVE, NBR_212, NULL, &nt, 1000) < 0) {
- nfc_perror(pnd, "nfc_initiator_select_dep_target");
- goto error;
- }
- print_nfc_target(nt, false);
- printf("Sending: %s\n", abtTx);
- int res;
- if ((res = nfc_initiator_transceive_bytes(pnd, abtTx, sizeof(abtTx), abtRx, sizeof(abtRx), 0)) < 0) {
- nfc_perror(pnd, "nfc_initiator_transceive_bytes");
- goto error;
- }
- abtRx[res] = 0;
- printf("Received: %s\n", abtRx);
- if (nfc_initiator_deselect_target(pnd) < 0) {
- nfc_perror(pnd, "nfc_initiator_deselect_target");
- goto error;
- }
- error:
- nfc_close(pnd);
- nfc_exit(context);
- return EXIT_SUCCESS;
- }
复制代码 接收端代码- #ifdef HAVE_CONFIG_H
- # include "config.h"
- #endif // HAVE_CONFIG_H
- #include <stdio.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <nfc/nfc.h>
- #include "utils/nfc-utils.h"
- #define MAX_FRAME_LEN 264
- static nfc_device *pnd;
- static void stop_dep_communication(int sig)
- {
- (void) sig;
- if (pnd)
- nfc_abort_command(pnd);
- else
- exit(EXIT_FAILURE);
- }
- int
- main(int argc, const char *argv[])
- {
- uint8_t abtRx[MAX_FRAME_LEN];
- int szRx;
- uint8_t abtTx[] = "Hello Mars!";
- nfc_context *context;
- nfc_init(&context);
- #define MAX_DEVICE_COUNT 2
- nfc_connstring connstrings[MAX_DEVICE_COUNT];
- size_t szDeviceFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT);
- // Little hack to allow using nfc-dep-initiator & nfc-dep-target from
- // the same machine: if there is more than one readers opened
- // nfc-dep-target will open the second reader
- // (we hope they're always detected in the same order)
- if (szDeviceFound == 1) {
- pnd = nfc_open(context, connstrings[0]);
- } else if (szDeviceFound > 1) {
- pnd = nfc_open(context, connstrings[1]);
- } else {
- printf("No device found.\n");
- return EXIT_FAILURE;
- }
- if (argc > 1) {
- printf("Usage: %s\n", argv[0]);
- return EXIT_FAILURE;
- }
- nfc_target nt = {
- .nm = {
- .nmt = NMT_DEP,
- .nbr = NBR_UNDEFINED
- },
- .nti = {
- .ndi = {
- .abtNFCID3 = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff, 0x00, 0x00 },
- .szGB = 4,
- .abtGB = { 0x12, 0x34, 0x56, 0x78 },
- .ndm = NDM_UNDEFINED,
- /* These bytes are not used by nfc_target_init: the chip will provide them automatically to the initiator */
- .btDID = 0x00,
- .btBS = 0x00,
- .btBR = 0x00,
- .btTO = 0x00,
- .btPP = 0x01,
- },
- },
- };
- if (!pnd) {
- printf("Unable to open NFC device.\n");
- return EXIT_FAILURE;
- }
- printf("NFC device: %s opened\n", nfc_device_get_name(pnd));
- signal(SIGINT, stop_dep_communication);
- printf("NFC device will now act as: ");
- print_nfc_target(nt, false);
- printf("Waiting for initiator request...\n");
- if ((szRx = nfc_target_init(pnd, &nt, abtRx, sizeof(abtRx), 0)) < 0) {
- nfc_perror(pnd, "nfc_target_init");
- goto error;
- }
- printf("Initiator request received. Waiting for data...\n");
- if ((szRx = nfc_target_receive_bytes(pnd, abtRx, sizeof(abtRx), 0)) < 0) {
- nfc_perror(pnd, "nfc_target_receive_bytes");
- goto error;
- }
- abtRx[(size_t) szRx] = '\0';
- printf("Received: %s\n", abtRx);
- printf("Sending: %s\n", abtTx);
- if (nfc_target_send_bytes(pnd, abtTx, sizeof(abtTx), 0) < 0) {
- nfc_perror(pnd, "nfc_target_send_bytes");
- goto error;
- }
- printf("Data sent.\n");
- error:
- nfc_close(pnd);
- nfc_exit(context);
- return EXIT_SUCCESS;
- }
复制代码 三、实验步骤 第一步,按以上修改libnfc ,然后编译,得到的exe 文件和libnfc.dll , 如果不会编译,请看我的另一篇文章:http://smartfire.cn/forum.php?mod=viewthread&tid=499&extra=page%3D1
第二步, 把exe 文件和libnfc.dll 放到同一个文件夹,然后在windows 下,CMD 命令行进到这个目录
第三步:硬件连接 A 电脑连接一块SmartNFC --PN532 开发板,扮演target 模式(卡模式),接受B 传过来的数据并显示,B 电脑连接一块SmartNFC --PN532 开发板,扮演主机模式,发送数据
默认是UART 连接如下图注意:一定要记得RX与TX交叉,就是串口板上的TX要接PN532开发板的RX,串口板上的RX接PN532的TX
第四步:命令先操作A 电脑,把pn532 模拟成卡
A 电脑,进到CMD ,输入:nfc-dep-target.exe
它会显示:Waiting for initiator request … 等主机发过来的数据
B电脑 CMD命令行进行libnfc目录,然后运行nfc-dep-initiator.exe
它就开始在COM3去操作PN532开发板,通过它去发送我们程序里预设的符串 “P2P transmit Sample--SmartFire.cn”
实现效果实拍两个PN532开发板,放在一起,RF对射
整套测试系统如下
下面附件,是风火轮科技编译好的可执行文件,方便童鞋们下载测试,
平台:WIN7 64bit
|
|