终端一体化运控平台
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

244 lines
7.1 KiB

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