/* Copyright 2010 GHI Electronics LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ using System; using System.Drawing; using System.IO.Ports; using System.Windows.Forms; namespace FezTerm { public partial class MainForm : Form { bool connected = false; string previousCmd; FezTermClient client; readonly byte delim = 255; public MainForm() { InitializeComponent(); this.toolStripStatusLabel1.Text = "Starting"; } internal Color OutputBackColor { get { return this.richTextBox1.BackColor; } set { this.richTextBox1.BackColor = value; } } internal Color OutputForeColor { get { return this.richTextBox1.ForeColor; } set { this.richTextBox1.ForeColor = value; } } internal Font OutputFont { get { return this.richTextBox1.Font; } set { this.richTextBox1.Font = value; } } private void Form1_Load(object sender, EventArgs e) { RefreshPortNames(); EnterDisconnectedState(); } private void btnRefreshPorts_Click(object sender, EventArgs e) { RefreshPortNames(); } private void RefreshPortNames() { try { string[] ports = SerialPort.GetPortNames(); this.comboBox1.Items.Clear(); this.comboBox1.Items.AddRange(ports); if (ports.Length >= 1) this.comboBox1.SelectedIndex = ports.Length - 1; } catch (Exception ex) { WriteLineTB(ex.Message); } } private void EnterConnectedState() { if (this.InvokeRequired) { this.BeginInvoke(new Action(()=> { EnterConnectedState(); })); return; } string com = this.comboBox1.SelectedItem as string; if (com == null) { System.Media.SystemSounds.Beep.Play(); return; } try { SerialPort sp = new SerialPort(com, 9600, Parity.None, 8, StopBits.One); sp.WriteTimeout = 2000; client = new FezTermClient(sp, delim); client.MessageReceived += new Action(client_MessageReceived); client.TermError += new Action(client_TermError); client.Start(); this.tbCmd.Focus(); this.tbCmd.SelectionStart = this.tbCmd.TextLength; this.comboBox1.Enabled = false; this.connected = true; this.btnRefreshPorts.Enabled = false; this.btnConnect.Text = "Disconnect"; this.richTextBox1.Clear(); WriteLineTB("Connected."); this.toolStripStatusLabel1.Text = "Connected"; } catch (Exception ex) { EnterDisconnectedState(); WriteLineTB(Environment.NewLine + ex.Message); } } void client_TermError(Exception obj) { WriteLineTB(obj.Message); } void client_MessageReceived(string reply) { if (reply == "bye") { try { EnterDisconnectedState(); } catch (Exception ex) { WriteLineTB(ex.Message); } } WriteLineTB(reply); // Output to display. } private void EnterDisconnectedState() { if (this.InvokeRequired) { this.BeginInvoke(new Action(()=> { EnterDisconnectedState(); })); return; } if (client != null) { try { if (client.IsOpen) client.Close(); } catch { } client = null; } this.btnConnect.Text = "Connect"; this.comboBox1.Enabled = true; this.connected = false; this.btnRefreshPorts.Enabled = true; WriteLineTB("Disconnected"); this.toolStripStatusLabel1.Text = "Disconnected"; } private void btnConnect_Click(object sender, EventArgs e) { if (this.connected) EnterDisconnectedState(); else EnterConnectedState(); } private void WriteLineTB(string line) { if (line == null || line.Length == 0) return; if (this.InvokeRequired) { this.BeginInvoke(new Action(() => { WriteLineTB(line); })); return; } this.richTextBox1.AppendText(line + Environment.NewLine); this.richTextBox1.SelectionStart = this.richTextBox1.Text.Length; this.richTextBox1.ScrollToCaret(); this.richTextBox1.Refresh(); } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { this.Close(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (this.client != null) this.client.Close(); } private void aboutToolStripMenuItem_Click(object sender, EventArgs e) { AboutBox ab = new AboutBox(); ab.ShowDialog(); } private void clearToolStripMenuItem_Click(object sender, EventArgs e) { this.richTextBox1.Clear(); } private void copyToClipboardToolStripMenuItem_Click(object sender, EventArgs e) { if (this.richTextBox1.Text.Length == 0) return; try { Clipboard.SetText(this.richTextBox1.Text); } catch (Exception ex) { WriteLineTB("Error: " +ex.Message); System.Media.SystemSounds.Beep.Play(); } } private void tbCmd_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) { e.Handled = true; RunCmdInternal(); } } public void RunCmd(string cmd) { this.tbCmd.Text = cmd; RunCmdInternal(); } private void RunCmdInternal() { string cmd = null; try { cmd = this.tbCmd.Text.Trim(); if (cmd.Length == 0) return; switch (cmd.ToLower()) { case "cls": case "clear": this.richTextBox1.Clear(); previousCmd = "cls"; return; } if (client != null && client.IsOpen) { // Send command line to host. try { // Echo command line to output. WriteLineTB(Environment.NewLine + "> " + cmd); client.WriteMessage(cmd); } catch (Exception ex) { EnterDisconnectedState(); WriteLineTB(ex.Message); } } } catch (Exception ex) { WriteLineTB(ex.Message); } finally { this.previousCmd = cmd; this.tbCmd.Clear(); } } private void tbCmd_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Up) this.tbCmd.Text = this.previousCmd; } private void buttonsToolStripMenuItem_Click(object sender, EventArgs e) { ButtonsForm bf = new ButtonsForm(this); bf.Show(); } private void optionsToolStripMenuItem_Click(object sender, EventArgs e) { OptionsForm of = new OptionsForm(this); of.ShowDialog(); } } }