private void Button1_Click(object sender, EventArgs e) { SerialSettingsForm sf = null; if (ComPort == null) { sf = new SerialSettingsForm(); } else { sf = new SerialSettingsForm(ComPort); } DialogResult res = sf.ShowDialog(); if (res == DialogResult.OK) { if (ComPort != null) { ComPort.Close(); } ComPort = new SerialPort(sf.SerialSelect.Text); ComPort.DataReceived += ComPort_DataReceived; ComPort.BaudRate = 9600; ComPort.Parity = Parity.None; ComPort.DataBits = 8; ComPort.StopBits = StopBits.One; ComPort.Open(); ComPort.DiscardInBuffer(); } }
private void Button1_Click(object sender, EventArgs e) { SerialSettingsForm sf = null; if (ComPort == null) { sf = new SerialSettingsForm(); } else { sf = new SerialSettingsForm(ComPort); } DialogResult res = sf.ShowDialog(); if (res == DialogResult.OK) { try { if (ComPort != null) { ComPort.Close(); } ComPort = new SerialPort(sf.SerialSelect.Text); ComPort.DataReceived += ComPort_DataReceived; ComPort.BaudRate = 9600; ComPort.Parity = Parity.None; ComPort.DataBits = 8; ComPort.StopBits = StopBits.One; ComPort.Open(); ComPort.DiscardInBuffer(); toolStripStatusLabel2.ForeColor = Color.Green; toolStripStatusLabel2.Text = sf.SerialSelect.Text + " Ready"; } catch { toolStripStatusLabel2.ForeColor = Color.Red; toolStripStatusLabel2.Text = "COM not ready"; MessageBox.Show("Error connecting " + sf.SerialSelect.Text); } EnableSendButton(); } }