数据协议发送与接收

最近在使用UWB与飞控进行数据通信的过程中,发现一些丢包现象,为此对协议接收进行总结。

发送端协议设计:

首先找到UWB project中串口发送缓存区,以本项目为例,待发送的数据都是存在下面u8类型数组中

u8 SendBuff[130];

通过数组找到向数组写入数据的段落

u8 i;	u8 sum = 0;//和校验
vs16 _temp;u8 SendBuff1_cnt=0;//字节数,8位
SendBuff[SendBuff1_cnt++]=0xAA;
SendBuff[SendBuff1_cnt++]=0xAF;//AAAF为帧头
SendBuff[SendBuff1_cnt++]=0x67;//功能字
SendBuff[SendBuff1_cnt++]=0;//数据长度,发送结束前赋值

_temp = (int)(instancegetidist_mm(0));
					
SendBuff[SendBuff1_cnt++]=BYTE1(_temp);
SendBuff[SendBuff1_cnt++]=BYTE0(_temp);
_temp = (int)(instancegetidist_mm(1));
				
SendBuff[SendBuff1_cnt++]=BYTE1(_temp);
SendBuff[SendBuff1_cnt++]=BYTE0(_temp);
_temp = (int)(instancegetidist_mm(2));
					
SendBuff[SendBuff1_cnt++]=BYTE1(_temp);
SendBuff[SendBuff1_cnt++]=BYTE0(_temp);
_temp = (int)(instancegetidist_mm(3));
				
SendBuff[SendBuff1_cnt++]=BYTE1(_temp);
SendBuff[SendBuff1_cnt++]=BYTE0(_temp);
					
SendBuff[3] = SendBuff1_cnt-4;//总字节数
for( i=0;i

其中BYTE是一个宏定义,因为发送的数据是两个字节,而串口一次传输只能进行一字节,所以要进行拆分

#define BYTE0(dwTemp)       (*(char *)(&dwTemp))
#define BYTE1(dwTemp)       (*((char *)(&dwTemp) + 1))
#define BYTE2(dwTemp)       (*((char *)(&dwTemp) + 2))
#define BYTE3(dwTemp)       (*((char *)(&dwTemp) + 3))

接收部分代码:

待补充

你可能感兴趣的:(stm32)