forked from ArduPilot/MissionPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTerminal.cs
768 lines (665 loc) · 24.6 KB
/
Terminal.cs
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using log4net;
using MissionPlanner.Comms;
using MissionPlanner.Controls;
using MissionPlanner.Log;
using MissionPlanner.Utilities;
using SerialPort = MissionPlanner.Comms.SerialPort;
namespace MissionPlanner.GCSViews
{
public partial class Terminal : MyUserControl, IActivate, IDeactivate
{
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
internal static ICommsSerial comPort;
public static bool threadrun;
private readonly List<string> cmdHistory = new List<string>();
private readonly object thisLock = new object();
private int history;
private bool inlogview;
private int inputStartPos;
DateTime lastsend = DateTime.MinValue;
public Terminal()
{
threadrun = false;
InitializeComponent();
}
public void Activate()
{
MainV2.instance.MenuConnect.Visible = false;
}
public void Deactivate()
{
try
{
if (comPort.IsOpen)
{
//comPort.Write("\rexit\rreboot\r");
comPort.Close();
}
}
catch
{
}
MainV2.instance.MenuConnect.Visible = true;
}
private void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (!comPort.IsOpen)
return;
// if btr > 0 then this shouldnt happen
comPort.ReadTimeout = 300;
try
{
lock (thisLock)
{
var buffer = new byte[256];
var a = 0;
while (comPort.IsOpen && comPort.BytesToRead > 0 && !inlogview)
{
var indata = (byte) comPort.ReadByte();
buffer[a] = indata;
if (buffer[a] >= 0x20 && buffer[a] < 0x7f || buffer[a] == '\n' || buffer[a] == 0x1b)
{
a++;
}
if (indata == '\n')
break;
if (a == (buffer.Length - 1))
break;
}
addText(Encoding.ASCII.GetString(buffer, 0, a + 1));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
if (!threadrun) return;
TXT_terminal.AppendText("Error reading com port\r\n");
}
}
private void addText(string data)
{
BeginInvoke((MethodInvoker) delegate
{
if (this.Disposing)
return;
if (inputStartPos > TXT_terminal.Text.Length)
inputStartPos = TXT_terminal.Text.Length - 1;
// gather current typed data
string currenttypedtext = TXT_terminal.Text.Substring(inputStartPos,
TXT_terminal.Text.Length - inputStartPos);
// remove typed data
TXT_terminal.Text = TXT_terminal.Text.Remove(inputStartPos, TXT_terminal.Text.Length - inputStartPos);
TXT_terminal.SelectionStart = TXT_terminal.Text.Length;
data = data.TrimEnd('\r'); // else added \n all by itself
data = data.Replace("\0", "");
data = data.Replace((char) 0x1b + "[K", ""); // remove control code
TXT_terminal.AppendText(data);
if (data.Contains("\b"))
{
TXT_terminal.Text = TXT_terminal.Text.Remove(TXT_terminal.Text.IndexOf('\b'));
TXT_terminal.SelectionStart = TXT_terminal.Text.Length;
}
// erase to end of line. in our case jump to end of line
if (data.Contains((char) 0x1b + "[K"))
{
TXT_terminal.SelectionStart = TXT_terminal.Text.Length;
}
inputStartPos = TXT_terminal.SelectionStart;
//add back typed text
TXT_terminal.AppendText(currenttypedtext);
});
}
private void TXT_terminal_Click(object sender, EventArgs e)
{
// auto scroll
//TXT_terminal.SelectionStart = TXT_terminal.Text.Length;
//TXT_terminal.ScrollToCaret();
//TXT_terminal.Refresh();
}
private void TXT_terminal_KeyDown(object sender, KeyEventArgs e)
{
TXT_terminal.SelectionStart = TXT_terminal.Text.Length;
/* if (e.KeyData == Keys.Up || e.KeyData == Keys.Down || e.KeyData == Keys.Left || e.KeyData == Keys.Right)
{
e.Handled = true; // ignore it
}*/
lock (thisLock)
{
switch (e.KeyData)
{
case Keys.Up:
if (history > 0)
{
TXT_terminal.Select(inputStartPos, TXT_terminal.Text.Length - inputStartPos);
TXT_terminal.SelectedText = "";
TXT_terminal.AppendText(cmdHistory[--history]);
}
e.Handled = true;
break;
case Keys.Down:
if (history < cmdHistory.Count - 1)
{
TXT_terminal.Select(inputStartPos, TXT_terminal.Text.Length - inputStartPos);
TXT_terminal.SelectedText = "";
TXT_terminal.AppendText(cmdHistory[++history]);
}
e.Handled = true;
break;
case Keys.Left:
case Keys.Back:
if (TXT_terminal.SelectionStart <= inputStartPos)
e.Handled = true;
break;
//case Keys.Right:
// break;
}
}
}
private void Terminal_FormClosing(object sender, FormClosingEventArgs e)
{
threadrun = false;
try
{
if (comPort != null && comPort.IsOpen)
{
comPort.Close();
}
}
catch
{
} // Exception System.IO.IOException: The specified port does not exist.
//System.Threading.Thread.Sleep(400);
}
private void TXT_terminal_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
{
if (comPort.IsOpen)
{
try
{
var cmd = "";
lock (thisLock)
{
if (MainV2.MONO)
{
cmd = TXT_terminal.Text.Substring(inputStartPos,
TXT_terminal.Text.Length - inputStartPos);
}
else
{
cmd = TXT_terminal.Text.Substring(inputStartPos,
TXT_terminal.Text.Length - inputStartPos - 1);
}
TXT_terminal.Select(inputStartPos, TXT_terminal.Text.Length - inputStartPos);
TXT_terminal.SelectedText = "";
if (cmd.Length > 0 && (cmdHistory.Count == 0 || cmdHistory.Last() != cmd))
{
cmdHistory.Add(cmd);
history = cmdHistory.Count;
}
}
log.Info("Command: " + cmd);
// do not change this \r is correct - no \n
if (cmd == "+++")
{
comPort.Write(Encoding.ASCII.GetBytes(cmd), 0, cmd.Length);
lastsend = DateTime.Now;
}
else
{
comPort.Write(Encoding.ASCII.GetBytes(cmd + "\r"), 0, cmd.Length + 1);
lastsend = DateTime.Now;
}
}
catch
{
CustomMessageBox.Show(Strings.ErrorCommunicating, Strings.ERROR);
}
}
}
/*
if (comPort.IsOpen)
{
try
{
comPort.Write(new byte[] { (byte)e.KeyChar }, 0, 1);
}
catch { MessageBox.Show("Error writing to com port"); }
}
e.Handled = true;*/
}
private void waitandsleep(int time)
{
var start = DateTime.Now;
while ((DateTime.Now - start).TotalMilliseconds < time && !inlogview)
{
try
{
if (!comPort.IsOpen || comPort.BytesToRead > 0)
{
return;
}
}
catch
{
threadrun = false;
return;
}
}
}
private void readandsleep(int time)
{
var start = DateTime.Now;
while ((DateTime.Now - start).TotalMilliseconds < time && !inlogview)
{
try
{
if (!comPort.IsOpen)
return;
if (comPort.BytesToRead > 0)
{
comPort_DataReceived(null, null);
}
}
catch
{
threadrun = false;
return;
}
}
}
private void Terminal_Load(object sender, EventArgs e)
{
setcomport();
}
private void setcomport()
{
if (comPort == null)
{
try
{
comPort = new SerialPort();
comPort.PortName = MainV2.comPortName;
comPort.BaudRate = int.Parse(MainV2._connectionControl.CMB_baudrate.Text);
comPort.ReadBufferSize = 1024*1024*4;
}
catch
{
CustomMessageBox.Show(Strings.InvalidBaudRate, Strings.ERROR);
}
}
}
private void start_Terminal(bool px4)
{
setcomport();
try
{
if (MainV2.comPort != null && MainV2.comPort.BaseStream != null && MainV2.comPort.BaseStream.IsOpen)
MainV2.comPort.BaseStream.Close();
if (comPort.IsOpen)
{
Console.WriteLine("Terminal Start - Close Port");
threadrun = false;
// if (DialogResult.Cancel == CustomMessageBox.Show("The port is open\n Continue?", "Continue", MessageBoxButtons.YesNo))
{
// return;
}
comPort.Close();
// allow things to cleanup
Thread.Sleep(400);
}
comPort.ReadBufferSize = 1024*1024*4;
comPort.PortName = MainV2.comPortName;
// test moving baud rate line
comPort.BaudRate = int.Parse(MainV2._connectionControl.CMB_baudrate.Text);
if (px4)
{
TXT_terminal.AppendText("Rebooting " + MainV2.comPortName + " at " + comPort.BaudRate + "\n");
// keep it local
using (var mine = new MAVLinkInterface())
{
mine.BaseStream.PortName = MainV2.comPortName;
mine.BaseStream.BaudRate = comPort.BaudRate;
mine.giveComport = true;
mine.BaseStream.Open();
// check if we are a mavlink stream
var buffer = mine.readPacket();
if (buffer.Length > 0)
{
log.Info("got packet - sending reboot via mavlink");
TXT_terminal.AppendText("Via Mavlink\n");
mine.doReboot(false, false);
try
{
mine.BaseStream.Close();
}
catch
{
}
}
else
{
log.Info("no packet - sending reboot via console");
TXT_terminal.AppendText("Via Console\n");
try
{
mine.BaseStream.Write("reboot\r");
mine.BaseStream.Write("exit\rreboot\r");
}
catch
{
}
try
{
mine.BaseStream.Close();
}
catch
{
}
}
}
TXT_terminal.AppendText("Waiting for reboot\n");
// wait 7 seconds for px4 reboot
log.Info("waiting for reboot");
var deadline = DateTime.Now.AddSeconds(9);
while (DateTime.Now < deadline)
{
Thread.Sleep(500);
Application.DoEvents();
}
var a = 0;
while (a < 5)
{
try
{
if (!comPort.IsOpen)
comPort.Open();
}
catch
{
}
Thread.Sleep(200);
a++;
}
}
else
{
log.Info("About to open " + comPort.PortName);
comPort.Open();
log.Info("toggle dtr");
comPort.toggleDTR();
}
try
{
comPort.DiscardInBuffer();
}
catch
{
}
startreadthread();
}
catch (Exception ex)
{
log.Error(ex);
TXT_terminal.AppendText("Cant open serial port\r\n");
return;
}
}
private void startreadthread()
{
Console.WriteLine("Terminal_Load run " + threadrun + " " + comPort.IsOpen);
BUT_disconnect.Enabled = true;
var t11 = new Thread(delegate()
{
threadrun = true;
Console.WriteLine("Terminal thread start run run " + threadrun + " " + comPort.IsOpen);
try
{
comPort.Write("\r");
}
catch
{
}
// 10 sec
waitandsleep(10000);
Console.WriteLine("Terminal thread 1 run " + threadrun + " " + comPort.IsOpen);
// 100 ms
readandsleep(100);
Console.WriteLine("Terminal thread 2 run " + threadrun + " " + comPort.IsOpen);
try
{
if (!inlogview && comPort.IsOpen)
comPort.Write("\n\n\n");
// 1 secs
if (!inlogview && comPort.IsOpen)
readandsleep(1000);
if (!inlogview && comPort.IsOpen)
comPort.Write("\r\r\r?\r");
}
catch (Exception ex)
{
Console.WriteLine("Terminal thread 3 " + ex);
ChangeConnectStatus(false);
threadrun = false;
return;
}
Console.WriteLine("Terminal thread 3 run " + threadrun + " " + comPort.IsOpen);
while (threadrun)
{
try
{
Thread.Sleep(10);
if (!threadrun)
break;
if (this.Disposing)
break;
if (inlogview)
continue;
if (!comPort.IsOpen)
{
Console.WriteLine("Comport Closed");
ChangeConnectStatus(false);
break;
}
if (comPort.BytesToRead > 0)
{
comPort_DataReceived(null, null);
}
if (comPort is MAVLinkSerialPort)
{
if (lastsend.AddMilliseconds(500) > DateTime.Now)
{
// 20 hz
((MAVLinkSerialPort) comPort).timeout = 50;
}
else
{
// 5 hz
((MAVLinkSerialPort) comPort).timeout = 200;
}
}
}
catch (Exception ex)
{
Console.WriteLine("Terminal thread 4 " + ex);
}
}
threadrun = false;
try
{
comPort.DtrEnable = false;
}
catch
{
}
try
{
Console.WriteLine("term thread close run " + threadrun + " " + comPort.IsOpen);
ChangeConnectStatus(false);
comPort.Close();
}
catch
{
}
Console.WriteLine("Comport thread close run " + threadrun);
});
t11.IsBackground = true;
t11.Name = "Terminal serial thread";
t11.Start();
// doesnt seem to work on mac
//comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
if (IsDisposed || Disposing)
return;
TXT_terminal.AppendText("Opened com port\r\n");
inputStartPos = TXT_terminal.SelectionStart;
TXT_terminal.Focus();
}
private void ChangeConnectStatus(bool connected)
{
if (IsDisposed || Disposing)
return;
Invoke((MethodInvoker) delegate
{
if (connected && BUT_disconnect.Enabled == false)
{
BUT_disconnect.Enabled = true;
}
else if (!connected && BUT_disconnect.Enabled)
{
BUT_disconnect.Enabled = false;
}
});
}
private void BUTsetupshow_Click(object sender, EventArgs e)
{
if (comPort.IsOpen)
{
try
{
var encoding = new ASCIIEncoding();
var data = encoding.GetBytes("exit\rsetup\rshow\r");
comPort.Write(data, 0, data.Length);
}
catch
{
}
}
TXT_terminal.Focus();
}
private void BUTradiosetup_Click(object sender, EventArgs e)
{
if (comPort.IsOpen)
{
try
{
var encoding = new ASCIIEncoding();
var data = encoding.GetBytes("exit\rsetup\r\nradio\r");
comPort.Write(data, 0, data.Length);
}
catch
{
}
}
TXT_terminal.Focus();
}
private void BUTtests_Click(object sender, EventArgs e)
{
if (comPort.IsOpen)
{
try
{
var encoding = new ASCIIEncoding();
var data = encoding.GetBytes("exit\rtest\r?\r\n");
comPort.Write(data, 0, data.Length);
}
catch
{
}
}
TXT_terminal.Focus();
}
private void Logs_Click(object sender, EventArgs e)
{
inlogview = true;
Thread.Sleep(300);
Form Log = new LogDownload();
ThemeManager.ApplyThemeTo(Log);
Log.ShowDialog();
inlogview = false;
}
private void BUT_logbrowse_Click(object sender, EventArgs e)
{
Form logbrowse = new LogBrowse();
ThemeManager.ApplyThemeTo(logbrowse);
logbrowse.Show();
}
private void BUT_RebootAPM_Click(object sender, EventArgs e)
{
if (comPort.IsOpen)
{
BUT_disconnect.Enabled = true;
return;
}
if (MainV2.comPort.BaseStream.IsOpen)
{
if (CMB_boardtype.Text.Contains("NSH"))
{
start_NSHTerminal();
return;
}
MainV2.comPort.BaseStream.Close();
}
if (CMB_boardtype.Text.Contains("APM"))
start_Terminal(false);
if (CMB_boardtype.Text.Contains("PX4"))
start_Terminal(true);
if (CMB_boardtype.Text.Contains("VRX"))
start_Terminal(true);
}
private void start_NSHTerminal()
{
try
{
if (MainV2.comPort != null && MainV2.comPort.BaseStream != null && MainV2.comPort.BaseStream.IsOpen)
{
comPort = new MAVLinkSerialPort(MainV2.comPort, MAVLink.SERIAL_CONTROL_DEV.SHELL);
comPort.BaudRate = 0;
// 20 hz
((MAVLinkSerialPort) comPort).timeout = 50;
comPort.Open();
startreadthread();
}
}
catch
{
}
}
private void BUT_disconnect_Click(object sender, EventArgs e)
{
try
{
try
{
comPort.Write("reboot\n");
}
catch
{
}
comPort.Close();
TXT_terminal.AppendText("Closed\n");
}
catch
{
}
}
}
}