This repository was archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage.cs
More file actions
213 lines (187 loc) · 6.29 KB
/
Copy pathpackage.cs
File metadata and controls
213 lines (187 loc) · 6.29 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
using System;
using System.Collections;
using System.IO.Ports;
using System.Drawing;
using System.Windows.Forms;
using System.Text;
/// <summary>
/// Summary description for Class1
/// </summary>
namespace UperMonitor
{
public class PackageManager
{
public const byte ESC = 0x5c, SOH = 0x01, EOT = 0x04;
public int rxCount = 0, txCount = 0;
public ArrayList SpeedBuf = new ArrayList(),
SteerBuf = new ArrayList(), MagBuf = new ArrayList();
private byte lastByte = EOT, bufTop = 0;
private byte[] rxBuffer = new byte[99];
public void PackAndPush(SerialPort port, byte id, byte[] payload)
{
byte[] package = new byte[payload.Length + 4];
payload.CopyTo(package, 2);
package[0] = SOH;
package[1] = id;
package[package.Length - 1] = EOT;
package[package.Length - 2] = ParitySum(payload);
try
{
port.Write(package, 0, package.Length);
}
catch (Exception ex)
{
MessageBox.Show("无法写入串口:" + ex.Message);
}
txCount += package.Length;
}
private bool PushToBuffer(byte b)
{
if (rxBuffer.Length > bufTop + 1)
rxBuffer[bufTop++] = b;
else
return false;
return true;
}
public bool VerifySOH(byte newByte)
{ // 找到帧头
++rxCount;
bool ret = (lastByte != ESC && newByte == SOH);
lastByte = newByte;
return ret;
}
private bool esc = false; // 是否转义
public bool VerifyEOT(byte newByte, MainForm owner)
{ // 找到帧尾
++rxCount;
if (!esc && newByte == EOT && CheckParity())
{ // 检测到帧尾
AnalysePackages(owner);
}
else if (!esc && newByte == ESC)
{
esc = true; // 转义下一个字节
return false;
}
else
{
esc = false;
if (PushToBuffer(newByte))
return false; // 继续读当前帧
}
bufTop = 0;
return true; // 超出缓冲区,寻找下一个包
}
private void AnalysePackages(MainForm owner)
{ // 解析当前帧,判定是那个数据包
SpeedPackage speed = new SpeedPackage();
SteerPackage steering = new SteerPackage();
DeepPackage deep = new DeepPackage();
switch (rxBuffer[0])
{
case 0x8a: // 方向
steering.DecodePayload(rxBuffer);
SteerBuf.Add(steering);
break;
case 0x8b: // 速度
speed.DecodePayload(rxBuffer);
if (SpeedBuf.Count > 999)
SpeedBuf.RemoveAt(0);
SpeedBuf.Add(speed);
owner.BeginInvoke(new MethodInvoker(() =>
{
owner.lblSpeedL.Text = "左轮速度:" + speed.SpeedL.ToString() + " cm/s";
owner.lblSpeedR.Text = "右轮速度:" + speed.SpeedR.ToString() + " cm/s";
}));
break;
case 0x8c: // AI
deep.DecodePayload(rxBuffer);
MagBuf.Add(deep);
break;
case 0x8d: // 开关
owner.BeginInvoke(new MethodInvoker(() =>
{
for (int i = 0; i < 8; ++i)
owner.checkedListBox.SetItemChecked(i, (rxBuffer[1] & (1 << i)) > 0);
}));
break;
case 0x8e: // 电磁
break;
case 0x8f: // 日志
string str = Encoding.UTF8.GetString(rxBuffer, 2, bufTop - 3);
Color color = Color.Black;
switch (rxBuffer[1])
{
case 1: color = Color.Red; break;
case 2: color = Color.Green; break;
case 3: color = Color.Blue; break;
}
owner.BeginInvoke(new MethodInvoker(() =>
{
owner.richLogBox.SelectionColor = color;
owner.richLogBox.AppendText(str);
}));
break;
default: break;
}
}
public byte ParitySum(byte[] arr)
{
byte sum = 0;
foreach (byte i in arr)
sum += i;
return sum;
}
private bool CheckParity()
{
if (bufTop < 3)
return false;
byte[] p = new byte[bufTop - 2];
Array.Copy(rxBuffer, 1, p, 0, bufTop - 2);
return ParitySum(p) == rxBuffer[bufTop - 1];
}
}
public class PIDPackage
{
public float Kp, Ki, Kd;
public byte[] Payload = new byte[12];
public void EncodePayload()
{
Array.Copy(BitConverter.GetBytes(Kp), 0, Payload, 0, 4);
Array.Copy(BitConverter.GetBytes(Ki), 0, Payload, 4, 4);
Array.Copy(BitConverter.GetBytes(Kd), 0, Payload, 8, 4);
}
}
class DeepPackage
{
public byte steering;
public byte[] sensor;
public void DecodePayload(byte[] payload)
{
steering = payload[1];
sensor = new byte[7];
for (int i = 0; i < 7; ++i)
sensor[i] = payload[i + 2];
}
}
class SpeedPackage
{
public int SpeedL, SpeedR, DutyL, DutyR;
public void DecodePayload(byte[] payload)
{
SpeedL = (Int16)(payload[1] << 8) | payload[2];
SpeedR = (Int16)(payload[3] << 8) | payload[4];
DutyL = payload[5];
DutyR = payload[6];
}
}
class SteerPackage
{
public int Width, Error, Left, Middle, Right;
public void DecodePayload(byte[] payload)
{
Width = payload[1];
Error = payload[2];
}
}
}