private void connect() { string selectedPort = comboBox1.GetItemText(comboBox1.SelectedItem); Connection_window.Text = ""; Connection_window.AppendText("Selected COM Port = " + selectedPort + Environment.NewLine); Connection_window.AppendText("Trying to open " + selectedPort + Environment.NewLine); try { isConnected = true; port.PortName = selectedPort; port.Open(); string start = "#STAR\n"; port.Write(start); port.ReadExisting(); logTraffic(TX_traffic_window, start, Color.Black); connectBtn.Text = "Disconnect"; Connection_window.AppendText("--------------------------------------" + Environment.NewLine); } catch { Connection_window.AppendText("Cannot open " + selectedPort + Environment.NewLine); return; } Connection_window.AppendText(selectedPort + " now open" + Environment.NewLine); Connection_window.AppendText("--------------------------------------" + Environment.NewLine); }
private void disconnect() { try { isConnected = false; port.Write("#STOP\n"); port.Close(); connectBtn.Text = "Connect"; } catch { return; } Connection_window.AppendText(port.PortName + " is now closed" + Environment.NewLine); Connection_window.AppendText("--------------------------------------" + Environment.NewLine); }