欢迎光临
我们一直在努力

PNP532 NFC P2P方法代码-PNP532命令抓包方式发送

最近项目用到了PNP532,官方提供了库,使用的是libnfc,但出于速度,本着使用简单就没研读具体的协议。
也是为了再各个平台方便移植。
思路是通过串口的方式用上位机进行P2P的方式,通过串口监测软件进行抓包,然后写一个简单的程序按照抓包的顺序发送数据,大多数的 数据帧都是固定的。除了你要发送的数据需要特殊拼帧。
下面是简单的方法封装:

#include "nfc.h"
#include "xserial.h"

unsigned char send_command_1[26] = { 0x55, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x03, 0xFD, 0xD4, 0x14, 0x01, 0x17, 0x00 };
unsigned char send_command_2[16] = { 0x00, 0x00, 0xFF, 0x09, 0xF7, 0xD4, 0x00, 0x00, 0x6C, 0x69, 0x62, 0x6E, 0x66, 0x63, 0xBE, 0x00 };
unsigned char send_command_3[9] = { 0x00, 0x00, 0xFF, 0x02, 0xFE, 0xD4, 0x02, 0x2A, 0x00 };
unsigned char send_command_4[10] = { 0x00, 0x00, 0xFF, 0x03, 0xFD, 0xD4, 0x12, 0x14, 0x06, 0x00 };
unsigned char send_command_5[19] = { 0x00, 0x00, 0xFF, 0x0C, 0xF4, 0xD4, 0x06, 0x63, 0x02, 0x63, 0x03, 0x63, 0x0D, 0x63, 0x38, 0x63, 0x3D, 0xB0, 0x00 };
unsigned char send_command_6[15] = { 0x00, 0x00, 0xFF, 0x08, 0xF8, 0xD4, 0x08, 0x63, 0x02, 0x80, 0x63, 0x03, 0x80, 0x59, 0x00 };
unsigned char send_command_7[11] = { 0x00, 0x00, 0xFF, 0x04, 0xFC, 0xD4, 0x32, 0x01, 0x00, 0xF9, 0x00 };
unsigned char send_command_8[15] = { 0x00, 0x00, 0xFF, 0x08, 0xF8, 0xD4, 0x06, 0x63, 0x05, 0x63, 0x38, 0x63, 0x3D, 0x83, 0x00 };
unsigned char send_command_9[12] = { 0x00, 0x00, 0xFF, 0x05, 0xFB, 0xD4, 0x08, 0x63, 0x05, 0x04, 0xB8, 0x00 };
unsigned char send_command_10[50] = { 0x00, 0x00, 0xFF, 0x2B, 0xD5, 0xD4, 0x8C, 0x02, 0x08, 0x00, 0x12, 0x34, 0x56, 0x40, 0x01, 0xFE, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0x0F, 0xAB, 0x12, 0x34,0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0x00, 0x00, 0x04, 0x12, 0x34, 0x56, 0x78, 0x00, 0xD0, 0x00 };
unsigned char send_command_11[9] = { 0x00, 0x00, 0xFF, 0x02, 0xFE, 0xD4, 0x86, 0xA6, 0x00 };
unsigned char send_command_12[18] = { 0x00, 0x00, 0xFF, 0x0B, 0xF5, 0xD4, 0x8E, 0x34, 0x35, 0x36, 0x34, 0x35, 0x36, 0x34, 0x35, 0x36, 0xC1, 0x00 };

unsigned char *send_command[11] = { send_command_1 ,send_command_2 ,send_command_3 ,send_command_4 ,
send_command_5 ,send_command_6 ,send_command_7 ,send_command_8 ,send_command_9 ,send_command_10 ,send_command_11 };
unsigned char send_command_len[11] = { 26,16,9,10,19,15,11,15,12,50,9 };
unsigned char recv_command[20];
void print_debug_log(unsigned char * outbuf, int outbuf_size, unsigned char * recvbuf, int recvbuf_size)
{
	printf("send buf is : ");
	for (int i = 0; i < outbuf_size; i++)
	{
		printf("%02X ", outbuf[i]);
	}
	printf("\n*******************\nrecv: ");
	for (int i = 0; i < recvbuf_size; i++)
	{
		printf("%02X ", recvbuf[i]);
	}
	printf("\n-----------------------------------------------------------------\n");
}
int nfc_p2p(int fd,unsigned char *send, unsigned char *get)
{
	unsigned char buffer[1024];
	int ret;
	
	for (int i = 0; i < 10; i++)
	{
		usleep(10000);
		xserial_send(fd, (char *)send_command[i], send_command_len[i]);
		memset(buffer, 0, sizeof(buffer));
		usleep(10000);
		ret = xserial_recv(fd, buffer, sizeof(buffer), 0);
		if (ret <= 0)
		{
			printf("closed\n");
			xserial_close(&fd);
			return 0;
		}
		printf("ret num =%d \n", ret);
		print_debug_log(send_command[i], send_command_len[i], buffer, ret);
	}
	while (1)
	{
		ret = xserial_recv(fd, buffer, 20, 10000);
		if (ret <= 5)
			continue;
		for (int i = 0; i < ret; i++)
		{

			printf("%02X ", buffer[i]);
		}
		printf("\n");
		break;
	}
	printf("\nbreaked\n");
	usleep(1000000);
	xserial_send(fd, (char *)send_command_11, sizeof(send_command_11));
	memset(buffer, 0, sizeof(buffer));
	usleep(100000);
	ret = xserial_recv(fd, buffer, sizeof(buffer), 0);
	if (ret <= 0)
	{
		xserial_close(&fd);
		return 0;
	}
	print_debug_log(send_command_11, sizeof(send_command_11), buffer, ret);


	for (int i = 0; i < ret - 5; i++)
	{
		if ((buffer[i] == 0xd5) && (buffer[i + 1] == 0x87) && (buffer[i + 2] == 0x00))
		{
			memcpy(get, &buffer[i + 3], ret - i - 5);
			printf("recvss:%s\n\n", get);
		}
	}
	unsigned char sends[512] = { 0 };
	unsigned char send_num = (unsigned char)strlen((char *)send);
	//0x00, 0x00, 0xFF, 0x0B, 0xF5, 0xD4, 0x8E, 0x34, 0x35, 0x36, 0x34, 0x35, 0x36, 0x34, 0x35, 0x36, 0xC1, 0x00
	sends[0] = 0x00;
	sends[1] = 0x00;
	sends[2] = 0xff;
	sends[3] = send_num + 2;
	sends[4] = ~(send_num + 2) + 1;
	sends[5] = 0xd4;
	sends[6] = 0x8e;
	memcpy(&sends[7], send, send_num);
	unsigned char check_num = 0;
	check_num += sends[5];
	check_num += sends[6];
	for (int i = 0; i < send_num; i++)
	{
		check_num += send[i];
	}
	sends[7 + send_num] = ~check_num + 1;
	sends[8 + send_num] = 0x00;
	usleep(100000);
	xserial_send(fd, (char *)sends, 9 + send_num);
	memset(buffer, 0, sizeof(buffer));
	usleep(100000);
	ret = xserial_recv(fd, buffer, sizeof(buffer), 0);
	if (ret <= 0)
	{
		xserial_close(&fd);
		return 0;
	}
	print_debug_log(sends, 9 + send_num, buffer, ret);

	usleep(10000);
}

对于需要发送的数据如何进行封装,下面这个帖子有详细的介绍,也很简单。方便浏览 我搬过来了:
原文链接:https://blog.csdn.net/roctwo/article/details/78743259

1.PREAMBLE1 byte4,

这个就是所谓的帧头,也称为前导码,一般是00
2.START CODE2 bytes (0x00 and 0xFF),

开始码 2个字节,分别是 00和FF
3.LEN1 byte indicating the number of bytes in the data field
(TFI and PD0 to PDn),

这个是数据长度,一个TFI和n个PD
3.LCS1 Packet Length Checksum LCS byte that satisfies the relation:
Lower byte of [LEN + LCS] = 0x00,

这个是LEN的补码,也就是(LEN取反+1)
4.TFI1 byte frame identifier, the value of this byte depends
on the way of the message
- D4h in case of a frame from the host controller to the PN532,
- D5h in case of a frame from the PN532 to the host controller.

表示数据流向 D4 表示 数据发向PN532

D5 表示 PN532数据发出
5.DATALEN-1 bytes of Packet Data Information
The first byte PD0 is the Command Code,

搞了半天,这才是我们想要发的数据,DATA的第一个字节PD0为控制字符,其余为普通数据。该包长度为 LEN-1
6.DCS1 Data Checksum DCS byte that satisfies the relation:
Lower byte of [TFI + PD0 + PD1 + … + PDn + DCS] = 0x00,

DCS 其实最坑了,根本不知道怎么算出来的。其实就是把这些十六进制数加起来,后两位取补码即可。即(TFI + PD0 + PD1 + … + PDn)累加和,取后两位的补码。
POSTAMBLE1 byte2.

帧尾 一般为 00
The amount of data that can be exchanged using this frame structure is limited to 255
bytes (including TFI).
这种的数据结构只能有255 个data (包括TFI)

赞(3) 打赏
未经允许不得转载:huangea的博客 » PNP532 NFC P2P方法代码-PNP532命令抓包方式发送

评论 2

  1. #1

    您好,可以分享一下具体的指令发送顺序吗(上位机->PN532),我按开发手册里的指令,发给PN532,PN532没有反应,困扰好几天了,还烦请大佬解答

    zzz3年前 (2020-12-05)回复
    • 你好,发送记录可以参考我写的另一个帖子 《PN532 命令调试抓包记录 P2P方式》https://www.huangea.com/2019/01/08/pn532-%e5%91%bd%e4%bb%a4%e8%b0%83%e8%af%95%e6%8a%93%e5%8c%85%e8%ae%b0%e5%bd%95-p2p%e6%96%b9%e5%bc%8f/

      huangea3年前 (2021-01-08)回复

有趣的网站

支持快讯、专题、百度收录推送、人机验证、多级分类筛选器,适用于垂直站点、科技博客、个人站,扁平化设计、简洁白色、超多功能配置、会员中心、直达链接、文章图片弹窗、自动缩略图等...

联系我们联系我们

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册