查看: 22709|回复: 1

NFC P2P(点对点)通信例子 C语言

[复制链接]

404

主题

245

回帖

2万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
20384
发表于 2015-1-10 13:47:07 | 显示全部楼层 |阅读模式
功能效果:
  本实例达到的效果是:
实现两个PN532开发板之间的P2P通信(点对点传一个字符串“P2P transmit Sameple – SmartFire.cn”),两台电脑,分别插一个SmartNFC --PN532 开发板,请从我们官方淘宝店购买:http://item.taobao.com/item.htm?&id=19307275283&

一、修改libNFC的接口
修改LIBNFC,指定到某个UART口,例如COM3,请看另一个文章:


二、C程序代码
发送端代码
  1. #ifdef HAVE_CONFIG_H
  2. #  include "config.h"
  3. #endif // HAVE_CONFIG_H

  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <signal.h>

  8. #include <nfc/nfc.h>

  9. #include "utils/nfc-utils.h"

  10. #define MAX_FRAME_LEN 264

  11. static nfc_device *pnd;

  12. static void stop_dep_communication(int sig)
  13. {
  14.   (void) sig;
  15.   if (pnd)
  16.     nfc_abort_command(pnd);
  17.   else
  18.     exit(EXIT_FAILURE);
  19. }

  20. int
  21. main(int argc, const char *argv[])
  22. {
  23.   nfc_target nt;
  24.   uint8_t  abtRx[MAX_FRAME_LEN];
  25.   uint8_t  abtTx[] = "P2P transmit Sample--SmartFire.cn";//发送的数据  --风火轮团队

  26.   if (argc > 1) {
  27.     printf("Usage: %s\n", argv[0]);
  28.     return EXIT_FAILURE;
  29.   }

  30.   nfc_context *context;
  31.   nfc_init(&context);

  32.   pnd = nfc_open(context, NULL);
  33.   if (!pnd) {
  34.     printf("Unable to open NFC device.\n");
  35.     return EXIT_FAILURE;
  36.   }
  37.   printf("NFC device: %s\n opened", nfc_device_get_name(pnd));

  38.   signal(SIGINT, stop_dep_communication);

  39.   if (nfc_initiator_init(pnd) < 0) {
  40.     nfc_perror(pnd, "nfc_initiator_init");
  41.     goto error;
  42.   }

  43.   if (nfc_initiator_select_dep_target(pnd, NDM_PASSIVE, NBR_212, NULL, &nt, 1000) < 0) {
  44.     nfc_perror(pnd, "nfc_initiator_select_dep_target");
  45.     goto error;
  46.   }
  47.   print_nfc_target(nt, false);

  48.   printf("Sending: %s\n", abtTx);
  49.   int res;
  50.   if ((res = nfc_initiator_transceive_bytes(pnd, abtTx, sizeof(abtTx), abtRx, sizeof(abtRx), 0)) < 0) {
  51.     nfc_perror(pnd, "nfc_initiator_transceive_bytes");
  52.     goto error;
  53.   }

  54.   abtRx[res] = 0;
  55.   printf("Received: %s\n", abtRx);

  56.   if (nfc_initiator_deselect_target(pnd) < 0) {
  57.     nfc_perror(pnd, "nfc_initiator_deselect_target");
  58.     goto error;
  59.   }

  60. error:
  61.   nfc_close(pnd);
  62.   nfc_exit(context);
  63.   return EXIT_SUCCESS;
  64. }
复制代码
接收端代码
  1. #ifdef HAVE_CONFIG_H
  2. #  include "config.h"
  3. #endif // HAVE_CONFIG_H

  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <signal.h>

  7. #include <nfc/nfc.h>

  8. #include "utils/nfc-utils.h"

  9. #define MAX_FRAME_LEN 264

  10. static nfc_device *pnd;

  11. static void stop_dep_communication(int sig)
  12. {
  13.   (void) sig;
  14.   if (pnd)
  15.     nfc_abort_command(pnd);
  16.   else
  17.     exit(EXIT_FAILURE);
  18. }

  19. int
  20. main(int argc, const char *argv[])
  21. {
  22.   uint8_t  abtRx[MAX_FRAME_LEN];
  23.   int  szRx;
  24.   uint8_t  abtTx[] = "Hello Mars!";

  25.   nfc_context *context;
  26.   nfc_init(&context);
  27. #define MAX_DEVICE_COUNT 2
  28.   nfc_connstring connstrings[MAX_DEVICE_COUNT];
  29.   size_t szDeviceFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT);
  30.   // Little hack to allow using nfc-dep-initiator & nfc-dep-target from
  31.   // the same machine: if there is more than one readers opened
  32.   // nfc-dep-target will open the second reader
  33.   // (we hope they're always detected in the same order)
  34.   if (szDeviceFound == 1) {
  35.     pnd = nfc_open(context, connstrings[0]);
  36.   } else if (szDeviceFound > 1) {
  37.     pnd = nfc_open(context, connstrings[1]);
  38.   } else {
  39.     printf("No device found.\n");
  40.     return EXIT_FAILURE;
  41.   }

  42.   if (argc > 1) {
  43.     printf("Usage: %s\n", argv[0]);
  44.     return EXIT_FAILURE;
  45.   }

  46.   nfc_target nt = {
  47.     .nm = {
  48.       .nmt = NMT_DEP,
  49.       .nbr = NBR_UNDEFINED
  50.     },
  51.     .nti = {
  52.       .ndi = {
  53.         .abtNFCID3 = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff, 0x00, 0x00 },
  54.         .szGB = 4,
  55.         .abtGB = { 0x12, 0x34, 0x56, 0x78 },
  56.         .ndm = NDM_UNDEFINED,
  57.         /* These bytes are not used by nfc_target_init: the chip will provide them automatically to the initiator */
  58.         .btDID = 0x00,
  59.         .btBS = 0x00,
  60.         .btBR = 0x00,
  61.         .btTO = 0x00,
  62.         .btPP = 0x01,
  63.       },
  64.     },
  65.   };

  66.   if (!pnd) {
  67.     printf("Unable to open NFC device.\n");
  68.     return EXIT_FAILURE;
  69.   }
  70.   printf("NFC device: %s opened\n", nfc_device_get_name(pnd));

  71.   signal(SIGINT, stop_dep_communication);

  72.   printf("NFC device will now act as: ");
  73.   print_nfc_target(nt, false);

  74.   printf("Waiting for initiator request...\n");
  75.   if ((szRx = nfc_target_init(pnd, &nt, abtRx, sizeof(abtRx), 0)) < 0) {
  76.     nfc_perror(pnd, "nfc_target_init");
  77.     goto error;
  78.   }

  79.   printf("Initiator request received. Waiting for data...\n");
  80.   if ((szRx = nfc_target_receive_bytes(pnd, abtRx, sizeof(abtRx), 0)) < 0) {
  81.     nfc_perror(pnd, "nfc_target_receive_bytes");
  82.     goto error;
  83.   }
  84.   abtRx[(size_t) szRx] = '\0';
  85.   printf("Received: %s\n", abtRx);

  86.   printf("Sending: %s\n", abtTx);
  87.   if (nfc_target_send_bytes(pnd, abtTx, sizeof(abtTx), 0) < 0) {
  88.     nfc_perror(pnd, "nfc_target_send_bytes");
  89.     goto error;
  90.   }
  91.   printf("Data sent.\n");

  92. error:
  93.   nfc_close(pnd);
  94.   nfc_exit(context);
  95.   return EXIT_SUCCESS;
  96. }
复制代码
三、实验步骤    第一步,按以上修改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连接如下图
注意:一定要记得RXTX交叉,就是串口板上的TX要接PN532开发板的RX,串口板上的RXPN532TX

    第四步:命令先操作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

相关帖子


风火轮微信公众号
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|风火轮WIKI|手机版|小黑屋|深圳风火轮团队 ( 粤ICP备17095099号 )

GMT+8, 2024-3-29 14:10 , Processed in 0.056965 second(s), 23 queries .

快速回复 返回顶部 返回列表
 
【客服1】 商务合作 15289193
【客服2】 业务洽谈 13257599
【客服3】 售前咨询 510313198
【邮箱】
smartfire@smartfire.cn