终端一体化运控平台
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

156 satır
5.2 KiB

  1. using BPASmartClient.Helper;
  2. using BPASmartClient.SerialPort;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using static BPASmartClient.GSIceCream.MessageDefine;
  7. namespace BPASmartClient.GSIceCream
  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. //是否下发指令,主线程等待
  20. private bool free = true;
  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. commandHandler.PauseAsk = delegate (bool pause)
  29. {
  30. free = !pause;
  31. };
  32. }
  33. public void Start()
  34. {
  35. commProxy.Start();
  36. running = true;
  37. MainLoop();
  38. }
  39. public void Stop()
  40. {
  41. }
  42. private MSG_RESOLVE_STEP currentStep;
  43. private void MainLoop()
  44. {
  45. ThreadManage.GetInstance.StartLong(new Action(() =>
  46. {
  47. if (free)
  48. {
  49. commProxy.SendData(commandHandler.GetHeartDW());
  50. SendCallback?.Invoke(BitConverter.ToString(commandHandler.GetHeartDW()));
  51. }
  52. Thread.Sleep(500);
  53. }), "冰淇淋询问线程");
  54. ThreadManage.GetInstance.StartLong(new Action(() =>
  55. {
  56. ResolveMsg();
  57. //Thread.Sleep(2000);
  58. }), "冰淇淋解析线程");
  59. }
  60. int contentLength = 0;
  61. int currentContentOffset = 0;
  62. private void ResolveMsg()
  63. {
  64. //while (running)
  65. //{
  66. List<byte> temp = new List<byte>();
  67. //一系列解包
  68. while (dataStorage.GetSize() > 0)
  69. {
  70. byte item = dataStorage.GetData();
  71. switch (currentStep)
  72. {
  73. case MSG_RESOLVE_STEP.NONE:
  74. if (item == MessageDefine.HEADER1)
  75. {
  76. temp.Add(item);
  77. currentStep = MSG_RESOLVE_STEP.HEADER1;
  78. continue;
  79. }
  80. break;
  81. case MSG_RESOLVE_STEP.HEADER1:
  82. if (item == MessageDefine.HEADER2_UP)
  83. {
  84. temp.Add(item);
  85. currentStep = MSG_RESOLVE_STEP.HEADER2;
  86. continue;
  87. }
  88. else
  89. {
  90. temp.Clear();
  91. currentStep = MSG_RESOLVE_STEP.NONE;
  92. continue;
  93. }
  94. case MSG_RESOLVE_STEP.HEADER2:
  95. switch ((IC_CMD)item)
  96. {
  97. case IC_CMD.HEART:
  98. temp.Add(item);
  99. contentLength = MessageDefine.MSG_LENGTH[(IC_CMD)item];
  100. currentContentOffset = 0;
  101. currentStep = MSG_RESOLVE_STEP.CMD;
  102. break;
  103. default:
  104. temp.Clear();
  105. currentStep = MSG_RESOLVE_STEP.NONE;
  106. break;
  107. }
  108. break;
  109. }
  110. int retry = 3;
  111. while (dataStorage.GetSize() < contentLength + 2 && retry >= 0)
  112. {
  113. retry--;
  114. Thread.Sleep(100);
  115. }
  116. if (retry < 0)
  117. {
  118. currentStep = MSG_RESOLVE_STEP.NONE;
  119. currentContentOffset = 0;
  120. contentLength = 0;
  121. continue;
  122. }
  123. while (currentContentOffset < contentLength)
  124. {
  125. item = dataStorage.GetData();
  126. temp.Add(item);
  127. currentContentOffset++;
  128. }
  129. retry = 3;
  130. while (dataStorage.GetSize() < 2 && retry >= 0)
  131. {
  132. retry--;
  133. Thread.Sleep(100);
  134. }
  135. temp.Add(dataStorage.GetData());
  136. temp.Add(dataStorage.GetData());
  137. ReciveCallback?.Invoke(BitConverter.ToString(temp.ToArray()));
  138. MorkIStatus.GetInstance().ProcessMsg(temp.ToArray());
  139. currentStep = MSG_RESOLVE_STEP.NONE;
  140. continue;
  141. }
  142. Thread.Sleep(5);
  143. //}
  144. }
  145. }
  146. }