private void BtnConnect_Click(object sender, EventArgs e) { if (PortList.SelectedIndex > -1) { VMPort.WriteTimeout = Form1.FactoryDefault.PortTimeout; VMPort.ReadTimeout = Form1.FactoryDefault.PortTimeout; if (VMPort.IsOpen) { VMPort.Close(); } VMPort.PortName = PortList.SelectedItem.ToString(); // try to open the selected port: try { VMPort.Open(); VMisConnected = true; VMStatus.Text = "Virtual Machine is Connected to " + VMPort.PortName; VMPort.DiscardOutBuffer(); //Clear Buffer VMPort.DiscardInBuffer(); //Clear Buffer } catch { MessageBox.Show("Serial port " + VMPort.PortName + " cannot be opened!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); // cmbComSelect.SelectedText = ""; VMStatus.Text = "Select serial port!"; } } else { MessageBox.Show("Please select a valid port from list to connect Virtual Machine ...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
private void BtnDisconnect_Click(object sender, EventArgs e) { if (VMPort.IsOpen) { VMPort.Close(); } VMisConnected = false; VMStatus.Text = "Virtual Machine is Disconnected."; }