终端一体化运控平台
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

SerialPortClient.cs 7.4 KiB

il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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)
  142. {
  143. //MessageLog.GetInstance.Show("咖乐美咖啡机连接失败");
  144. return;
  145. }
  146. if (comPort.IsOpen) comPort.Close();
  147. comPort.PortName = portName;
  148. comPort.BaudRate = (int)baudRate;
  149. comPort.Parity = parity;
  150. comPort.DataBits = (int)dataBits;
  151. comPort.StopBits = stopBits;
  152. comPort.Open();
  153. //MessageLog.GetInstance.Show("咖乐美咖啡机连接成功");
  154. }
  155. }
  156. /// <summary>
  157. /// 关闭端口
  158. /// </summary>
  159. public void Close()
  160. {
  161. if (!IsHavePort) return;
  162. if (comPort.IsOpen) comPort.Close();
  163. }
  164. /// <summary>
  165. /// 丢弃来自串行驱动程序的接收和发送缓冲区的数据
  166. /// </summary>
  167. public void DiscardBuffer()
  168. {
  169. comPort.DiscardInBuffer();
  170. comPort.DiscardOutBuffer();
  171. }
  172. #endregion
  173. private object lck4Serial = new object();
  174. #region 写入数据
  175. /// <summary>
  176. /// 写入数据
  177. /// </summary>
  178. /// <param name="buffer"></param>
  179. public void Write(byte[] buffer, int offset, int count)
  180. {
  181. lock (lck4Serial)
  182. {
  183. if (!IsHavePort) return;
  184. if (!(comPort.IsOpen)) comPort.Open();
  185. comPort.Write(buffer, offset, count);
  186. }
  187. }
  188. /// <summary>
  189. /// 读取数据
  190. /// </summary>
  191. /// <returns></returns>
  192. public byte[] Read()
  193. {
  194. lock (lck4Serial)
  195. {
  196. if (!IsHavePort) return new byte[0];
  197. if (!(comPort.IsOpen)) comPort.Open();
  198. byte[] buffer = new byte[1024];
  199. comPort.Read(buffer, 0, buffer.Length);
  200. return buffer;
  201. }
  202. }
  203. public void Start()
  204. {
  205. Open();
  206. }
  207. public void Stop()
  208. {
  209. Close();
  210. }
  211. private static readonly object objLock = new object();
  212. /// <summary>
  213. /// 发送数据
  214. /// </summary>
  215. /// <param name="data">发送数据</param>
  216. public void SendData(byte[] data)
  217. {
  218. lock(objLock)
  219. Write(data, 0, data.Length);
  220. }
  221. public void SetDataStorage(DataStorage<byte> dataStorage)
  222. {
  223. this.dataStorage = dataStorage;
  224. }
  225. #endregion
  226. }
  227. }