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ů.
 
 

175 řádky
5.7 KiB

  1. using BPA.Utility;
  2. using HBLConsole.Communication;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using static HBLDevice.IceCream.MessageDefine;
  7. using HBLConsole.Service;
  8. using HBLConsole.Model;
  9. namespace HBLDevice.IceCream
  10. {
  11. public class IceCreamMachine
  12. {
  13. //指令组装
  14. private CommandHandler commandHandler = new CommandHandler();
  15. //通讯代理
  16. SerialPortClient commProxy = null;
  17. //数据仓库
  18. private DataStorage<byte> dataStorage = new DataStorage<byte>();
  19. //主线程运行标识
  20. private bool running = false;
  21. //是否下发指令,主线程等待
  22. private bool free = true;
  23. public Action<string> SendCallback;
  24. public Action<string> ReciveCallback;
  25. public IceCreamMachine(string portName, BaudRates baud)
  26. {
  27. commProxy = new SerialPortClient(portName, baud);
  28. commProxy.SetDataStorage(dataStorage);
  29. commandHandler.Init(commProxy);
  30. commandHandler.PauseAsk = delegate (bool pause)
  31. {
  32. free = !pause;
  33. };
  34. }
  35. public void Start()
  36. {
  37. commProxy.Start();
  38. running = true;
  39. MainLoop();
  40. }
  41. public void Stop()
  42. {
  43. }
  44. private MSG_RESOLVE_STEP currentStep;
  45. private void MainLoop()
  46. {
  47. ThreadManage.GetInstance.StartLong(new Action(() =>
  48. {
  49. if (free)
  50. {
  51. commProxy.SendData(commandHandler.GetHeartDW());
  52. SendCallback?.Invoke(BitConverter.ToString(commandHandler.GetHeartDW()));
  53. }
  54. Thread.Sleep(500);
  55. }), "冰淇淋询问线程");
  56. ThreadManage.GetInstance.StartLong(new Action(() =>
  57. {
  58. ResolveMsg();
  59. //Thread.Sleep(2000);
  60. }), "冰淇淋解析线程");
  61. //Executer.GetInstance().Start(() =>
  62. //{
  63. // while (running)
  64. // {
  65. // commProxy.SendData(commandHandler.GetHeartDW());
  66. // SendCallback?.Invoke(BitConverter.ToString(commandHandler.GetHeartDW()));
  67. // Thread.Sleep(2000);
  68. // }
  69. //}, "冰淇淋询问线程");
  70. //Executer.GetInstance().Start(() =>
  71. //{
  72. // ResolveMsg();
  73. //}, "冰淇淋解析线程");
  74. }
  75. int contentLength = 0;
  76. int currentContentOffset = 0;
  77. private void ResolveMsg()
  78. {
  79. //while (running)
  80. //{
  81. List<byte> temp = new List<byte>();
  82. //一系列解包
  83. while (dataStorage.GetSize() > 0)
  84. {
  85. byte item = dataStorage.GetData();
  86. switch (currentStep)
  87. {
  88. case MSG_RESOLVE_STEP.NONE:
  89. if (item == MessageDefine.HEADER1)
  90. {
  91. temp.Add(item);
  92. currentStep = MSG_RESOLVE_STEP.HEADER1;
  93. continue;
  94. }
  95. break;
  96. case MSG_RESOLVE_STEP.HEADER1:
  97. if (item == MessageDefine.HEADER2_UP)
  98. {
  99. temp.Add(item);
  100. currentStep = MSG_RESOLVE_STEP.HEADER2;
  101. continue;
  102. }
  103. else
  104. {
  105. temp.Clear();
  106. currentStep = MSG_RESOLVE_STEP.NONE;
  107. continue;
  108. }
  109. case MSG_RESOLVE_STEP.HEADER2:
  110. switch ((IC_CMD)item)
  111. {
  112. case IC_CMD.HEART:
  113. temp.Add(item);
  114. contentLength = MessageDefine.MSG_LENGTH[(IC_CMD)item];
  115. currentContentOffset = 0;
  116. currentStep = MSG_RESOLVE_STEP.CMD;
  117. break;
  118. default:
  119. temp.Clear();
  120. currentStep = MSG_RESOLVE_STEP.NONE;
  121. break;
  122. }
  123. break;
  124. }
  125. int retry = 3;
  126. while (dataStorage.GetSize() < contentLength + 2 && retry >= 0)
  127. {
  128. retry--;
  129. Thread.Sleep(100);
  130. }
  131. if (retry < 0)
  132. {
  133. currentStep = MSG_RESOLVE_STEP.NONE;
  134. currentContentOffset = 0;
  135. contentLength = 0;
  136. continue;
  137. }
  138. while (currentContentOffset < contentLength)
  139. {
  140. item = dataStorage.GetData();
  141. temp.Add(item);
  142. currentContentOffset++;
  143. }
  144. retry = 3;
  145. while (dataStorage.GetSize() < 2 && retry >= 0)
  146. {
  147. retry--;
  148. Thread.Sleep(100);
  149. }
  150. temp.Add(dataStorage.GetData());
  151. temp.Add(dataStorage.GetData());
  152. ReciveCallback?.Invoke(BitConverter.ToString(temp.ToArray()));
  153. MorkIStatus.GetInstance().ProcessMsg(temp.ToArray());
  154. currentStep = MSG_RESOLVE_STEP.NONE;
  155. continue;
  156. }
  157. Thread.Sleep(5);
  158. //}
  159. }
  160. }
  161. }