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; using System.Collections; using System.IO; 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(); #region 端口刷新 string[] ports = SerialPort.GetPortNames(); string SavePort = ""; Array.Sort(ports); comboBox_ComIndex.Items.AddRange(ports); //打开文件 StreamReader objReader = new StreamReader(Directory.GetCurrentDirectory() + "\\Port"); string sLine = ""; ArrayList array_CfgInfo = new ArrayList(); array_CfgInfo.Clear(); while (sLine != null) { sLine = objReader.ReadLine(); array_CfgInfo.Add(sLine); } objReader.Close(); try { SavePort = array_CfgInfo[0].ToString().Split('=')[1]; m_strBaudrate = array_CfgInfo[1].ToString().Split('=')[1]; comboBox_ComIndex.SelectedIndex = comboBox_ComIndex.Items.IndexOf(SavePort); } catch (Exception) { comboBox_ComIndex.SelectedIndex = -1; } #endregion } private void PortSettings_Load(object sender, EventArgs e) { } #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(); } private void comboBox_ComIndex_SelectedIndexChanged(object sender, EventArgs e) { //删除原文件 if (File.Exists(Directory.GetCurrentDirectory()+"\\Port")) File.Delete(Directory.GetCurrentDirectory() + "\\Port"); //保存文件 string Info = ""; Info += "PORT=" + comboBox_ComIndex.Text + "\r\n"; Info += "BAUD=115200"; System.IO.File.WriteAllText(Directory.GetCurrentDirectory() + "\\Port", Info); } } }