一、流程图
二、详细代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80//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++内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复