终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SerialPortClient.cs 7.3 KiB

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