private void Show_CurrentSettings() { Ngo_Final mainForm; mainForm = new Ngo_Final(); foreach (Ngo_Final tmpForm in Application.OpenForms) { if (tmpForm.Name == "Ngo_Final") { mainForm = tmpForm; break; } } combo_Port.SelectedItem = mainForm.serialPort1.PortName.ToString(); combo_bps.SelectedItem = mainForm.serialPort1.BaudRate.ToString(); combo_DataBits.SelectedItem = mainForm.serialPort1.DataBits.ToString(); combo_Parity.SelectedItem = mainForm.serialPort1.Parity.ToString(); combo_StopBits.SelectedItem = mainForm.serialPort1.StopBits.ToString(); combo_FlowCtrl.SelectedItem = mainForm.serialPort1.Handshake.ToString(); }
private void Apply_Click(object sender, EventArgs e) { //Initialize the RS232 port with the selection made by user Ngo_Final mainForm; mainForm = new Ngo_Final(); foreach (Ngo_Final tmpForm in Application.OpenForms) { if (tmpForm.Name == "Ngo_Final") { mainForm = tmpForm; break; } } mainForm.serialPort1.Close(); //close the serial port before changing settings mainForm.serialPort1.PortName = combo_Port.SelectedItem.ToString(); mainForm.serialPort1.BaudRate = int.Parse(combo_bps.SelectedItem.ToString()); mainForm.serialPort1.DataBits = int.Parse(combo_DataBits.SelectedItem.ToString()); mainForm.serialPort1.Parity = (System.IO.Ports.Parity)Enum.Parse(typeof(System.IO.Ports.Parity), combo_Parity.SelectedItem.ToString()); mainForm.serialPort1.StopBits = (System.IO.Ports.StopBits)Enum.Parse(typeof(System.IO.Ports.StopBits), combo_StopBits.SelectedItem.ToString()); mainForm.serialPort1.Handshake = (System.IO.Ports.Handshake)Enum.Parse(typeof(System.IO.Ports.Handshake), combo_FlowCtrl.SelectedItem.ToString()); try { mainForm.serialPort1.Open(); //open the serial port with the new settings this.Close(); //close the portsettings form } catch (UnauthorizedAccessException) { MessageBox.Show("Cannot open " + combo_Port.SelectedItem.ToString() + " port ! \nPlease select another port"); } catch { MessageBox.Show("Cannot open serial port"); } }