-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadar.cs
More file actions
329 lines (307 loc) · 12.3 KB
/
Copy pathRadar.cs
File metadata and controls
329 lines (307 loc) · 12.3 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading.Tasks;
namespace RADARMRM
{
class Radar
{
byte operationMode;
int radarNO;
int udpPort;
Form1 uiForm;
UdpClient socket;
IPEndPoint receiveIPEnd;
byte[] sendPacket = new byte[32];
byte[] receivePacket;
int sendLength;
int oldScanLength = 0;
int[] fullScan, fullRScan;
int lastMessageIndex = 0;
int thisMessageID = 0;
int messageHandled = 1;
bool starting, stopping;
public bool MRMShortRange { get; set; }
public byte MRMRange { get; set; }
public byte MRMCodeChannel { get; set; }
public bool MRMBackgroundReady { get; set; }
public int CATUnDetect { get; set; }
public UInt32 CATTarget { get; set; }
public UInt32[] MRMTarget { get; set; }
public bool MRMConfirmed { get; set; }
public UInt32 MRMCloseTarget { get; set; }
public Int16 CATSensitivity { get; set; }
public Int16 MRMSensitivity { get; set; }
public UInt16 DetectionNO { get; set; }
public int LifeSensitivity { get; set; }
public Single LifeStart { get; set; }
public Single LifeRange { get; set; }
public int LifeAverageDepth { get; set; }
public bool LifeRunning { get; private set; }
public int LifeSections { get; private set; }
public int CurrentLifeSection { get; private set; }
public Single LifeDetectedDistance { get; set; }
public byte OperationMode { get { return operationMode; } set { operationMode = value; } }
public Radar(IPEndPoint ip, Form1 ui, int no)
{
radarNO = no;
LifeRunning = false;
LifeSections = 0;
LifeDetectedDistance = -1;
CATUnDetect = 1;
operationMode = 1;
MRMConfirmed = false;
MRMBackgroundReady = false;
MRMTarget = new UInt32[4];
MRMRange = 20;
udpPort = ip.Port;
socket = new UdpClient();
socket.Connect(ip);
Task.Factory.StartNew(Receiving, TaskCreationOptions.LongRunning);
uiForm = ui;
}
void Receiving()
{
while (true)
{
receivePacket = socket.Receive(ref receiveIPEnd);
Array.Reverse(receivePacket);
UInt16 msgType = BitConverter.ToUInt16(receivePacket, receivePacket.Length - 2);
UInt16 msgID = BitConverter.ToUInt16(receivePacket, receivePacket.Length - 4);
switch (msgType)
{
case 0x1101:
ConfigurationConfirm();
break;
case 0x1102:
ControlConfirm();
break;
case 0x1103:
RemoteConfirm();
break;
case 0x1111:
MRMBackgroundConfirm();
break;
case 0x1202:
if (FullScan())
{
if (radarNO == 1)
uiForm.UpdateData(fullScan);
}
break;
case 0x1203:
if (FullScan())
{
uiForm.UpdateStatus("Reference received.\n");
Buffer.BlockCopy(fullScan, 0, fullRScan, 0, fullRScan.Length * 4);
uiForm.UpdateRData(fullRScan);
}
break;
case 0x1204:
CATInfoConfirm();
break;
case 0x1205:
MRMInfoConfirm();
break;
default:
break;
}
}
}
void Request()
{
Array.Reverse(sendPacket, 0, sendLength);
Task<int> task = Task<int>.Factory.FromAsync(socket.BeginSend, socket.EndSend, sendPacket, sendLength, null);
}
public void ConfigurationRequest()
{
UInt16 msgType = 0x1001;
sendLength = 12;
sendPacket[7] = operationMode;
sendPacket[6] = MRMCodeChannel;
BitConverter.GetBytes(CATSensitivity).CopyTo(sendPacket, 4);
BitConverter.GetBytes(MRMSensitivity).CopyTo(sendPacket, 2);
sendPacket[1] = MRMRange;
BitConverter.GetBytes(msgType).CopyTo(sendPacket, 10);
Request();
}
public void RemoteRequest(byte mrmRange1, byte sensitivity1, byte averageDepth1, int start1, byte mrmRange2, byte sensitivity2, byte averageDepth2, int start2)
{
UInt16 msgType = 0x1003;
sendLength = 20;
BitConverter.GetBytes(msgType).CopyTo(sendPacket, 18);
sendPacket[15] = mrmRange1;
sendPacket[14] = sensitivity1;
sendPacket[13] = averageDepth1;
BitConverter.GetBytes(start1).CopyTo(sendPacket, 8);
sendPacket[7] = mrmRange2;
sendPacket[6] = sensitivity2;
sendPacket[5] = averageDepth2;
BitConverter.GetBytes(start2).CopyTo(sendPacket, 0);
Request();
}
void ConfigurationConfirm()
{
UInt32 status = BitConverter.ToUInt32(receivePacket, 0);
if (status == 0)
uiForm.UpdateStatus(radarNO.ToString() + "号雷达配置成功");
else
uiForm.UpdateStatus(radarNO.ToString() + "号雷达配置失败");
}
void CATInfoConfirm()
{
CATUnDetect = 0;
CATTarget = BitConverter.ToUInt32(receivePacket, 0);
uiForm.UpdateStatus("CAT detected at " + CATTarget.ToString() + ".\n");
}
void MRMInfoConfirm()
{
UInt32 detected = BitConverter.ToUInt32(receivePacket, 12);
CurrentLifeSection = BitConverter.ToUInt16(receivePacket, 18);
if (detected != 0)
{
LifeDetectedDistance = ((float)BitConverter.ToUInt32(receivePacket, 8)) * 0.00914891f + 0.8f * (float)CurrentLifeSection + LifeStart;
//uiForm.UpdateStatus(radarNO.ToString() + "号雷达探测到目标位于" + LifeDetectedDistance.ToString("f2") + "米\n");
LifeRunning = false;
}
else
uiForm.UpdateStatus(radarNO.ToString() + "号雷达正在扫描第" + CurrentLifeSection.ToString() + "段\n");
}
void ControlConfirm()
{
UInt32 status = BitConverter.ToUInt32(receivePacket, 0);
if (status == 0)
{
uiForm.UpdateStatus(radarNO.ToString() + "号雷达正常响应");
if (starting)
{
LifeRunning = true;
starting = false;
}
if (stopping)
{
LifeRunning = false;
stopping = false;
}
}
else if (status == 3)
{
LifeRunning = false;
uiForm.UpdateStatus(radarNO.ToString() + "号雷达探测完成");
}
else if (status == 100)
{
LifeRunning = false;
uiForm.UpdateStatus("远程遥控停止\n");
}
else if (status > 100) //long vehicle local start
{
LifeRunning = true;
CurrentLifeSection = 0;
LifeSections = (int)status - 100;
LifeDetectedDistance = -1;
uiForm.RemoteStartAsync();
uiForm.UpdateStatus("远程遥控开始\n");
//uiForm.UpdateReport(String.Format("{0,-24}{1,-10}{2,-22}{3,-24}\n", DateTime.Now, "XXXXXXX", "正在扫描" + LifeSections.ToString() + "段中第" + (CurrentLifeSection + 1).ToString() + "段", "XXX"));
}
else
uiForm.UpdateStatus("生命探测仪控制异常\n" + status.ToString());
}
void RemoteConfirm()
{
UInt32 status = BitConverter.ToUInt32(receivePacket, 0);
if (status == 0)
uiForm.UpdateStatus("设置保存至雷达成功\n");
else
uiForm.UpdateStatus("设置保存至雷达异常\n");
}
void MRMBackgroundConfirm()
{
UInt32 status = BitConverter.ToUInt32(receivePacket, 0);
if (status == 0)
{
MRMBackgroundReady = true;
uiForm.UpdateStatus(radarNO.ToString() + "号雷达待命\n");
}
else
uiForm.UpdateStatus(radarNO.ToString() + "号雷达待命\n");
}
public void StartLife()
{
LifeSections = (int)((LifeRange + 0.7) / 0.8);//ignore last section if its length is less than 0.1 m;
int start = 11000 + (int)(LifeStart * 6671);
CurrentLifeSection = 0;
LifeDetectedDistance = -1;
starting = true;
UInt16 msgType = 0x1002;
sendLength = 12;
sendPacket[7] = 6;
sendPacket[6] = (byte)LifeSections;
sendPacket[5] = (byte)LifeSensitivity;
sendPacket[4] = (byte)LifeAverageDepth;
BitConverter.GetBytes(start).CopyTo(sendPacket, 0);
BitConverter.GetBytes(msgType).CopyTo(sendPacket, 10);
Request();
}
public void Stop()
{
stopping = true;
UInt16 msgType = 0x1002;
sendLength = 12;
sendPacket[7] = 0;
BitConverter.GetBytes(msgType).CopyTo(sendPacket, 10);
Request();
}
bool FullScan()
{
int packetLength = receivePacket.Length;
UInt16 messageID = BitConverter.ToUInt16(receivePacket, packetLength - 4);
UInt16 messageIndex = BitConverter.ToUInt16(receivePacket, packetLength - 6);
UInt16 totalNumberOfMessages = BitConverter.ToUInt16(receivePacket, packetLength - 8);
UInt16 numberOfSamplesInThisMessage = BitConverter.ToUInt16(receivePacket, packetLength - 10);
UInt16 totalNumberOfScanSamples = BitConverter.ToUInt16(receivePacket, packetLength - 12);
if (totalNumberOfScanSamples != oldScanLength)
{
oldScanLength = totalNumberOfScanSamples;
fullScan = new int[oldScanLength];
fullRScan = new int[oldScanLength];
}
if (messageIndex == 0)
{
Buffer.BlockCopy(receivePacket, (512 - numberOfSamplesInThisMessage) * 4, fullScan, (int)totalNumberOfScanSamples * 4 - numberOfSamplesInThisMessage * 4, numberOfSamplesInThisMessage * 4);
if (totalNumberOfMessages == 1)
{
Array.Reverse(fullScan);
return true;
}
else
{
lastMessageIndex = messageIndex;
thisMessageID = messageID;
messageHandled = 1;
}
}
else
{
if (messageID == thisMessageID)
{
if (messageIndex == (lastMessageIndex + 1))
{
Buffer.BlockCopy(receivePacket, (512 - numberOfSamplesInThisMessage) * 4, fullScan, (int)totalNumberOfScanSamples * 4 - messageIndex * 2048 - numberOfSamplesInThisMessage * 4, numberOfSamplesInThisMessage * 4);
lastMessageIndex++;
messageHandled++;
if (messageHandled == totalNumberOfMessages)
{
Array.Reverse(fullScan);
return true;
}
}
}
}
return false;
}
}
}