選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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