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.
 
 

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