123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.IO.Ports;
- namespace MOTINOVA_Motor_Factory_Set
- {
- public partial class PortSettings : Form
- {
- #region 串口相关变量
- private bool Listening = false;// 侦听串口是否是接收数据标志位
- public static string m_strDataBits = "8";
- public static string m_strStopBits = "1";
- public static string m_strCheckBits = "None";
- public static string m_strBaudrate = "115200";
- public bool g_blnIsOpen = false;
- #endregion
- public PortSettings()
- {
- InitializeComponent();
- }
- private void PortSettings_Load(object sender, EventArgs e)
- {
- #region 端口刷新
- string[] ports = SerialPort.GetPortNames();
- Array.Sort(ports);
- comboBox_ComIndex.Items.AddRange(ports);
- comboBox_ComIndex.SelectedIndex = comboBox_ComIndex.Items.Count > 0 ? 0 : -1;
- #endregion
- }
- #region 打开或关闭串口
- public bool openPort()
- {
- try
- {
- if (!serialPort1.IsOpen)
- {
- serialPort1.Open();
- g_blnIsOpen = true;
- }
- return true;
- }
- catch (System.Exception ex)
- {
- MessageBox.Show(ex.Message);
- return false;
- }
- }
- public bool closePort()
- {
- try
- {
- if (serialPort1.IsOpen)
- {
- while (Listening) Application.DoEvents();
- serialPort1.Close();
- g_blnIsOpen = false;
- }
- return true;
- }
- catch (System.Exception ex)
- {
- MessageBox.Show(ex.Message);
- return false;
- }
- }
- private void button_Enter_Click(object sender, EventArgs e)
- {
- serialPort1.PortName = comboBox_ComIndex.Text;
- serialPort1.BaudRate = int.Parse(m_strBaudrate);
- serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), m_strCheckBits);
- serialPort1.DataBits = Int32.Parse(m_strDataBits);
- serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), (m_strStopBits == "1.5") ? "3" : m_strStopBits);
- Hide();
- }
- #endregion
- #region 串口数据接收
- private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
- {
- try
- {
- Listening = true;
- int n = serialPort1.BytesToRead;
- byte[] buf = new byte[n];//将一次串口事件中接收到的数据暂存于buf中(注:对于超过8个字节的数据,C#的
- //串口接收事件将按每次处理8个字节的方式多次处理
- serialPort1.Read(buf, 0, n);//将接收到的数据读入buf
- this.Invoke((EventHandler)delegate
- {
- });
- ImportForm.buffer.AddRange(buf);//将读取的数据放入buffer中
- CheckForm.buffer.AddRange(buf);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- return;
- }
- finally
- {
- Listening = false;
- }
- }
- #endregion
- private void button_Connect_Click(object sender, EventArgs e)
- {
- try
- {
- if (!serialPort1.IsOpen)
- {
- serialPort1.PortName = comboBox_ComIndex.Text;
- serialPort1.BaudRate = int.Parse(m_strBaudrate);
- serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), m_strCheckBits);
- serialPort1.DataBits = Int32.Parse(m_strDataBits);
- serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), (m_strStopBits == "1.5") ? "3" : m_strStopBits);
- serialPort1.Open();
- comboBox_ComIndex.Enabled = false;
- button_Connect.Text = "断开";
- button_Enter.Enabled = false;
- g_blnIsOpen = true;
- }
- else
- {
- while (Listening) Application.DoEvents();
- //打开时点击,则关闭串口
- serialPort1.Close();
- comboBox_ComIndex.Enabled = true;
- button_Connect.Text = "连接";
- button_Enter.Enabled = true;
- g_blnIsOpen = false;
- }
- }
- catch (System.Exception ex)
- {
- MessageBox.Show(ex.Message);
- return;
- }
- this.Hide();
- }
- }
- }
|