-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathultrasonic.cpp
62 lines (55 loc) · 1.5 KB
/
ultrasonic.cpp
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
#include "ultrasonic.h"
#include <QDebug>
ultrasonic::ultrasonic()
{
data_structre = {0};
step = 0;
}
bool ultrasonic::data_analysis(QByteArray buf)
{
for(int i=0;i<buf.size();i++)
{
again_this_loop:
switch (step)
{
case 0: //获取头
{
if(uint8_t(buf.at(i))==HEAD_1){
data_structre.buff[step] = buf.at(i);
step++;
}
}break;
case 1: //获取头
{
if(buf.at(i)==HEAD_2){
data_structre.buff[step] = buf.at(i);
step++;
}
}break;
case DATA_LEN: //接校验和
{
step = 0;
uint16_t sum = 0;
for(int i=0;i<DATA_LEN;i++){
if(i==2||i==3){continue;}
sum += data_structre.buff[i];
}
sum = 0x10000 - sum;
if(sum == data_structre.sum){ //解码成功
for(int i=0;i<ULTRA_NUM;i++)
{
qDebug()<<data_structre.data[i];
}
return true;
}
}break;
default:
{
data_structre.buff[step] = buf.at(i);
step++;
if(step==DATA_LEN) {goto again_this_loop;}
}break;
}
}
return false;
}