PortSettings.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO.Ports;
  9. namespace MOTINOVA_Motor_Factory_Set
  10. {
  11. public partial class PortSettings : Form
  12. {
  13. #region 串口相关变量
  14. private bool Listening = false;// 侦听串口是否是接收数据标志位
  15. public static string m_strDataBits = "8";
  16. public static string m_strStopBits = "1";
  17. public static string m_strCheckBits = "None";
  18. public static string m_strBaudrate = "115200";
  19. public bool g_blnIsOpen = false;
  20. #endregion
  21. public PortSettings()
  22. {
  23. InitializeComponent();
  24. }
  25. private void PortSettings_Load(object sender, EventArgs e)
  26. {
  27. #region 端口刷新
  28. string[] ports = SerialPort.GetPortNames();
  29. Array.Sort(ports);
  30. comboBox_ComIndex.Items.AddRange(ports);
  31. comboBox_ComIndex.SelectedIndex = comboBox_ComIndex.Items.Count > 0 ? 0 : -1;
  32. #endregion
  33. }
  34. #region 打开或关闭串口
  35. public bool openPort()
  36. {
  37. try
  38. {
  39. if (!serialPort1.IsOpen)
  40. {
  41. serialPort1.Open();
  42. g_blnIsOpen = true;
  43. }
  44. return true;
  45. }
  46. catch (System.Exception ex)
  47. {
  48. MessageBox.Show(ex.Message);
  49. return false;
  50. }
  51. }
  52. public bool closePort()
  53. {
  54. try
  55. {
  56. if (serialPort1.IsOpen)
  57. {
  58. while (Listening) Application.DoEvents();
  59. serialPort1.Close();
  60. g_blnIsOpen = false;
  61. }
  62. return true;
  63. }
  64. catch (System.Exception ex)
  65. {
  66. MessageBox.Show(ex.Message);
  67. return false;
  68. }
  69. }
  70. private void button_Enter_Click(object sender, EventArgs e)
  71. {
  72. serialPort1.PortName = comboBox_ComIndex.Text;
  73. serialPort1.BaudRate = int.Parse(m_strBaudrate);
  74. serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), m_strCheckBits);
  75. serialPort1.DataBits = Int32.Parse(m_strDataBits);
  76. serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), (m_strStopBits == "1.5") ? "3" : m_strStopBits);
  77. Hide();
  78. }
  79. #endregion
  80. #region 串口数据接收
  81. private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  82. {
  83. try
  84. {
  85. Listening = true;
  86. int n = serialPort1.BytesToRead;
  87. byte[] buf = new byte[n];//将一次串口事件中接收到的数据暂存于buf中(注:对于超过8个字节的数据,C#的
  88. //串口接收事件将按每次处理8个字节的方式多次处理
  89. serialPort1.Read(buf, 0, n);//将接收到的数据读入buf
  90. this.Invoke((EventHandler)delegate
  91. {
  92. });
  93. ImportForm.buffer.AddRange(buf);//将读取的数据放入buffer中
  94. CheckForm.buffer.AddRange(buf);
  95. }
  96. catch (Exception ex)
  97. {
  98. MessageBox.Show(ex.Message);
  99. return;
  100. }
  101. finally
  102. {
  103. Listening = false;
  104. }
  105. }
  106. #endregion
  107. private void button_Connect_Click(object sender, EventArgs e)
  108. {
  109. try
  110. {
  111. if (!serialPort1.IsOpen)
  112. {
  113. serialPort1.PortName = comboBox_ComIndex.Text;
  114. serialPort1.BaudRate = int.Parse(m_strBaudrate);
  115. serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), m_strCheckBits);
  116. serialPort1.DataBits = Int32.Parse(m_strDataBits);
  117. serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), (m_strStopBits == "1.5") ? "3" : m_strStopBits);
  118. serialPort1.Open();
  119. comboBox_ComIndex.Enabled = false;
  120. button_Connect.Text = "断开";
  121. button_Enter.Enabled = false;
  122. g_blnIsOpen = true;
  123. }
  124. else
  125. {
  126. while (Listening) Application.DoEvents();
  127. //打开时点击,则关闭串口
  128. serialPort1.Close();
  129. comboBox_ComIndex.Enabled = true;
  130. button_Connect.Text = "连接";
  131. button_Enter.Enabled = true;
  132. g_blnIsOpen = false;
  133. }
  134. }
  135. catch (System.Exception ex)
  136. {
  137. MessageBox.Show(ex.Message);
  138. return;
  139. }
  140. this.Hide();
  141. }
  142. }
  143. }