2011-09-08 22:31:32 -03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using ArdupilotMega;
|
|
|
|
|
using System.IO.Ports;
|
2012-04-24 10:49:27 -03:00
|
|
|
|
using ArdupilotMega.Comms;
|
2012-04-25 07:10:11 -03:00
|
|
|
|
using ArdupilotMega.Utilities;
|
2012-05-23 20:52:07 -03:00
|
|
|
|
using System.Text.RegularExpressions;
|
2011-09-08 22:31:32 -03:00
|
|
|
|
|
|
|
|
|
namespace ArdupilotMega.GCSViews
|
|
|
|
|
{
|
|
|
|
|
public partial class Terminal : MyUserControl
|
|
|
|
|
{
|
|
|
|
|
ICommsSerial comPort = MainV2.comPort.BaseStream;
|
|
|
|
|
Object thisLock = new Object();
|
|
|
|
|
public static bool threadrun = false;
|
|
|
|
|
bool inlogview = false;
|
|
|
|
|
List<string> cmdHistory = new List<string>();
|
|
|
|
|
int history = 0;
|
|
|
|
|
int inputStartPos = 0;
|
|
|
|
|
|
|
|
|
|
public Terminal()
|
|
|
|
|
{
|
2012-07-16 10:37:35 -03:00
|
|
|
|
threadrun = false;
|
2011-09-08 22:31:32 -03:00
|
|
|
|
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (!comPort.IsOpen)
|
|
|
|
|
return;
|
2012-07-12 11:06:22 -03:00
|
|
|
|
|
|
|
|
|
// if btr > 0 then this shouldnt happen
|
|
|
|
|
comPort.ReadTimeout = 300;
|
|
|
|
|
|
2011-09-08 22:31:32 -03:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
lock (thisLock)
|
|
|
|
|
{
|
2012-05-23 20:52:07 -03:00
|
|
|
|
System.Threading.Thread.Sleep(20);
|
|
|
|
|
byte[] buffer = new byte[256];
|
|
|
|
|
int a = 0;
|
2012-07-12 11:06:22 -03:00
|
|
|
|
|
2012-05-23 20:52:07 -03:00
|
|
|
|
while (comPort.BytesToRead > 0)
|
|
|
|
|
{
|
|
|
|
|
byte indata = (byte)comPort.ReadByte();
|
|
|
|
|
|
|
|
|
|
buffer[a] = indata;
|
|
|
|
|
|
|
|
|
|
if (buffer[a] >= 0x20 && buffer[a] < 0x7f || buffer[a] == (int)'\n' || buffer[a] == (int)'\r')
|
|
|
|
|
{
|
|
|
|
|
a++;
|
|
|
|
|
}
|
2012-06-04 06:56:46 -03:00
|
|
|
|
if (a == (buffer.Length-1))
|
|
|
|
|
break;
|
2012-05-23 20:52:07 -03:00
|
|
|
|
}
|
2011-12-17 05:22:40 -04:00
|
|
|
|
|
2012-05-23 20:52:07 -03:00
|
|
|
|
addText(ASCIIEncoding.ASCII.GetString(buffer,0,a+1));
|
2011-09-08 22:31:32 -03:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-12 11:06:22 -03:00
|
|
|
|
catch (Exception ex) { Console.WriteLine(ex.ToString()); if (!threadrun) return; TXT_terminal.AppendText("Error reading com port\r\n"); }
|
2011-09-08 22:31:32 -03:00
|
|
|
|
}
|
|
|
|
|
|
2011-12-17 05:22:40 -04:00
|
|
|
|
void addText(string data)
|
|
|
|
|
{
|
2012-07-12 11:06:22 -03:00
|
|
|
|
this.BeginInvoke((System.Windows.Forms.MethodInvoker)delegate()
|
2011-12-17 05:22:40 -04:00
|
|
|
|
{
|
|
|
|
|
TXT_terminal.SelectionStart = TXT_terminal.Text.Length;
|
|
|
|
|
|
2012-05-23 20:52:07 -03:00
|
|
|
|
data = data.Replace("U3","");
|
|
|
|
|
data = data.Replace("U$", "");
|
|
|
|
|
data = data.Replace(@"U""","");
|
|
|
|
|
data = data.Replace("d'`F", "");
|
|
|
|
|
data = data.Replace("U.", "");
|
|
|
|
|
data = data.Replace("'`","");
|
|
|
|
|
|
2011-12-17 05:22:40 -04:00
|
|
|
|
data = data.TrimEnd('\r'); // else added \n all by itself
|
2012-07-05 20:27:42 -03:00
|
|
|
|
data = data.Replace("\0", "");
|
2011-12-17 05:22:40 -04:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
inputStartPos = TXT_terminal.SelectionStart;
|
|
|
|
|
});
|
2012-07-12 11:06:22 -03:00
|
|
|
|
|
2011-12-17 05:22:40 -04:00
|
|
|
|
}
|
|
|
|
|
|
2011-09-08 22:31:32 -03:00
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
/* 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;
|
|
|
|
|
|
2012-07-22 04:51:05 -03:00
|
|
|
|
try
|
2011-09-08 22:31:32 -03:00
|
|
|
|
{
|
2012-07-22 04:51:05 -03:00
|
|
|
|
if (comPort.IsOpen)
|
|
|
|
|
{
|
|
|
|
|
comPort.Close();
|
|
|
|
|
}
|
2011-09-08 22:31:32 -03:00
|
|
|
|
}
|
2012-07-22 04:51:05 -03:00
|
|
|
|
catch { } // Exception System.IO.IOException: The specified port does not exist.
|
|
|
|
|
|
2011-09-08 22:31:32 -03:00
|
|
|
|
System.Threading.Thread.Sleep(400);
|
2012-07-16 10:37:35 -03:00
|
|
|
|
|
|
|
|
|
MainV2.giveComport = false;
|
2011-09-08 22:31:32 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TXT_terminal_KeyPress(object sender, KeyPressEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.KeyChar == '\r')
|
|
|
|
|
{
|
|
|
|
|
if (comPort.IsOpen)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string cmd = "";
|
|
|
|
|
lock (thisLock)
|
|
|
|
|
{
|
2012-05-16 09:21:27 -03:00
|
|
|
|
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);
|
|
|
|
|
}
|
2011-09-08 22:31:32 -03:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// do not change this \r is correct - no \n
|
2012-02-23 21:38:56 -04:00
|
|
|
|
if (cmd == "+++")
|
2012-07-12 11:06:22 -03:00
|
|
|
|
{
|
2012-02-23 21:38:56 -04:00
|
|
|
|
comPort.Write(Encoding.ASCII.GetBytes(cmd), 0, cmd.Length);
|
2012-07-12 11:06:22 -03:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
comPort.Write(Encoding.ASCII.GetBytes(cmd + "\r"), 0, cmd.Length + 1);
|
2012-02-23 21:38:56 -04:00
|
|
|
|
}
|
2011-09-08 22:31:32 -03:00
|
|
|
|
}
|
2012-03-09 11:18:12 -04:00
|
|
|
|
catch { CustomMessageBox.Show("Error writing to com port"); }
|
2011-09-08 22:31:32 -03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
if (comPort.IsOpen)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
comPort.Write(new byte[] { (byte)e.KeyChar }, 0, 1);
|
|
|
|
|
}
|
|
|
|
|
catch { MessageBox.Show("Error writing to com port"); }
|
|
|
|
|
}
|
|
|
|
|
e.Handled = true;*/
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-03 19:46:56 -03:00
|
|
|
|
private void waitandsleep(int time)
|
|
|
|
|
{
|
|
|
|
|
DateTime start = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
while ((DateTime.Now - start).TotalMilliseconds < time)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (comPort.BytesToRead > 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch { threadrun = false; return; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-12 11:06:22 -03:00
|
|
|
|
private void readandsleep(int time)
|
|
|
|
|
{
|
|
|
|
|
DateTime start = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
while ((DateTime.Now - start).TotalMilliseconds < time)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (comPort.BytesToRead > 0)
|
|
|
|
|
{
|
|
|
|
|
comPort_DataReceived((object)null, (SerialDataReceivedEventArgs)null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch { threadrun = false; return; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-08 22:31:32 -03:00
|
|
|
|
private void Terminal_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2012-03-18 20:26:20 -03:00
|
|
|
|
MainV2.giveComport = true;
|
2011-09-08 22:31:32 -03:00
|
|
|
|
|
|
|
|
|
if (comPort.IsOpen)
|
|
|
|
|
comPort.Close();
|
|
|
|
|
|
|
|
|
|
comPort.ReadBufferSize = 1024 * 1024;
|
|
|
|
|
|
2012-03-18 20:26:20 -03:00
|
|
|
|
comPort.PortName = MainV2.comPortName;
|
2011-10-17 19:49:49 -03:00
|
|
|
|
|
2011-09-08 22:31:32 -03:00
|
|
|
|
comPort.Open();
|
|
|
|
|
|
2011-12-29 06:31:42 -04:00
|
|
|
|
comPort.toggleDTR();
|
|
|
|
|
|
2012-09-03 19:46:56 -03:00
|
|
|
|
comPort.DiscardInBuffer();
|
|
|
|
|
|
2011-09-08 22:31:32 -03:00
|
|
|
|
System.Threading.Thread t11 = new System.Threading.Thread(delegate()
|
|
|
|
|
{
|
|
|
|
|
threadrun = true;
|
2011-12-16 08:04:20 -04:00
|
|
|
|
|
2012-09-03 19:46:56 -03:00
|
|
|
|
// 10 sec
|
|
|
|
|
waitandsleep(10000);
|
|
|
|
|
|
|
|
|
|
// 100 ms
|
|
|
|
|
readandsleep(100);
|
2012-03-29 19:17:06 -03:00
|
|
|
|
try
|
|
|
|
|
{
|
2011-12-17 05:22:40 -04:00
|
|
|
|
comPort.Write("\n\n\n");
|
2012-05-23 20:52:07 -03:00
|
|
|
|
|
2012-07-12 11:06:22 -03:00
|
|
|
|
// 1 secs
|
|
|
|
|
readandsleep(1000);
|
2012-05-23 20:52:07 -03:00
|
|
|
|
|
|
|
|
|
comPort.Write("\r\r\r?\r");
|
2012-03-29 19:17:06 -03:00
|
|
|
|
}
|
2012-07-12 11:06:22 -03:00
|
|
|
|
catch { threadrun = false; return; }
|
2012-05-23 20:52:07 -03:00
|
|
|
|
|
2011-09-08 22:31:32 -03:00
|
|
|
|
while (threadrun)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
System.Threading.Thread.Sleep(10);
|
|
|
|
|
if (inlogview)
|
|
|
|
|
continue;
|
|
|
|
|
if (!comPort.IsOpen)
|
|
|
|
|
break;
|
|
|
|
|
if (comPort.BytesToRead > 0)
|
|
|
|
|
{
|
|
|
|
|
comPort_DataReceived((object)null, (SerialDataReceivedEventArgs)null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
|
|
|
|
}
|
2011-12-17 05:22:40 -04:00
|
|
|
|
|
2012-07-05 20:27:42 -03:00
|
|
|
|
threadrun = false;
|
|
|
|
|
|
2011-12-17 05:22:40 -04:00
|
|
|
|
comPort.DtrEnable = false;
|
|
|
|
|
|
2012-07-05 20:27:42 -03:00
|
|
|
|
comPort.Close();
|
|
|
|
|
|
2011-09-08 22:31:32 -03:00
|
|
|
|
Console.WriteLine("Comport thread close");
|
|
|
|
|
});
|
|
|
|
|
t11.IsBackground = true;
|
|
|
|
|
t11.Name = "Terminal serial thread";
|
|
|
|
|
t11.Start();
|
|
|
|
|
|
|
|
|
|
// doesnt seem to work on mac
|
|
|
|
|
//comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
|
|
|
|
|
|
|
|
|
|
TXT_terminal.AppendText("Opened com port\r\n");
|
|
|
|
|
inputStartPos = TXT_terminal.SelectionStart;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception) { TXT_terminal.AppendText("Cant open serial port\r\n"); return; }
|
2011-12-22 10:33:05 -04:00
|
|
|
|
|
|
|
|
|
TXT_terminal.Focus();
|
2011-09-08 22:31:32 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void BUTsetupshow_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (comPort.IsOpen)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
|
|
|
|
|
byte[] 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
|
|
|
|
|
{
|
|
|
|
|
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
|
|
|
|
|
byte[] 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
|
|
|
|
|
{
|
|
|
|
|
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
|
|
|
|
|
byte[] 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)
|
|
|
|
|
{
|
|
|
|
|
Form Log = new Log();
|
2012-03-03 20:42:42 -04:00
|
|
|
|
ThemeManager.ApplyThemeTo(Log);
|
2011-09-08 22:31:32 -03:00
|
|
|
|
inlogview = true;
|
|
|
|
|
Log.ShowDialog();
|
|
|
|
|
inlogview = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void BUT_logbrowse_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Form logbrowse = new LogBrowse();
|
2012-03-03 20:42:42 -04:00
|
|
|
|
ThemeManager.ApplyThemeTo(logbrowse);
|
2011-09-08 22:31:32 -03:00
|
|
|
|
logbrowse.ShowDialog();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|