private bool PortCtrl(bool ctrl) { if (true == ctrl) { if (scom.IsOpen == false) { try { scom.Config(combPortNum.Text, Convert.ToInt32(combPortBaud.Text), combPortChk.Text); scom.Open(); } catch (Exception ex) { MessageBox.Show("打开通信端口失败" + "," + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } } } else { try { scom.Close(); } catch (System.Exception ex) { MessageBox.Show("关闭通信端口失败" + "," + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } } return(true); }
private void btnCashChargOpenPort_Click(object sender, RoutedEventArgs e) { var msg = ""; com = new SerialCom(cmbCashChargePorts.Text); if (com.Open(out msg) == false) { Core.Log.In(msg); return; } StatusCode.Init(); wrapPanel.IsEnabled = true; wp1.IsEnabled = true; constrant = new Constrant(com); }
private void ToggleSerialPort() { try { if (SerialCom.IsPortOpen) { SerialCom.Close(); } else { SerialCom.Open(); } ToggleButtonText = SerialCom.IsPortOpen ? "Close" : "Open"; OnPropertyChanged(nameof(SerialCom)); } catch (System.Exception) { } }
private void ToolStripButtonConnect_Click(object sender, EventArgs e) { try { if (!_isConnected) { var portName = comboBoxPortName.Text; if (comboBoxPortName.SelectedIndex >= 0) { portName = _portNames[comboBoxPortName.SelectedIndex]; } var baudRate = int.Parse(comboBoxBaudRate.Text); _serialCom = new SerialCom(portName, baudRate, _appConfig.SetDtrOnConnect); _serialCom.OnDataReceived += SerialCom_OnDataReceived; _serialCom.OnException += SerialCom_OnException; _serialCom.Open(); _isConnected = true; buttonConnect.Text = "Disconnect"; this.Text = string.Format("{0} [{1} - {2}]", Application.ProductName, portName, baudRate); UpdateStatusLabel("Connected at Baudrate " + baudRate); comboBoxPortName.Enabled = false; comboBoxBaudRate.Enabled = false; } else { _isConnected = false; buttonConnect.Text = "Connect"; comboBoxPortName.Enabled = true; comboBoxBaudRate.Enabled = true; UpdateStatusLabel("Disconnected"); this.Text = Application.ProductName; _serialCom.Close(); } UpdateSerialPortDescription(); } catch (Exception ex) { PopupException(ex.Message); } }
private void btnOpen_Click(object sender, EventArgs e) { if (btnOpen.Text == "Close") { _spr.Close(); btnOpen.Text = "Open"; } else { string portName = cbPortName.Text; int baudRate = cbBaudRate.Text.ToInt32(9600); Parity parity = (Parity)Enum.Parse(typeof(Parity), cbParity.Text); int dataBits = cbDataBits.Text.ToInt32(8); StopBits stopBits = (StopBits)Enum.Parse(typeof(StopBits), cbStopBits.Text); //StopBits stopBits = (StopBits)1; _spr = new SerialCom(portName, baudRate, parity, dataBits, stopBits); _spr.Open(); _spr.DataReceived += new SerialCom.SerialPorterDataReceiveEventArgs(_spr_DataReceived); //_spr.sp.DataReceived += new SerialDataReceivedEventHandler(spr_DataReceived); btnOpen.Text = "Close"; } }