Exemplo n.º 1
0
        /// <summary>
        /// Initialized the GUI
        /// Attempts to open COM 9 by default
        /// </summary>
        /// <param name="frmChoose">The parent chooser form</param>
        public frmTeslaGui(frmChoose frmChoose)
        {
            InitializeComponent();
            this.chooserForm = frmChoose;
            updateGUI = new UpdateGUI(this.UpdateGUIFunction);

            //add the serial port selection to the menu
            string[] serialPorts = System.IO.Ports.SerialPort.GetPortNames();
            foreach (string portName in serialPorts)
            {
                COMPortToolStripMenuItem.DropDownItems.Add(portName, null, (object sender, EventArgs e) =>
                {
                    serialPort.PortName = portName;
                    UncheckAllComs();
                    ((ToolStripMenuItem)sender).Checked = true;
                });
                if (portName == "COM9")
                {
                    ((ToolStripMenuItem)COMPortToolStripMenuItem.DropDownItems[COMPortToolStripMenuItem.DropDownItems.Count - 1]).Checked = true;
                }
            }

            //set up serial port
            serialPort = new System.IO.Ports.SerialPort();
            if (serialPorts.Contains("COM9"))
            {
                serialPort.PortName = "COM9";
            }
            serialPort.BaudRate = 57600;
            serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort_DataReceived);

            //Initialize classes
            antComm = new ANTCommunication(ref serialPort, this);
            spBuffer = new BufferedReader(this);
            tempConv = new TempVal();

            //Initialize timers
            flashTimer = new System.Timers.Timer(50);
            flashTimer.Elapsed += new System.Timers.ElapsedEventHandler(flashTimer_Elapsed);
            simTimer = new System.Timers.Timer(500);
            simTimer.Elapsed += new System.Timers.ElapsedEventHandler(simTimer_Elapsed);

            pastTemps = new Queue<double>(150);

            //Clear labels
            lblError.Text = "";
            lblLastMessage.Text = "";

            //Set up last received
            lastRecieved = new Dictionary<DataDecoder.SensorType, int>(3);
            lastRecieved.Add(DataDecoder.SensorType.Temperature, 100);
            lastRecieved.Add(DataDecoder.SensorType.Accelerometer, 100);
            lastRecieved.Add(DataDecoder.SensorType.Button, 100);
        }
Exemplo n.º 2
0
 /// <summary>
 /// EVENT: Called when the "Open COM" button is clicked
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void btnOpenCom_Click(object sender, EventArgs e)
 {
     if (!serialPort.IsOpen)
     {
         try
         {
             serialPort.Open();
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }
         if (serialPort.IsOpen)
         {
             lblComStatus.Text = "Open";
             btnOpenCom.Text = "Close COM";
             antComm = new ANTCommunication(ref serialPort, this);
         }
     }
     else
     {
         try
         {
             serialPort.Close();
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }
         if (!serialPort.IsOpen)
         {
             lblComStatus.Text = "Closed";
             btnOpenCom.Text = "Open COM";
             antComm = null;
         }
     }
 }