using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.IO.Ports; using System.Windows.Forms; using System.Threading; //CodeProjectSerialComms program //23/04/2013 16:29 namespace IridiumTest { public partial class Form1 : Form { public static SerialPort gpsPort = new SerialPort(); public static SerialPort iridiumPort = new SerialPort(); internal delegate void SerialDataReceivedEventHandlerDelegate(object sender, SerialDataReceivedEventArgs e); delegate void SetTextCallback(string text); string InputData = String.Empty; string gpsString = String.Empty; public Form1() { InitializeComponent(); } private void btnGetSerialPorts_Click(object sender, EventArgs e) { getSerialPorts(); } private void getSerialPorts() { string[] ArrayPortNames = null; int index = -1; string gpsPortName = null; //Com Ports ArrayPortNames = SerialPort.GetPortNames(); do { index += 1; cboGPSPorts.Items.Add(ArrayPortNames[index]); } while (!((ArrayPortNames[index] == gpsPortName) || (index == ArrayPortNames.GetUpperBound(0)))); Array.Sort(ArrayPortNames); if (index == ArrayPortNames.GetUpperBound(0)) { gpsPortName = ArrayPortNames[0]; } //get first item print in text if (cboGPSPorts.Items.Count > 3) cboGPSPorts.Text = ArrayPortNames[3]; else cboGPSPorts.Text = ArrayPortNames[0]; cboIridiumPorts.Text = ArrayPortNames[0]; //Baud Rate cboGPSBaudRate.Items.Add(300); cboGPSBaudRate.Items.Add(600); cboGPSBaudRate.Items.Add(1200); cboGPSBaudRate.Items.Add(2400); cboGPSBaudRate.Items.Add(9600); cboGPSBaudRate.Items.Add(14400); cboGPSBaudRate.Items.Add(19200); cboGPSBaudRate.Items.Add(38400); cboGPSBaudRate.Items.Add(57600); cboGPSBaudRate.Items.Add(115200); cboGPSBaudRate.Items.ToString(); //get first item print in text cboGPSBaudRate.Text = cboGPSBaudRate.Items[4].ToString(); cboIridiumBaudRate.Items.Add(300); cboIridiumBaudRate.Items.Add(600); cboIridiumBaudRate.Items.Add(1200); cboIridiumBaudRate.Items.Add(2400); cboIridiumBaudRate.Items.Add(9600); cboIridiumBaudRate.Items.Add(14400); cboIridiumBaudRate.Items.Add(19200); cboIridiumBaudRate.Items.Add(38400); cboIridiumBaudRate.Items.Add(57600); cboIridiumBaudRate.Items.Add(115200); cboIridiumBaudRate.Items.ToString(); //get first item print in text cboIridiumBaudRate.Text = cboIridiumBaudRate.Items[6].ToString(); //Data Bits cboGPSDataBits.Items.Add(7); cboGPSDataBits.Items.Add(8); //get the first item print it in the text cboGPSDataBits.Text = cboGPSDataBits.Items[1].ToString(); cboIridiumDataBits.Items.Add(7); cboIridiumDataBits.Items.Add(8); //get the first item print it in the text cboIridiumDataBits.Text = cboIridiumDataBits.Items[1].ToString(); //Stop Bits cboGPSStopBits.Items.Add("One"); cboGPSStopBits.Items.Add("OnePointFive"); cboGPSStopBits.Items.Add("Two"); //get the first item print in the text cboGPSStopBits.Text = cboGPSStopBits.Items[0].ToString(); cboIridiumStopBits.Items.Add("One"); cboIridiumStopBits.Items.Add("OnePointFive"); cboIridiumStopBits.Items.Add("Two"); //get the first item print in the text cboIridiumStopBits.Text = cboIridiumStopBits.Items[0].ToString(); //Parity cboGPSParity.Items.Add("None"); cboGPSParity.Items.Add("Even"); cboGPSParity.Items.Add("Mark"); cboGPSParity.Items.Add("Odd"); cboGPSParity.Items.Add("Space"); //get the first item print in the text cboGPSParity.Text = cboGPSParity.Items[0].ToString(); cboIridiumParity.Items.Add("None"); cboIridiumParity.Items.Add("Even"); cboIridiumParity.Items.Add("Mark"); cboIridiumParity.Items.Add("Odd"); cboIridiumParity.Items.Add("Space"); //get the first item print in the text cboIridiumParity.Text = cboIridiumParity.Items[0].ToString(); //Handshake cboGPSHandShaking.Items.Add("None"); cboGPSHandShaking.Items.Add("XOnXOff"); cboGPSHandShaking.Items.Add("RequestToSend"); cboGPSHandShaking.Items.Add("RequestToSendXOnXOff"); //get the first item print it in the text cboGPSHandShaking.Text = cboGPSHandShaking.Items[0].ToString(); cboGPSHandShaking.Items.Add("None"); cboIridiumHandShaking.Items.Add("None"); cboIridiumHandShaking.Items.Add("XOnXOff"); cboIridiumHandShaking.Items.Add("RequestToSend"); cboIridiumHandShaking.Items.Add("RequestToSendXOnXOff"); //get the first item print it in the text cboIridiumHandShaking.Text = cboIridiumHandShaking.Items[0].ToString(); } private void gpsPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { InputData = gpsPort.ReadExisting(); if (InputData != String.Empty) { this.BeginInvoke(new SetTextCallback(GPSSetText), new object[] { InputData }); } } private void GPSSetText(string text) { if (text.Contains("$GPGGA") && text.Length < 140) { this.rtbGPSIncoming.AppendText(text); gpsString = text; Console.Write(gpsString); } } private void iridiumPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { InputData = iridiumPort.ReadExisting(); if (InputData != String.Empty) { this.BeginInvoke(new SetTextCallback(IridiumSetText), new object[] { InputData }); } } private void IridiumSetText(string text) { this.rtbIridiumIncoming.AppendText(text); } private void btnPortState_Click(object sender, EventArgs e) { openCloseGPSPort(); } private void openCloseGPSPort() { if (btnGPSPortState.Text == "GPS is Closed") { btnGPSPortState.Text = "GPS is Open"; gpsPort.PortName = Convert.ToString(cboGPSPorts.Text); gpsPort.BaudRate = Convert.ToInt32(cboGPSBaudRate.Text); gpsPort.DataBits = Convert.ToInt16(cboGPSDataBits.Text); gpsPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cboGPSStopBits.Text); gpsPort.Handshake = (Handshake)Enum.Parse(typeof(Handshake), cboGPSHandShaking.Text); gpsPort.Parity = (Parity)Enum.Parse(typeof(Parity), cboGPSParity.Text); gpsPort.Open(); } else if (btnGPSPortState.Text == "GPS is Open") { btnGPSPortState.Text = "GPS is Closed"; gpsPort.Close(); } } private void openCloseIridiumPort() { if (btnIridiumPortState.Text == "Iridium is Closed") { btnIridiumPortState.Text = "Iridium is Open"; iridiumPort.PortName = Convert.ToString(cboIridiumPorts.Text); iridiumPort.BaudRate = Convert.ToInt32(cboIridiumBaudRate.Text); iridiumPort.DataBits = Convert.ToInt16(cboIridiumDataBits.Text); iridiumPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cboIridiumStopBits.Text); iridiumPort.Handshake = (Handshake)Enum.Parse(typeof(Handshake), cboIridiumHandShaking.Text); iridiumPort.Parity = (Parity)Enum.Parse(typeof(Parity), cboIridiumParity.Text); iridiumPort.Open(); } else if (btnIridiumPortState.Text == "Iridium is Open") { btnIridiumPortState.Text = "Iridium is Closed"; iridiumPort.Close(); } } private void rtbOutgoing_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)13) // enter key { gpsPort.Write("\r\n"); rtbGPSOutgoing.Text = ""; } else if (e.KeyChar < 32 || e.KeyChar > 126) { e.Handled = true; // ignores anything else outside printable ASCII range } else { gpsPort.Write(e.KeyChar.ToString()); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (gpsPort.IsOpen) gpsPort.Close(); if (iridiumPort.IsOpen) iridiumPort.Close(); timerIridium.Stop(); } private void btnExit_Click(object sender, EventArgs e) { gpsPort.Close(); iridiumPort.Close(); timerIridium.Stop(); Application.Exit(); } private void Form1_Load(object sender, EventArgs e) { getSerialPorts(); rtbGPSIncoming.Clear(); gpsPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(gpsPort_DataReceived); openCloseGPSPort(); rtbIridiumIncoming.Clear(); iridiumPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(iridiumPort_DataReceived); openCloseIridiumPort(); Thread.Sleep(1000); rtbIridiumOutgoing.AppendText("Sending D0\r\n"); IModem.sendD0(); Thread.Sleep(1000); rtbIridiumOutgoing.AppendText("Sending K0\r\n"); IModem.sendK0(); Thread.Sleep(1000); rtbIridiumOutgoing.AppendText("Sending SBDD2\r\n"); IModem.sendSBDD2(); Thread.Sleep(1000); timerIridium.Interval = 2000; timerIridium.Start(); } public enum Action : int { //list sequential actions here sendSBDD2 = 1, readGpsI2C, sendCSQ, loadSBDWT, sendSBDI, uploadEngrLog, sendFLBBRunCmd, lastAction, size = lastAction } private static Action currentAction; private static DateTime surfaceOpsSubstateStartTime; private static TimeSpan surfaceOpsSubstate1Timeout = new TimeSpan(0, 0, 2); private static TimeSpan surfaceOpsSubstate2Timeout = new TimeSpan(0, 0, 5); private static TimeSpan surfaceOpsSubstate3Timeout = new TimeSpan(0, 0, 20); private static TimeSpan surfaceOpsSubstate4Timeout = new TimeSpan(0, 0, 10); private void timerIridium_Tick(object sender, EventArgs e) { timerIridium.Interval = 300000; bool endOfSequence = false; currentAction = Action.sendSBDD2; while(!endOfSequence) { switch (currentAction) { #region SurOps Action Sequence case (Action.sendSBDD2): rtbIridiumOutgoing.AppendText("Sending SBDD2\r\n"); IModem.sendSBDD2(); //TODO remove once waitForResponse has been implemented surfaceOpsSubstateStartTime = DateTime.Now; currentAction = Action.readGpsI2C; rtbIridiumOutgoing.AppendText("Reading GPS\r\n"); break; case (Action.readGpsI2C): if ((DateTime.Now - surfaceOpsSubstateStartTime) > surfaceOpsSubstate1Timeout) { //ActionReturn = readGPSI2C(); //IModem.sendI2CCmd(); surfaceOpsSubstateStartTime = DateTime.Now; currentAction = Action.sendCSQ; rtbIridiumOutgoing.AppendText("Sending CSQ\r\n"); } break; case (Action.sendCSQ): if ((DateTime.Now - surfaceOpsSubstateStartTime) > surfaceOpsSubstate2Timeout) { IModem.sendCSQ(); surfaceOpsSubstateStartTime = DateTime.Now; currentAction = Action.loadSBDWT; rtbIridiumOutgoing.AppendText("Loading SBDWT\r\n"); } break; case (Action.loadSBDWT): if ((DateTime.Now - surfaceOpsSubstateStartTime) > surfaceOpsSubstate3Timeout) { IModem.loadSBDWT(gpsString); surfaceOpsSubstateStartTime = DateTime.Now; currentAction = Action.sendSBDI; rtbIridiumOutgoing.AppendText("Sending SBDI\r\n"); } break; case (Action.sendSBDI): if ((DateTime.Now - surfaceOpsSubstateStartTime) > surfaceOpsSubstate4Timeout) { IModem.sendSBDI(); endOfSequence = true; } break; default: break; #endregion } Thread.Sleep(1000); } } private void rtbIridiumIncoming_TextChanged(object sender, EventArgs e) { } } }