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.
 
 

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