private void button6_Click(object sender, EventArgs e) { rec: if (button6.Text == "连接") { try { COM.BaudRate = 115200; COM.PortName = comboBox1.Text; COM.DataBits = 8; COM.StopBits = StopBits.One; COM.Parity = Parity.None; COM.ReadBufferSize = 8; COM.Open(); button6.Text = "断开"; } catch (Exception ex) { MessageBox.Show(ex.Message); } } else { try { COM.Close(); button6.Text = "连接"; } catch (Exception) { } } }
/// <summary> /// 开始按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void start_Click(object sender, EventArgs e) { if (start.Text == "Start") { try { COM.DataBits = Convert.ToInt32(comboBoxDataBits.SelectedItem.ToString()); COM.BaudRate = Convert.ToInt32(comboBoxBaudRate.SelectedItem); COM.PortName = comboBoxCom.SelectedItem.ToString(); COM.Open(); } catch (System.IO.IOException) { MessageBox.Show(COM.PortName + "打开失败,请检查连接。"); freshen.Visible = true; return; } catch (System.UnauthorizedAccessException) { MessageBox.Show("访问被拒绝,是否还有其他端口监听软件?"); return; } start.Text = "Stop"; sendTime(); } else { COM.Close(); start.Text = "Start"; progressBar.Value = 0; Warring = false; labelProgressBarValue.Text = "0"; } }
private void button2_Click(object sender, EventArgs e) { ushort[] txBuffer = { 1500, 2000, 1000, 1400 }; byte[] byteArray = GetByteArray(txBuffer); COM.PortName = SettCom.Default.Port; COM.BaudRate = SettCom.Default.BaudRate; COM.DataBits = SettCom.Default.DataBits; COM.ReadTimeout = SettCom.Default.ReadTimeout; COM.WriteTimeout = SettCom.Default.WriteTimeout; COM.Parity = SettCom.Default.Parity; COM.StopBits = SettCom.Default.StopBits; try { COM.Open(); COM.Write(byteArray, 0, 2 * txBuffer.Length); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (COM.IsOpen) { COM.Close(); } } }
private void btnPortClose_Click(object sender, EventArgs e) { try { if (loop) // end read { loop = false; thread.Join(); thread = null; } if (COM.IsOpen) // close { COM.Close(); } if (!COM.IsOpen) { lbCOMstatus.Text = "串口已关闭"; } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
/// <summary> /// 窗口关闭 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_FormClosing(object sender, FormClosingEventArgs e) { this.Controls.Clear(); if (start.Text == "Start") { COM.Close(); } }
private void ConnectMenu_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e) { string item = e.ClickedItem.ToString(); //ToolStripItemClickedEventArgs.ClickedItem; if (item == "Отключить" && COM.IsOpen) { COM.Close(); is_connected = false; stateForm = WorkState.Idle; // Connectbtn.Enabled = false; stateRegs = WorkState.Idle; //return; } else if (item == "Настройка") { if (!is_open) { if (COM.IsOpen) { COM.Close(); } stateRegs = WorkState.Idle; stateForm = WorkState.Idle; Form Cs = new WindowsFormsApplication1.COM_settings(); Cs.Show(); is_open = true; Csisopen = true; } } else { if (COM.IsOpen) { COM.Close(); } COM.PortName = item; try { COM.Open(); is_connected = true; stateForm = WorkState.Update; stateRegs = WorkState.Update; } catch (Exception ex) { m_lastError = ex.Message; } } }
private void PortClose_Click(object sender, EventArgs e) { if (COM.IsOpen) { COM.Close(); } else { MessageBox.Show("Port Closed."); } }
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) { if (dataFromWeigherCount < DataFromWeigherSelectionCount - 1) { AddDataInArray(sender); dataFromWeigherCount++; } else { float value = Parser.GetFilteredWeight(dataArr); Log.Write($"Weigh done. Finish weigh={value}"); WeighFinish?.Invoke(value); dataArr = null; COM.mySerialPort.DataReceived -= DataReceivedHandler; COM.Close(); } }
public void GetData() { dataFromWeigherCount = 0; dataArr = new float[DataFromWeigherSelectionCount - 2]; try { if (COM.mySerialPort.IsOpen == true) { COM.Close(); } COM.mySerialPort.Open(); COM.mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); Log.Write("Scale subscribe for weigh event"); } catch { } }
private void PbConect_Click(object sender, EventArgs e) { if (Lbss.Text == "Disconnect") { COM.PortName = Cbcom.Text; //Chon cong COM ket voi voi VXL COM.Open(); Lbss.Text = "Connect"; PbConect.Text = "Disconnect"; // Sau khi nhan Connect cho phep nhap du lieu de gui len VXL PbStart.Enabled = true; } else { // Khi tat cong Ket noi ta set lai gia tri ban dau nhu khi Form Load COM.Close(); Lbss.Text = "Disconnect"; PbConect.Text = "Connect"; PbStart.Enabled = false; } }
/// <summary> /// 窗口载入 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { string[] comlist = SerialPort.GetPortNames(); comboBoxCom.Items.Clear(); if (comlist.Length == 0) { MessageBox.Show("无串口设备"); //freshen.Visible = true; if (start.Text == "Start") { start.Text = "No Link"; start.Enabled = false; COM.Close(); } } else { //freshen.Visible = false; foreach (var s in comlist) { comboBoxCom.Items.Add(s); } //comboBoxCom选定条目 comboBoxCom.SelectedIndex = 0; //comboBoxBaudRate 1.9600 3.38400 4.115200 comboBoxBaudRate.SelectedIndex = 4; //comboBoxWordLength comboBoxDataBits.SelectedIndex = 3; //开始按钮使能 start.Enabled = true; if (start.Text == "No Link") { start.Text = "Start"; } //恢复上次的阈值 trackBarMax.Value = Properties.Settings.Default.MaxValue; trackBarMin.Value = Properties.Settings.Default.MinValue; labelTrackBarMax.Text = trackBarMax.Value.ToString(); labelTrackBarMin.Text = trackBarMin.Value.ToString(); } }
private void PbOpen_Click(object sender, EventArgs e) { if (PbOpen.Text == "Open") { COM.BaudRate = 9600; COM.PortName = CbSellectCom.Text; COM.Open(); if (COM.IsOpen == true) { label.Text = "Connected"; } PbOpen.Text = "Close"; Cursor.Position = new Point(300, 300); } else { COM.Close(); if (COM.IsOpen == false) { PbOpen.Text = "Open"; label.Text = "Disconnected"; } } }
private void btnConnect_Click(object sender, EventArgs e) { //连接COM或断开 if (cmbCOM.Text == "") { txtTips.Text += "选择COM口!\r\n"; } else if (cmbBaud.Text == "") { txtTips.Text += "选择波特率!\r\n"; } else { if (btnConnect.Text == "连接") { COM.PortName = cmbCOM.Text; int baud; int.TryParse(cmbBaud.Text, out baud); COM.BaudRate = baud; if (!COM.IsOpen) { try { COM.Open(); cmbCOM.Enabled = false; }catch (System.IO.IOException exce) { txtTips.Text += exce.Message; } } if (!COM.IsOpen) { txtTips.Text += "连接失败!\r\n"; } } else if (btnConnect.Text == "关闭") { if (COM.IsOpen) { try { COM.Close(); cmbCOM.Enabled = true; } catch (System.IO.IOException exce) { txtTips.Text += exce.Message; } } if (COM.IsOpen) { txtTips.Text += "关闭失败!\r\n"; } } } if (COM.IsOpen) { btnConnect.Text = "关闭"; } else { btnConnect.Text = "连接"; } }
private void button2_Click(object sender, EventArgs e) { bool isException = false; try { COM.PortName = comboBox1.SelectedItem.ToString(); COM.BaudRate = int.Parse(comboBox2.SelectedItem.ToString()); COM.DataBits = int.Parse(comboBox3.SelectedItem.ToString()); COM.ReadTimeout = int.Parse(textBox1.Text); COM.WriteTimeout = int.Parse(textBox2.Text); switch (comboBox4.SelectedItem) { case "None": COM.Parity = Parity.None; break; case "Even": COM.Parity = Parity.Even; break; case "Odd": COM.Parity = Parity.Odd; break; case "Mark": COM.Parity = Parity.Mark; break; case "Space": COM.Parity = Parity.Space; break; } switch (comboBox5.SelectedItem) { case "One": COM.StopBits = StopBits.One; break; case "OnePointFive": COM.StopBits = StopBits.OnePointFive; break; case "Two": COM.StopBits = StopBits.Two; break; } COM.Open(); } catch (Exception ex) { MessageBox.Show("ERROR: Открытие невозможно : " + ex.Message); isException = true; } finally { if (COM.IsOpen) { COM.Close(); } if (!isException) { button1.Enabled = true; MessageBox.Show("Succesfull opened COM-port. Save settings"); } } }