SerialPortBase.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using ProjectBase.Data.Logs;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO.Ports;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace SIASUN.Autopilot.GNSS
  10. {
  11. public class SerialPortBase
  12. {
  13. public event EventSerialDataReceiveHandler serialDataReceived;
  14. private SerialPort serialPort;
  15. private Thread thread;
  16. volatile bool _keepReading;
  17. /// <summary>
  18. /// 串口通信
  19. /// </summary>
  20. /// <param name="PortNumber">端口号</param>
  21. /// <param name="baudRate">波特率</param>
  22. /// <param name="DataBits">数据位</param>
  23. public SerialPortBase(string PortNumber, int baudRate, int DataBits)
  24. {
  25. serialPort = new SerialPort();
  26. thread = null;
  27. _keepReading = false;
  28. serialPort.PortName = PortNumber;
  29. // 波特率
  30. serialPort.BaudRate = baudRate;
  31. // 数据位
  32. serialPort.DataBits = DataBits;
  33. // 两个停止位
  34. serialPort.StopBits = System.IO.Ports.StopBits.One;
  35. // 无奇偶校验位
  36. serialPort.Parity = System.IO.Ports.Parity.None;
  37. serialPort.ReadTimeout = 100;
  38. serialPort.WriteTimeout = -1;
  39. Open();
  40. }
  41. // 串口是否打开
  42. public bool IsOpen
  43. {
  44. get
  45. {
  46. return serialPort.IsOpen;
  47. }
  48. }
  49. /// <summary>
  50. /// 开始接收数据
  51. /// </summary>
  52. private void StartReading()
  53. {
  54. if (!_keepReading)
  55. {
  56. _keepReading = true;
  57. thread = new Thread(new ThreadStart(ReadPort));
  58. thread.Start();
  59. }
  60. }
  61. /// <summary>
  62. /// 停止读取数据
  63. /// </summary>
  64. public void StopReading()
  65. {
  66. if (_keepReading)
  67. {
  68. _keepReading = false;
  69. thread.Join();
  70. thread = null;
  71. LogHelper.log.Debug("关闭接收线程。");
  72. }
  73. }
  74. /// <summary>
  75. /// 数据接收
  76. /// </summary>
  77. private void ReadPort()
  78. {
  79. LogHelper.log.Debug("开始接收数据。");
  80. //START:
  81. while (_keepReading)
  82. {
  83. int offset = 0;
  84. int timeout = 0;
  85. byte[] readBuffer = new byte[1024 * 8];
  86. while (IsOpen)
  87. {
  88. int count = serialPort.BytesToRead;
  89. if (count > 0)
  90. {
  91. if ((offset + count) > 1024 * 8)
  92. {
  93. break;
  94. }
  95. int readCount = serialPort.Read(readBuffer, offset, count);
  96. offset += readCount;
  97. timeout = 0;
  98. }
  99. else
  100. {
  101. break;
  102. }
  103. if (timeout > 100)
  104. {
  105. break;
  106. }
  107. //Thread.Sleep(10);
  108. timeout++;
  109. }
  110. string strData = System.Text.Encoding.Default.GetString(readBuffer).TrimEnd('\0');
  111. if (!string.IsNullOrEmpty(strData))
  112. {
  113. if (serialDataReceived != null)
  114. {
  115. SerialPortDataReceiveEventArgs receive = new SerialPortDataReceiveEventArgs(strData);
  116. serialDataReceived(receive);
  117. }
  118. LogHelper.log.Debug("接收数据完成 。");
  119. }
  120. }
  121. }
  122. /// <summary>
  123. /// 串口打开
  124. /// </summary>
  125. private void Open()
  126. {
  127. try
  128. {
  129. Close();
  130. serialPort.Open();
  131. if (serialPort.IsOpen)
  132. {
  133. StartReading();
  134. }
  135. else
  136. {
  137. LogHelper.log.Error("串口打开失败。");
  138. }
  139. }
  140. catch (Exception ex)
  141. {
  142. LogHelper.log.Error("打开串口时出现错误,错误为", ex);
  143. return;
  144. }
  145. }
  146. /// <summary>
  147. /// 串口关闭
  148. /// </summary>
  149. public void Close()
  150. {
  151. StopReading();
  152. serialPort.Close();
  153. }
  154. /// <summary>
  155. /// 串口写入数据
  156. /// </summary>
  157. /// <param name="send"></param>
  158. /// <param name="offSet"></param>
  159. /// <param name="count"></param>
  160. public void WritePort(byte[] send, int offSet, int count)
  161. {
  162. if (IsOpen)
  163. {
  164. LogHelper.log.Debug("开始写入数据。");
  165. serialPort.Write(send, offSet, count);
  166. }
  167. }
  168. }
  169. /// <summary>
  170. /// 串口接收数据
  171. /// </summary>
  172. public delegate void EventSerialDataReceiveHandler(SerialPortDataReceiveEventArgs e);
  173. public class SerialPortDataReceiveEventArgs : EventArgs
  174. {
  175. public string Value { get; set; }
  176. public SerialPortDataReceiveEventArgs(string value)
  177. {
  178. this.Value = value;
  179. }
  180. }
  181. }