-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommunication.cpp
More file actions
90 lines (77 loc) · 2.58 KB
/
Copy pathcommunication.cpp
File metadata and controls
90 lines (77 loc) · 2.58 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
#include "communication.h"
#include <iostream>
Communication::Communication() {
}
void Communication::Communication_init(GetData_Interface *Get_data, SendData_Interface *Send_data) {
GetDataInterface = Get_data;
GetDataInterface->Get_data_init();
SendDataInterface = Send_data;
SendDataInterface->Send_data_init();
}
// 获取接收的数据
uint8_t *Communication::Get_Data(void) {
return (uint8_t *) GetDataInterface->data_buffer;
}
// 获取命令长度
uint8_t Communication::Get_Data_Length(void) {
return GetDataInterface->data_length;
}
// 获取命令标志
uint8_t Communication::Get_DataAccept_Flag(void) {
return GetDataInterface->data_accept_flag;
}
// 获取事件的标志
uint8_t Communication::Get_DataEvent_Id(void) {
return GetDataInterface->data_event_id;
}
// 清除命令数据和相关标志
void Communication::Clear_Data(void) {
for (uint8_t i = 0; i < GetDataInterface->data_length; i++) {
GetDataInterface->data_buffer[i] = 0;
}
GetDataInterface->data_length = 0;
GetDataInterface->data_accept_flag = 0;
GetDataInterface->data_event_id = 0;
}
// 处理得到的数据,并返回标志位
Date_Handle Communication::data_handle() {
Date_Handle Date_Handle_Temp = {no_event, nullptr};
//没有接收到数据就退出
if (Get_DataAccept_Flag()) {
//如果数据校正失败也退出
Data_Buffer data_temp = {0, nullptr};
data_temp.data = Get_Data();
data_temp.length = Get_Data_Length();
if (!(GetDataInterface->Data_correction(data_temp)))
return Date_Handle_Temp;
} else {
return Date_Handle_Temp;
}
Date_Handle_Temp.event_data = GetDataInterface->data_buffer;
switch (Get_DataEvent_Id()) {
/******************这里根据自己的通信协议写不同的返回值,如下所示********************/
// case ServoControl_Sign:
// Date_Handle_Temp.event = ServoControl_event;
// break;
default:
break;
}
return Date_Handle_Temp;
}
void Communication::Send_protocol_data(Data_Buffer send_data, Send_Data_Type SendDataType) {
#if use_dma == 0
if (SendDataType != SendData_normal){
//添加通讯协议
SendDataInterface->Add_protocol(send_data, SendDataType);
//发送帧头
SendDataInterface->Send_data(SendDataInterface->data_head);
}
#endif
SendDataInterface->Send_data(send_data);
#if use_dma == 0
if (SendDataType != SendData_normal){
//发送帧尾
SendDataInterface->Send_data(SendDataInterface->data_end);
}
#endif
}