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

ICChipMachine.cs 16 KiB

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