dataParsing.c
4.1 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "dataParsing.h"
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"
#include "my_ringbuff.h"
#include "string.h"
#include "stdio.h"
#include "jz_upgrade.h"
#include "my_typedef.h"
#define UART_RECIVE_BUFF_MAX (1024*5)
#define LENGTH_BIT_H 3
#define LENGTH_BIT_L 4
#define ANSWER_BIT 5
#define FUNC_BIT_1 7
#define FUNC_BIT_2 8
#define DATA_BIT 9
osThreadId_t ParsingTaskHandle;
const osThreadAttr_t ParsingTask_attributes = {
.name = "ParsingTask",
.stack_size = 1024,
.priority = (osPriority_t) osPriorityNormal,
};
ringBuf_t* Uart1RingBufHandle;
s_getFrame FrameStru = {0};
/********************** function **********************/
static void DataParsingTask(void *argument);
static int8_t FrameParsing(uint8_t* frame, uint16_t len);
//创建数据处理任务
int8_t Create_Data_Parsing(void)
{
//
UpgradeTest_Init();
//初始化队列
Uart1RingBufHandle = ringBufInit(Uart1RingBufHandle,UART_RECIVE_BUFF_MAX);
// SEGGER_RTT_WriteString(0,"come data parsing\r\n");
// printf("come data parsing\r\n");
//创建缓存数据解析任务
ParsingTaskHandle = osThreadNew(DataParsingTask, NULL, &ParsingTask_attributes);
// printf("%d %d\r\n",FrameStru.flag,FrameStru.count);
return 0;
}
//数据解析 (缓存数据提取数据帧)
static void DataParsingTask(void *argument)
{
int16_t getLen = 0;
uint16_t readLen = 1; //开始每次读一个字节,确定长度位之后再改变
uint16_t dataLen = 0;
char byte[512] = {0};
uint32_t frameHead = 0x5a5a77;
while(1)
{
if(FrameStru.flag==1)
continue;
memset(byte,0x00,sizeof(byte));
//读缓存队列数据到 byte,返回实际读取的数据长度
getLen = ringBufRead(Uart1RingBufHandle,byte,readLen);
//缓存队列存在数据
if(getLen>0)
{
// printf("come\r\n");
//数据转存
memcpy(FrameStru.buff+FrameStru.count, byte, getLen);
FrameStru.count += getLen;
if(FrameStru.count<=5)
{
//帧头判断
if(FrameStru.count==3)
{
if((frameHead!=(FrameStru.buff[0]<<16)+(FrameStru.buff[1]<<8)+FrameStru.buff[2]))
{
memcpy(&FrameStru.buff[0],&FrameStru.buff[1],2);
FrameStru.count--;
continue;
}
}
//帧长度判断
if(FrameStru.count==5)
{
dataLen = FrameStru.buff[3]*256+FrameStru.buff[4];
if(readLen>512)
goto errorloop;
}
}
else
{
readLen = dataLen - getLen - 5;
if(readLen<1)
readLen = 1;
if(FrameStru.count >= dataLen)
{
if(FrameStru.buff[dataLen-2]==0x00 && FrameStru.buff[dataLen-1]==0x23)
{
// FrameStru.flag = 1; //接收满一帧标志
FrameParsing(FrameStru.buff,FrameStru.count);
// frameCount++;
// printf("a frame\r\n");
goto errorloop;
}
else
goto errorloop;
}
}
}
continue;
errorloop:
readLen = 1;
getLen = 0;
FrameStru.count= 0;
memset(FrameStru.buff,0x00,sizeof(FrameStru.buff));
FrameStru.flag = 0;
}
}
//帧解析(帧实现)
static int8_t FrameParsing(uint8_t* frame, uint16_t len)
{
uint8_t diviceName = DEVICE_NAME;
uint8_t result = 0;
//连接帧
uint8_t connectBuff[] = {0x5a,0x5a,0x77,0x00,0x0c,0x01,0x00,0x50,0x51,0x00,0x00,0x23};
uint8_t versionBuff[] = {0x5a,0x5a,0x77,0x00,0x0c,0x01,0x00,0x53,0xf1,0x00,0x00,0x23};
if(frame[FUNC_BIT_1]==0XA1)
Upgrade_Manager(frame,len);
else
{
if(frame[0]==0x5a)
{
//连接帧
if(memcmp(connectBuff,frame,12)==0&&frame[ANSWER_BIT]==0x01)
{
Make5BFrame_Test(0x50,0x51,&diviceName,1);
}
//版本查询
else if(memcmp(versionBuff,frame,12)==0&&frame[ANSWER_BIT]==0x01)
{
Make5BFrame_Test(0x53,0xf1,VersionNum,sizeof(VersionNum));
}
}
}
return 0;
}
int16_t point_2_vel_1(uint8_t high, uint8_t low)
{
int16_t ret = 0;
int16_t temp = 0;
temp = high*256 + low;
// printf("%d\r\n",temp);
// LL_USART_ClearFlag_ORE(USART2);
ret = 2048 - (temp*2048)/32768;
// printf("%d\r\n",(high>>5)&0x01);
return ret;
}
int16_t point_2_vel(uint8_t high, uint8_t low)
{
int16_t ret = 0;
int16_t temp = 0;
temp = high*256 + low;
// printf("%d\r\n",temp);
// LL_USART_ClearFlag_ORE(USART2);
ret = 2048 - (temp*2048)/32768;
// printf("%d\r\n",(high>>5)&0x01);
return ret;
}