void GetCommunicationStatus() { Quad_SerialPort.Write("c"); for (int i = 0; i < 100000; i++) { ; } try { Status = Quad_SerialPort.ReadLine(); Status.Trim(); Connection_ListBox.Items.Add("Data Received"); } catch (TimeoutException) { Connection_ListBox.Items.Add("Timeout Exception Occurred"); } //Connection_ListBox.Items.Add("Status:"); Connection_ListBox.Items.Add("\n\n"); Connection_ListBox.Items.Add(Status); Connection_ListBox.Items.Add("\n\n"); if (Status == "Granted") { progressBar1.Value = 40; Connection_ListBox.Items.Add("Access Granted"); Quad_SerialPort.Write("b"); try { DataRequest = Quad_SerialPort.ReadLine(); DataRequest.Trim(); } catch (TimeoutException) { Connection_ListBox.Items.Add("Timeout Exception Occurred"); } if (DataRequest == "Request Granted") { progressBar1.Value = 70; Connection_ListBox.Items.Add("Exchanging Data"); /* * * Check Code Here * */ } else { Connection_ListBox.Items.Add("Invalid Data"); //MessageBox.Show("An Error Occurred while Trying to Exchange Data with the Device", "Data Sharing Unavailable", MessageBoxButtons.OK, MessageBoxIcon.Error); //this.Close(); } } else { Connection_ListBox.Items.Add("Invalid Data"); //MessageBox.Show("An Error Occurred while Connecting to the Device", "Failed to Communicate", MessageBoxButtons.OK, MessageBoxIcon.Error); //this.Close(); } }
public Form2(string COMPort, int Baud_Rate) { InitializeComponent(); this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form2_FormClosed); Quad_SerialPort.PortName = COMPort; Quad_SerialPort.BaudRate = Baud_Rate; Quad_SerialPort.Open(); if (Quad_SerialPort.IsOpen) { progressBar1.Value = 20; Connection_ListBox.Items.Add("Connected to the Device"); Connection_ListBox.Items.Add("Trying to Communicate..."); GetCommunicationStatus(); } else { MessageBox.Show("An Error Occurred while Connecting to the Device", "Failed to Connect", MessageBoxButtons.OK, MessageBoxIcon.Error); this.Close(); } }
private void button1_Click(object sender, EventArgs e) { if (comboBox1.SelectedIndex > -1) { String Port_Name = comboBox1.Text; Quad_SerialPort.PortName = Port_Name; int BaudRate_Int = 0; if (BaudRate.SelectedIndex > -1) { int.TryParse(BaudRate.Text, out BaudRate_Int); Quad_SerialPort.BaudRate = BaudRate_Int; Quad_SerialPort.Open(); if (Quad_SerialPort.IsOpen) { MessageBox.Show("Connection Established!"); Quad_SerialPort.Close(); this.Hide(); Form2 newscrn = new Form2(Port_Name, BaudRate_Int); newscrn.Show(); } else { MessageBox.Show("Check the COM Port Status Again."); } } else { MessageBox.Show("Please Select the Baud Rate"); } } else { MessageBox.Show("Please Select a COM Port"); } }