终端一体化运控平台
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

390 lines
15 KiB

  1. using BPASmartClient.EventBus;
  2. using BPASmartClient.Helper;
  3. using BPASmartClient.Message;
  4. using BPASmartClient.Model;
  5. using BPASmartClient.Model.单片机;
  6. using BPASmartClient.Model.单片机.Enum;
  7. using BPASmartClient.Peripheral;
  8. using BPASmartClient.SerialPort;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Runtime.InteropServices;
  12. using System.Threading;
  13. using static BPASmartClient.EventBus.EventBus;
  14. namespace BPASmartClient.SCChip
  15. {
  16. public class ICChipMachine :BasePeripheral
  17. {
  18. //通讯代理
  19. SerialPortClient commProxy = null;
  20. //数据仓库
  21. private DataStorage<byte> dataStorage = new DataStorage<byte>();
  22. //单片机基础协议
  23. private ICChipPackage package = new ICChipPackage();
  24. //串口COM口
  25. public string PortName { get; set; }
  26. //串口波特率
  27. public string BaudRate { get; set; }
  28. //心跳时间
  29. private DateTime lastRefreshTime = DateTime.MinValue;
  30. /// <summary>
  31. /// 是否在线
  32. /// </summary>
  33. public bool OnLine { get { return DateTime.Now.Subtract(lastRefreshTime).TotalSeconds <= 3; } }
  34. public ICChipMachine()
  35. {
  36. }
  37. /// <summary>
  38. /// 主线程开始运行
  39. /// </summary>
  40. public override void Start()
  41. {
  42. try
  43. {
  44. commProxy.Start();
  45. IsConnected = true;
  46. MainLoop();
  47. }
  48. catch (Exception ex)
  49. {
  50. MessageLog.GetInstance.Show($"BPASmartClient.SCChip 中引发错误,ICChipMachine 类,描述:[{ex.Message}]");
  51. MessageLog.GetInstance.ShowEx($"BPASmartClient.SCChip 中引发错误,ICChipMachine 类,描述:[{ex.Message}]");
  52. }
  53. }
  54. /// <summary>
  55. /// 停止运行
  56. /// </summary>
  57. public override void Stop()
  58. {
  59. try
  60. {
  61. commProxy.Stop();
  62. IsConnected = false;
  63. }
  64. catch (Exception ex)
  65. {
  66. MessageLog.GetInstance.Show($"BPASmartClient.SCChip 中引发错误,ICChipMachine 类,描述:[{ex.Message}]");
  67. MessageLog.GetInstance.ShowEx($"BPASmartClient.SCChip 中引发错误,ICChipMachine 类,描述:[{ex.Message}]");
  68. }
  69. }
  70. /// <summary>
  71. /// 主循环,循环询问状态
  72. /// </summary>
  73. private void MainLoop()
  74. {
  75. ThreadManage.GetInstance().StartLong(new Action(() =>
  76. {
  77. ResolveMsg();
  78. }),"单片机解析线程");
  79. }
  80. private void ResolveMsg()
  81. {
  82. List<byte> temp = new List<byte>();
  83. //一系列解包
  84. while (dataStorage.GetSize() > 0)
  85. {
  86. byte item = dataStorage.GetData();
  87. if (item == 0xAA)
  88. {
  89. temp.Add(item);
  90. while (dataStorage.GetSize() < 4) { Thread.Sleep(5); }
  91. while (temp.Count < 5)
  92. {
  93. temp.Add(dataStorage.GetData());
  94. }
  95. if (temp[4] == 0xBB)
  96. {
  97. var package = ByteToStructure(temp.ToArray());
  98. ProcessMsg(package);
  99. }
  100. temp.Clear();
  101. }
  102. continue;
  103. }
  104. Thread.Sleep(5);
  105. }
  106. /// <summary>
  107. /// 由byte数组转换为结构体
  108. /// </summary>
  109. private ICChipPackage ByteToStructure(byte[] dataBuffer)
  110. {
  111. ICChipPackage structure = new ICChipPackage();
  112. int size = Marshal.SizeOf(typeof(ICChipPackage));
  113. IntPtr allocIntPtr = Marshal.AllocHGlobal(size);
  114. try
  115. {
  116. Marshal.Copy(dataBuffer,0,allocIntPtr,size);
  117. structure = (ICChipPackage)Marshal.PtrToStructure(allocIntPtr,typeof(ICChipPackage));
  118. }
  119. finally
  120. {
  121. Marshal.FreeHGlobal(allocIntPtr);
  122. }
  123. return structure;
  124. }
  125. private byte[] StructureToByte(ICChipPackage structure)
  126. {
  127. structure.Header = 0xAA;
  128. structure.Sender = IC_SENDER.CONSOLE;
  129. structure.End = 0xBB;
  130. int size = Marshal.SizeOf(typeof(ICChipPackage));
  131. byte[] buffer = new byte[size];
  132. IntPtr bufferIntPtr = Marshal.AllocHGlobal(size);
  133. try
  134. {
  135. Marshal.StructureToPtr(structure,bufferIntPtr,true);
  136. Marshal.Copy(bufferIntPtr,buffer,0,size);
  137. }
  138. finally
  139. {
  140. Marshal.FreeHGlobal(bufferIntPtr);
  141. }
  142. return buffer;
  143. }
  144. public void ProcessMsg(ICChipPackage data)
  145. {
  146. try
  147. {
  148. switch (data.Cmd)
  149. {
  150. case IC_CMD.HEART_BEAT:
  151. lastRefreshTime = DateTime.Now;
  152. status["OnLine"] = OnLine;
  153. break;
  154. case IC_CMD.TAKE_CUP:
  155. switch ((IC_CUP)data.Value)
  156. {
  157. case IC_CUP.CUP_COFFEE:
  158. status["CompletedTake_CPU_CUP_COFFEE"] = true;
  159. break;
  160. case IC_CUP.CUP_ICECREAM:
  161. status["CompletedTake_CPU_CUP_ICECREAM"] = true;
  162. break;
  163. }
  164. break;
  165. case IC_CMD.OPEN_SE:
  166. switch ((IC_SE)data.Value)
  167. {
  168. case IC_SE.SE_1:
  169. status["CompletedOpen_SE_1"] = true;
  170. break;
  171. case IC_SE.SE_2:
  172. status["CompletedOpen_SE_2"] = true;
  173. break;
  174. case IC_SE.SE_3:
  175. status["CompletedOpen_SE_3"] = true;
  176. break;
  177. }
  178. break;
  179. case IC_CMD.CLOSE_SE:
  180. switch ((IC_SE)data.Value)
  181. {
  182. case IC_SE.SE_1:
  183. status["CompletedClose_SE_1"] = true;
  184. break;
  185. case IC_SE.SE_2:
  186. status["CompletedClose_SE_2"] = true;
  187. break;
  188. case IC_SE.SE_3:
  189. status["CompletedClose_SE_3"] = true;
  190. break;
  191. }
  192. break;
  193. }
  194. if(!OnLine) IsWork = false;
  195. else IsWork = true;
  196. }
  197. catch (Exception ex)
  198. {
  199. }
  200. }
  201. protected override void InitStatus()
  202. {
  203. status["CompletedTake_CPU_CUP_ICECREAM"] = false;
  204. status["CompletedTake_CPU_CUP_COFFEE"] = false;
  205. status["CompletedOpen_SE_1"] = false;
  206. status["CompletedOpen_SE_2"] = false;
  207. status["CompletedOpen_SE_3"] = false;
  208. status["CompletedClose_SE_1"] = false;
  209. status["CompletedClose_SE_2"] = false;
  210. status["CompletedClose_SE_3"] = false;
  211. }
  212. public override void Init()
  213. {
  214. commProxy = new SerialPortClient(PortName,(BaudRates)Enum.Parse(typeof(BaudRates),BaudRate));
  215. commProxy.SetDataStorage(dataStorage);
  216. //STM32F103RCT6单片机下杯
  217. EventBus.EventBus.GetInstance().Subscribe<SCChip_TakeCupEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  218. {
  219. try
  220. {
  221. switch ((@event as SCChip_TakeCupEvent).Cup)
  222. {
  223. case IC_CUP.CUP_ICECREAM:
  224. status["CompletedTake_CPU_CUP_ICECREAM"] = false;
  225. break;
  226. case IC_CUP.CUP_COFFEE:
  227. status["CompletedTake_CPU_CUP_COFFEE"] = false;
  228. break;
  229. }
  230. package.Cmd = IC_CMD.TAKE_CUP;
  231. package.Value = (byte)(@event as SCChip_TakeCupEvent).Cup;
  232. commProxy.SendData(StructureToByte(package));
  233. bool wait = true;
  234. var waitTimeout = DateTime.Now.AddSeconds(3);
  235. while (wait)
  236. {
  237. wait = DateTime.Now < waitTimeout;
  238. if (wait)
  239. {
  240. switch ((@event as SCChip_TakeCupEvent).Cup)
  241. {
  242. case IC_CUP.CUP_ICECREAM:
  243. wait = !(bool)status["CompletedTake_CPU_CUP_ICECREAM"];
  244. break;
  245. case IC_CUP.CUP_COFFEE:
  246. wait = !(bool)status["CompletedTake_CPU_CUP_COFFEE"];
  247. break;
  248. }
  249. }
  250. Thread.Sleep(10);
  251. }
  252. }
  253. catch (Exception ex)
  254. {
  255. MessageLog.GetInstance.Show($"BPASmartClient.SCChip 中引发错误,ICChipMachine 类,描述:[{ex.Message}]");
  256. }
  257. });
  258. //STM32F103RCT6单片机舵机打料
  259. EventBus.EventBus.GetInstance().Subscribe<SCChip_MakeIceCreamEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  260. {
  261. try
  262. {
  263. switch ((IC_SE)(@event as SCChip_MakeIceCreamEvent).SteeringEngine)
  264. {
  265. case IC_SE.SE_1:
  266. status["CompletedOpen_SE_1"] = false;
  267. break;
  268. case IC_SE.SE_2:
  269. status["CompletedOpen_SE_2"] = false;
  270. break;
  271. case IC_SE.SE_3:
  272. status["CompletedOpen_SE_3"] = false;
  273. break;
  274. }
  275. package.Cmd = IC_CMD.OPEN_SE;
  276. package.Value = (byte)(@event as SCChip_MakeIceCreamEvent).SteeringEngine;
  277. commProxy.SendData(StructureToByte(package));
  278. bool wait = true;
  279. DateTime waitTimeout = DateTime.Now.AddSeconds(3);
  280. while (wait)
  281. {
  282. wait = DateTime.Now < waitTimeout;
  283. if (wait)
  284. {
  285. switch ((IC_SE)(@event as SCChip_MakeIceCreamEvent).SteeringEngine)
  286. {
  287. case IC_SE.SE_1:
  288. wait = !(bool)status["CompletedOpen_SE_1"];
  289. break;
  290. case IC_SE.SE_2:
  291. wait = !(bool)status["CompletedOpen_SE_2"];
  292. break;
  293. case IC_SE.SE_3:
  294. wait = !(bool)status["CompletedOpen_SE_3"];
  295. break;
  296. }
  297. }
  298. Thread.Sleep(10);
  299. }
  300. Thread.Sleep(2000);
  301. package.Cmd = IC_CMD.CLOSE_SE;
  302. package.Value = (byte)(@event as SCChip_MakeIceCreamEvent).SteeringEngine;
  303. commProxy.SendData(StructureToByte(package));
  304. wait = true;
  305. waitTimeout = DateTime.Now.AddSeconds(3);
  306. while (wait)
  307. {
  308. wait = DateTime.Now < waitTimeout;
  309. if (wait)
  310. {
  311. switch ((IC_SE)(@event as SCChip_MakeIceCreamEvent).SteeringEngine)
  312. {
  313. case IC_SE.SE_1:
  314. wait = !(bool)status["CompletedClose_SE_1"];
  315. break;
  316. case IC_SE.SE_2:
  317. wait = !(bool)status["CompletedClose_SE_2"];
  318. break;
  319. case IC_SE.SE_3:
  320. wait = !(bool)status["CompletedClose_SE_3"];
  321. break;
  322. }
  323. }
  324. Thread.Sleep(10);
  325. }
  326. }
  327. catch (Exception ex)
  328. {
  329. MessageLog.GetInstance.Show($"BPASmartClient.SCChip 中引发错误,ICChipMachine 类,描述:[{ex.Message}]");
  330. MessageLog.GetInstance.ShowEx($"BPASmartClient.SCChip 中引发错误,ICChipMachine 类,描述:[{ex.Message}]");
  331. }
  332. });
  333. //STM32F103RCT6单片机舵机打开或者关闭
  334. EventBus.EventBus.GetInstance().Subscribe<SCChip_SESwitchCreamEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  335. {
  336. try
  337. {
  338. package.Cmd = (@event as SCChip_SESwitchCreamEvent).IsOpen ? IC_CMD.OPEN_SE : IC_CMD.CLOSE_SE;
  339. package.Value = (byte)(@event as SCChip_SESwitchCreamEvent).SteeringEngine;
  340. commProxy.SendData(StructureToByte(package));
  341. }
  342. catch (Exception ex)
  343. {
  344. MessageLog.GetInstance.Show($"BPASmartClient.SCChip 中引发错误,ICChipMachine 类,描述:[{ex.Message}]");
  345. MessageLog.GetInstance.ShowEx($"BPASmartClient.SCChip 中引发错误,ICChipMachine 类,描述:[{ex.Message}]");
  346. }
  347. });
  348. //STM32F103RCT6单片机控制冰淇淋机器转
  349. EventBus.EventBus.GetInstance().Subscribe<SCChip_RotorSwitchEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  350. {
  351. try
  352. {
  353. package.Cmd = IC_CMD.ROTOR;
  354. package.Value = (@event as SCChip_RotorSwitchEvent).TurnOn ? (byte)IC_ROTOR.OPEN_ROTOR : (byte)IC_ROTOR.CLOSE_ROTOR;
  355. commProxy.SendData(StructureToByte(package));
  356. }
  357. catch (Exception ex)
  358. {
  359. MessageLog.GetInstance.Show($"BPASmartClient.SCChip 中引发错误,ICChipMachine 类,描述:[{ex.Message}]");
  360. MessageLog.GetInstance.ShowEx($"BPASmartClient.SCChip 中引发错误,ICChipMachine 类,描述:[{ex.Message}]");
  361. }
  362. });
  363. }
  364. }
  365. }