终端一体化运控平台
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SerialPortClient.cs 7.1 KiB

2 年前
2 年前
2 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using BPASmartClient.Helper;
  2. using BPASmartClient.Message;
  3. using System;
  4. using System.IO.Ports;
  5. using System.Linq;
  6. namespace BPASmartClient.SerialPort
  7. {
  8. public class SerialPortClient
  9. {
  10. #region 变量属性
  11. private DataStorage<byte> dataStorage;
  12. public event SerialErrorReceivedEventHandler ErrorReceived;
  13. private System.IO.Ports.SerialPort comPort = new System.IO.Ports.SerialPort();
  14. private string portName = "COM1";//串口号,默认COM1
  15. private BaudRates baudRate = BaudRates.BR_9600;//波特率
  16. private Parity parity = Parity.None;//校验位
  17. private StopBits stopBits = StopBits.One;//停止位
  18. private DataBits dataBits = DataBits.Eight;//数据位
  19. /// <summary>
  20. /// 串口号
  21. /// </summary>
  22. public string PortName
  23. {
  24. get { return portName; }
  25. set { portName = value; }
  26. }
  27. /// <summary>
  28. /// 波特率
  29. /// </summary>
  30. public BaudRates BaudRate
  31. {
  32. get { return baudRate; }
  33. set { baudRate = value; }
  34. }
  35. #endregion
  36. #region 构造函数
  37. /// <summary>
  38. /// 无参构造函数
  39. /// </summary>
  40. public SerialPortClient(string name, BaudRates baud)
  41. {
  42. this.portName = name;
  43. this.baudRate = baud;
  44. BoundEvents();
  45. }
  46. void BoundEvents()
  47. {
  48. comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
  49. comPort.ErrorReceived += new SerialErrorReceivedEventHandler(comPort_ErrorReceived);
  50. }
  51. /// <summary>
  52. /// 参数构造函数(使用枚举参数构造)
  53. /// </summary>
  54. /// <param name="baud">波特率</param>
  55. /// <param name="par">奇偶校验位</param>
  56. /// <param name="sBits">停止位</param>
  57. /// <param name="dBits">数据位</param>
  58. /// <param name="name">串口号</param>
  59. public SerialPortClient(string name, BaudRates baud, Parity par, DataBits dBits, StopBits sBits)
  60. {
  61. this.portName = name;
  62. this.baudRate = baud;
  63. this.parity = par;
  64. this.dataBits = dBits;
  65. this.stopBits = sBits;
  66. BoundEvents();
  67. }
  68. /// <summary>
  69. /// 参数构造函数(使用字符串参数构造)
  70. /// </summary>
  71. /// <param name="baud">波特率</param>
  72. /// <param name="par">奇偶校验位</param>
  73. /// <param name="sBits">停止位</param>
  74. /// <param name="dBits">数据位</param>
  75. /// <param name="name">串口号</param>
  76. public SerialPortClient(string name, string baud, string par, string dBits, string sBits)
  77. {
  78. this.portName = name;
  79. this.baudRate = (BaudRates)Enum.Parse(typeof(BaudRates), baud);
  80. this.parity = (Parity)Enum.Parse(typeof(Parity), par);
  81. this.dataBits = (DataBits)Enum.Parse(typeof(DataBits), dBits);
  82. this.stopBits = (StopBits)Enum.Parse(typeof(StopBits), sBits);
  83. BoundEvents();
  84. }
  85. #endregion
  86. #region 事件处理函数
  87. /// <summary>
  88. /// 数据接收处理
  89. /// </summary>
  90. void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
  91. {
  92. if (comPort.IsOpen) //判断是否打开串口
  93. {
  94. byte[] receivedData = new byte[comPort.BytesToRead]; //创建接收字节数组
  95. comPort.Read(receivedData, 0, receivedData.Length); //读取数据
  96. //触发整条记录的处理
  97. if (dataStorage != null)
  98. {
  99. dataStorage.PutData(receivedData);
  100. }
  101. }
  102. else
  103. {
  104. Console.WriteLine("请打开某个串口", "Error");
  105. }
  106. }
  107. /// <summary>
  108. /// 错误处理函数
  109. /// </summary>
  110. void comPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
  111. {
  112. if (ErrorReceived != null)
  113. {
  114. ErrorReceived(sender, e);
  115. }
  116. }
  117. #endregion
  118. #region 串口关闭/打开
  119. /// <summary>
  120. /// 端口是否已经打开
  121. /// </summary>
  122. public bool IsOpen
  123. {
  124. get
  125. {
  126. return comPort.IsOpen;
  127. }
  128. }
  129. /// <summary>
  130. /// 判断是否有这个串口
  131. /// </summary>
  132. public bool IsHavePort => System.IO.Ports.SerialPort.GetPortNames().Contains(PortName);
  133. /// <summary>
  134. /// 打开端口
  135. /// </summary>
  136. /// <returns></returns>
  137. public void Open()
  138. {
  139. lock (lck4Serial)
  140. {
  141. if (!IsHavePort) return;
  142. if (comPort.IsOpen) comPort.Close();
  143. comPort.PortName = portName;
  144. comPort.BaudRate = (int)baudRate;
  145. comPort.Parity = parity;
  146. comPort.DataBits = (int)dataBits;
  147. comPort.StopBits = stopBits;
  148. comPort.Open();
  149. }
  150. }
  151. /// <summary>
  152. /// 关闭端口
  153. /// </summary>
  154. public void Close()
  155. {
  156. if (!IsHavePort) return;
  157. if (comPort.IsOpen) comPort.Close();
  158. }
  159. /// <summary>
  160. /// 丢弃来自串行驱动程序的接收和发送缓冲区的数据
  161. /// </summary>
  162. public void DiscardBuffer()
  163. {
  164. comPort.DiscardInBuffer();
  165. comPort.DiscardOutBuffer();
  166. }
  167. #endregion
  168. private object lck4Serial = new object();
  169. #region 写入数据
  170. /// <summary>
  171. /// 写入数据
  172. /// </summary>
  173. /// <param name="buffer"></param>
  174. public void Write(byte[] buffer, int offset, int count)
  175. {
  176. lock (lck4Serial)
  177. {
  178. if (!IsHavePort) return;
  179. if (!(comPort.IsOpen)) comPort.Open();
  180. comPort.Write(buffer, offset, count);
  181. }
  182. }
  183. /// <summary>
  184. /// 读取数据
  185. /// </summary>
  186. /// <returns></returns>
  187. public byte[] Read()
  188. {
  189. lock (lck4Serial)
  190. {
  191. if (!IsHavePort) return new byte[0];
  192. if (!(comPort.IsOpen)) comPort.Open();
  193. byte[] buffer = new byte[1024];
  194. comPort.Read(buffer, 0, buffer.Length);
  195. return buffer;
  196. }
  197. }
  198. public void Start()
  199. {
  200. Open();
  201. }
  202. public void Stop()
  203. {
  204. Close();
  205. }
  206. /// <summary>
  207. /// 发送数据
  208. /// </summary>
  209. /// <param name="data">发送数据</param>
  210. public void SendData(byte[] data)
  211. {
  212. Write(data, 0, data.Length);
  213. }
  214. public void SetDataStorage(DataStorage<byte> dataStorage)
  215. {
  216. this.dataStorage = dataStorage;
  217. }
  218. #endregion
  219. }
  220. }