NFC P2P(点对点)通信例子 C语言
功能效果:本实例达到的效果是:实现两个PN532开发板之间的P2P通信(点对点传一个字符串“P2P transmit Sameple – SmartFire.cn”),两台电脑,分别插一个SmartNFC --PN532 开发板,请从我们官方淘宝店购买:http://item.taobao.com/item.htm?&id=19307275283&一、修改libNFC的接口修改LIBNFC,指定到某个UART口,例如COM3,请看另一个文章:http://smartfire.cn/bbs/thread-32472-1-1.html
二、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_tabtRx;
uint8_tabtTx[] = "P2P transmit Sample--SmartFire.cn";//发送的数据--风火轮团队
if (argc > 1) {
printf("Usage: %s\n", argv);
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 = 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_tabtRx;
intszRx;
uint8_tabtTx[] = "Hello Mars!";
nfc_context *context;
nfc_init(&context);
#define MAX_DEVICE_COUNT 2
nfc_connstring connstrings;
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);
} else if (szDeviceFound > 1) {
pnd = nfc_open(context, connstrings);
} else {
printf("No device found.\n");
return EXIT_FAILURE;
}
if (argc > 1) {
printf("Usage: %s\n", argv);
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连接如下图http://img03.taobaocdn.com/imgextra/i3/56843193/T2Fl6BXjdXXXXXXXXX_!!56843193.jpg注意:一定要记得RX与TX交叉,就是串口板上的TX要接PN532开发板的RX,串口板上的RX接PN532的TX
第四步:命令先操作A电脑,把pn532模拟成卡
A电脑,进到CMD,输入:nfc-dep-target.exe
http://en.smartfire.cn/bbs/data/attachment/forum/201305/08/193345f3ox1sdlxsdq5bfl.png.thumb.jpg
它会显示:Waiting for initiator request …等主机发过来的数据
B电脑CMD命令行进行libnfc目录,然后运行nfc-dep-initiator.exehttp://en.smartfire.cn/bbs/data/attachment/forum/201305/08/19350249mf1f9nmdzazwyc.jpg.thumb.jpg
它就开始在COM3去操作PN532开发板,通过它去发送我们程序里预设的符串“P2P transmit Sample--SmartFire.cn”
实现效果实拍两个PN532开发板,放在一起,RF对射 http://en.smartfire.cn/bbs/data/attachment/forum/201305/08/193638befr2b3rer4lltj4.jpg.thumb.jpg
整套测试系统如下http://en.smartfire.cn/bbs/data/attachment/forum/201305/08/1938179yrmw0nt55yxthw0.jpg.thumb.jpghttp://en.smartfire.cn/bbs/data/attachment/forum/201305/08/193819ryoiioqr9b1mmeb8.jpg.thumb.jpg
http://en.smartfire.cn/bbs/data/attachment/forum/201305/08/19420928kcrk2ik226phh2.jpg.thumb.jpg
下面附件,是风火轮科技编译好的可执行文件,方便童鞋们下载测试,
平台:WIN7 64bit
页:
[1]