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

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