SerialPortEntity.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Ports;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace Core.StlMes.Client.Mcp.Mch.Mcms
  9. {
  10. public delegate void ReceiveDataHandler(object sender, object data, string Message,DataType dataType);
  11. public partial class SerialPortEntity : SerialPort
  12. {
  13. private CancellationTokenSource ct = new CancellationTokenSource();
  14. private Task task;
  15. private IProtocol _protocol;
  16. public SerialPortEntity(IProtocol iProtocol)
  17. {
  18. _protocol = iProtocol;
  19. }
  20. public void ChangeProtocol(IProtocol iProtocol)
  21. {
  22. _protocol = iProtocol;
  23. }
  24. public string StrBaudRate
  25. {
  26. get { return BaudRate.ToString(); }
  27. set
  28. {
  29. var iBaudRate = 0;
  30. if (!int.TryParse(value, out iBaudRate))
  31. iBaudRate = 9600;
  32. BaudRate = iBaudRate;
  33. }
  34. }
  35. public string StrDataBits
  36. {
  37. get { return DataBits.ToString(); }
  38. set
  39. {
  40. var iDataBits = 0;
  41. if (!int.TryParse(value, out iDataBits))
  42. iDataBits = 8;
  43. DataBits = iDataBits;
  44. }
  45. }
  46. public string StrParity
  47. {
  48. get
  49. {
  50. switch (Parity)
  51. {
  52. case Parity.None:
  53. return "NONE";
  54. case Parity.Even:
  55. return "EVEN";
  56. case Parity.Mark:
  57. return "MARK";
  58. case Parity.Odd:
  59. return "ODD";
  60. case Parity.Space:
  61. return "SPACE";
  62. default:
  63. return "EVEN";
  64. ;
  65. }
  66. }
  67. set
  68. {
  69. switch (value)
  70. {
  71. case "EVEN":
  72. Parity = Parity.Even;
  73. break;
  74. case "MARK":
  75. Parity = Parity.Mark;
  76. break;
  77. case "NONE":
  78. Parity = Parity.None;
  79. break;
  80. case "ODD":
  81. Parity = Parity.Odd;
  82. break;
  83. case "SPACE":
  84. Parity = Parity.Space;
  85. break;
  86. default:
  87. Parity = Parity.Even;
  88. break;
  89. }
  90. }
  91. }
  92. public string StrStopBits
  93. {
  94. get
  95. {
  96. switch (StopBits)
  97. {
  98. case StopBits.None:
  99. return "NONE";
  100. case StopBits.One:
  101. return "ONE";
  102. case StopBits.OnePointFive:
  103. return "ONEPOINTFIVE";
  104. case StopBits.Two:
  105. return "TWO";
  106. default:
  107. return "ONE";
  108. ;
  109. }
  110. }
  111. set
  112. {
  113. switch (value)
  114. {
  115. case "NONE":
  116. StopBits = StopBits.None;
  117. break;
  118. case "ONE":
  119. StopBits = StopBits.One;
  120. break;
  121. case "ONEPOINTFIVE":
  122. StopBits = StopBits.OnePointFive;
  123. break;
  124. case "TWO":
  125. StopBits = StopBits.Two;
  126. break;
  127. default:
  128. StopBits = StopBits.One;
  129. break;
  130. }
  131. }
  132. }
  133. public string DeviceName { get; set; }
  134. public event ReceiveDataHandler ReceiveData;
  135. protected virtual void OnReceiveData(object data, string message, DataType dataType)
  136. {
  137. var handler = ReceiveData;
  138. if (handler != null)
  139. new Task(() => { handler(this, data, DeviceName + " " + message, dataType); }).Start();
  140. // handler(this, data, DeviceName + " " + message);
  141. }
  142. public void Start()
  143. {
  144. try
  145. {
  146. if (_protocol == null)
  147. {
  148. OnReceiveData(null, "未指定协议", DataType.Error);
  149. return;
  150. }
  151. Open();
  152. OnReceiveData(null,
  153. string.Format("打开串口[{0}--{1} {2} {3} {4}]成功!", PortName, StrBaudRate, StrParity, StrDataBits,
  154. StrStopBits), DataType.Open);
  155. ct = new CancellationTokenSource();
  156. task = new Task(ReceiveTask, ct.Token);
  157. task.Start();
  158. OnReceiveData(null, "开始监听数据",DataType.EvenMessage);
  159. }
  160. catch (Exception ex)
  161. {
  162. OnReceiveData(null, ex.Message,DataType.Error);
  163. }
  164. }
  165. public void ReceiveTask()
  166. {
  167. var ReceiveData = new List<byte>();
  168. while (!ct.IsCancellationRequested)
  169. try
  170. {
  171. if (!IsOpen) continue;
  172. if (BytesToRead > 0)
  173. {
  174. //等待100毫秒 等足够帧过来
  175. Thread.Sleep(100);
  176. var Datas = new byte[BytesToRead];
  177. Read(Datas, 0, Datas.Length);
  178. ReceiveData = ReceiveData.Concat(Datas).ToList();
  179. OnReceiveData(null, "成功接受数据:" + Datas.Select(p=>p.ToString("X2")).Aggregate((a,b)=> a+ " " + b ) + "(" + Encoding.Default.GetString(Datas.ToArray()) + ")" , DataType.ReceiveData);
  180. object Data;
  181. DeframeType Result = _protocol.AnalaysData(ReceiveData, out Data);
  182. if (Result != DeframeType.LengthLess) ReceiveData.Clear();
  183. if (Result == DeframeType.RightFrame)
  184. {
  185. OnReceiveData(Data, "成功解析数据!" + Data.ToString(), DataType.ReceiveData);
  186. }
  187. if (Result == DeframeType.WrongFrame)
  188. {
  189. OnReceiveData(null, "数据解析失败",DataType.EvenMessage);
  190. }
  191. }
  192. }
  193. catch (Exception)
  194. {
  195. }
  196. }
  197. public void Stop()
  198. {
  199. try
  200. {
  201. ct.Cancel();
  202. task.Wait();
  203. }
  204. catch (Exception ex)
  205. {
  206. // ignored
  207. }
  208. Thread.Sleep(100);
  209. if (IsOpen)
  210. {
  211. Close();
  212. OnReceiveData(null, string.Format("关闭串口[{0}]成功!", PortName),DataType.Close);
  213. }
  214. }
  215. }
  216. public enum DataType
  217. {
  218. Open =0,
  219. Close = 1,
  220. SendData= 2,
  221. ReceiveData = 3,
  222. EvenMessage= 4,
  223. Error = 5,
  224. Other = 99
  225. }
  226. }