终端一体化运控平台
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

252 řádky
7.3 KiB

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