//___________________________________________________________________________ private void BConnect_Click(object sender, EventArgs e) { if (CBPort.Text.Length == 0) { MessageBox.Show("Не задан порт"); CBPort.Focus(); } else { if (ComPort == null) { ComPort = new COMPort(MessShow, DHandlingRecieve); } if (ComPort.PortIsOpen()) { ComPort.ClosePort(); TBOut.AppendText(string.Format("Порт [{0}] закрыт\n", ComPort.PortName())); BConnect.Text = "Открыть"; } else { if (ComPort.OpenPort(CBPort.Text, CBBaud.Text) != null) { BConnect.Text = CBPort.Text; TBOut.AppendText(string.Format("Порт [{0}] открыт\n", ComPort.PortName())); } } } }
private void BtnConnect_Click(object sender, EventArgs e) { if (BtnConnect.Text == "Connect") { try { serialPort1.PortName = CBPort.Text; serialPort1.Open(); BtnConnect.Text = "Disconnect"; BtnConnect.ForeColor = Color.Red; CBPort.Enabled = false; PortsRefresh.Enabled = false; TxtLog.Text = ""; TxtSendToPort.Text = ""; this.Height = FormHeightWithGBControl; RePosition(); BGW_CheckConnection.RunWorkerAsync(); LedSet(ledState.Ready); } catch (Exception ex) { MessageBox.Show(ex.Message); } } else { try { serialPort1.Close(); BtnConnect.Text = "Connect"; BtnConnect.ForeColor = Color.Green; CBPort.Enabled = true; PortsRefresh.Enabled = true; this.Height = FormHeightWithoutGBControl; RePosition(); CBPort.Focus(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }