概述
一、流程图
二、详细代码
//begin解析数据包
static uint8_t parse_packet_buf[20480];
static int16_t parse_packet_buf_index = 0;
static uint8_t resolver_num_packets = 0;
static void add_parse_packet_buf(uint8_t* data, uint16_t len)
{
if(len<(20480-parse_packet_buf_index))
{
memcpy(&parse_packet_buf[parse_packet_buf_index], data, len);
parse_packet_buf_index+=len;
}
}
static void delete_parse_packet_buf(uint16_t len)
{
memcpy(parse_packet_buf, &parse_packet_buf[len], parse_packet_buf_index-len);
parse_packet_buf_index-=len;
}
static uint16_t resolver_delete(uint8_t* buff)
{
int16_t byte_count = 0;
int16_t data_len = 0;
uint8_t check_sum = 0;
uint8_t recv_checksum = 0;
if(parse_packet_buf_index<=5)
{
return byte_count;
}
if ((buff[0] == 0xAA) && (buff[1] == 0x55))
{
data_len = (buff[3] << 8) + buff[4];
if ((data_len != 0) && (data_len < 1024))
{
recv_checksum = get_checksum(&buff[5], data_len);
check_sum = buff[2];
byte_count = data_len + 5;
if (recv_checksum == check_sum)
{
resolver_num_packets++;
printf("check succesrn");
}
else
{
//CheckSum错误
//滑动丢弃无用字节
byte_count = 1;
printf("check errrn");
}
}
else
{
//滑动丢弃无用字节
byte_count = 1;
}
}
return byte_count;
}
static uint8_t parse_packet(uint8_t* data, uint16_t len)
{
uint16_t delete_len = 0;
resolver_num_packets = 0;
add_parse_packet_buf(data,len);
while(1)
{
delete_len = resolver_delete(parse_packet_buf);
if(delete_len == 0)
{
break;
}
else
{
delete_parse_packet_buf(delete_len);
}
}
return resolver_num_packets;
}
//end解析数据包
最后
以上就是完美眼神为你收集整理的C/C++ 协议解析通用流转包方法不怕连包一、流程图二、详细代码 的全部内容,希望文章能够帮你解决C/C++ 协议解析通用流转包方法不怕连包一、流程图二、详细代码 所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复