数据协议发送与接收

最近在使用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<SendBuff1_cnt;i++)
	sum += SendBuff[i];
SendBuff[SendBuff1_cnt++] = sum;

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

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

接收部分代码:

待补充

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值