终端一体化运控平台
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.
 
 
 

906 lines
50 KiB

  1. using BPASmartClient.CustomResource.Pages.Model;
  2. using BPASmartClient.Helper;
  3. using BPASmartClient.JXJFoodSmallStation.Model.HK_PLC;
  4. using BPASmartClient.JXJFoodSmallStation.Model.Siemens;
  5. using BPASmartClient.Modbus;
  6. using System;
  7. using System.Collections.Concurrent;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Configuration;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace BPASmartClient.JXJFoodSmallStation.Model
  16. {
  17. public class ProcessControl
  18. {
  19. private volatile static ProcessControl _Instance;
  20. public static ProcessControl GetInstance => _Instance ?? (_Instance = new ProcessControl());
  21. private ProcessControl() { }
  22. public ObservableCollection<RemoteRecipeRawMaterial> RawMaterials { get; set; } = new ObservableCollection<RemoteRecipeRawMaterial>();
  23. /// <summary>
  24. /// 配方数据
  25. /// </summary>
  26. public ObservableCollection<RemoteRecipeData> RemoteRecipes = new ObservableCollection<RemoteRecipeData>();
  27. /// <summary>
  28. /// 原料的名称和料仓的位置对应
  29. /// </summary>
  30. public Dictionary<string, short> RawMaterialsNamePos = new Dictionary<string, short>();
  31. /// <summary>
  32. /// 配方队列
  33. /// </summary>
  34. public ConcurrentQueue<string> RecipeQueue = new ConcurrentQueue<string>();
  35. public ConcurrentQueue<string> RecipeQueueTray2 = new ConcurrentQueue<string>();
  36. public SiemensDeviceStatus SiemensDevice = new SiemensDeviceStatus();
  37. public HKDeviceStatus HKDevice = new HKDeviceStatus();
  38. GVL_SmallStation SmallStation = new GVL_SmallStation();
  39. XL_Finish_DB RecipeFinishInfo = new XL_Finish_DB();
  40. /// <summary>
  41. /// 接收原料数据
  42. /// </summary>
  43. public RecipeRawMaterial RawMaterial;
  44. public void Init()
  45. {
  46. for (int i = 0; i < 16; i++)
  47. {
  48. if (DeviceInquire.GetInstance.GetDevice(i).DeviceName != null)
  49. {
  50. if (!RawMaterialsNamePos.ContainsKey(DeviceInquire.GetInstance.GetDevice(i).DeviceName))
  51. {
  52. RawMaterialsNamePos.Add(DeviceInquire.GetInstance.GetDevice(i).DeviceName, (short)DeviceInquire.GetInstance.GetDevice(i).deviceStatus.DeviceNum);
  53. }
  54. }
  55. }
  56. ActionManage.GetInstance.CancelRegister("SiemensRecipeRecive");
  57. ActionManage.GetInstance.Register(new Action<object>((res) =>
  58. {
  59. if (SiemensDevice.IsConnected)
  60. {
  61. if (res != null && res is XL_Start_DB recipe)
  62. {
  63. RawMaterials.Clear();
  64. for (int i = 0; i < 15; i++)
  65. {
  66. if (RawMaterialsNamePos.ContainsKey(recipe.Material[i].Material_Name))
  67. {
  68. RawMaterials.Add(new RemoteRecipeRawMaterial()
  69. {
  70. RawMaterialName = recipe.Material[i].Material_Name,
  71. RawMaterialBarrelNum = recipe.Material[i].Material_BarrelNum,
  72. RawMaterialWeight = recipe.Material[i].Material_Weight,
  73. RawMaterialLocation = (int)RawMaterialsNamePos[recipe.Material[i].Material_Name]
  74. });
  75. }
  76. else
  77. {
  78. //报警,配方的原料名称下发和设备不一致
  79. }
  80. }
  81. Json<RemoteRecipeDataColl>.Data.Recipes.Add(new RemoteRecipeData()
  82. {
  83. RecipeName = recipe.RecipeName,
  84. RecipeCode = recipe.RecipeCode,
  85. RawMaterial = RawMaterials,
  86. TrayCode = recipe.TrayCode
  87. });
  88. }
  89. }
  90. }), "SiemensRecipeRecive", true);
  91. string HK_PLC_IP = ConfigurationManager.AppSettings["HKPlc_IP"];
  92. string Siemens_PLC_IP = ConfigurationManager.AppSettings["Siemens_IP"];
  93. try
  94. {
  95. HKDevice.HK_PLC_S7.Connect(S7.Net.CpuType.S71200, HK_PLC_IP);
  96. //SiemensDevice.Siemens_PLC_S7.Connect(S7.Net.CpuType.S71500, Siemens_PLC_IP);
  97. if (HKDevice.IsConnected)
  98. {
  99. HKDevice.Init();
  100. MessageLog.GetInstance.ShowUserLog("海科plc初始化完成");
  101. }
  102. if (SiemensDevice.IsConnected)
  103. {
  104. SiemensDevice.Init();
  105. MessageLog.GetInstance.ShowUserLog("");
  106. }
  107. }
  108. catch(Exception ex)
  109. {
  110. }
  111. RecipeQueue.Clear();
  112. //Json<RemoteRecipeDataColl>.Data.Recipes = TestData.GetInstance.Recipes;//添加测试数据
  113. ThreadManage.GetInstance().StartLong(new Action(() =>
  114. {
  115. ReceviceData();
  116. RecipeInfoToHKPLC();
  117. Thread.Sleep(10);
  118. }), "流程处理", true);
  119. ThreadManage.GetInstance().StartLong(new Action(() =>
  120. {
  121. if (SiemensDevice.IsConnected && HKDevice.IsConnected)
  122. {
  123. /*AgvGetInDelivery();
  124. AgvGetInPickUp();*/
  125. }
  126. Thread.Sleep(10);
  127. }), "AGV进站送取货", true);
  128. ThreadManage.GetInstance().StartLong(new Action(() =>
  129. {
  130. if (HKDevice.IsConnected)
  131. {
  132. GetStatus();
  133. ManualOpen();
  134. ManualClose();
  135. }
  136. Thread.Sleep(10);
  137. }), "手动操作", true);
  138. }
  139. private void GetStatus()
  140. {
  141. for (int i = 0; i < 8; i++)
  142. {
  143. HKDevice.HK_PLC_S7.Read<bool>("DB5.DBX0." + i);
  144. }
  145. for (int i = 0; i < 8; i++)
  146. {
  147. HKDevice.HK_PLC_S7.Read<bool>("DB5.DBX1." + i);
  148. }
  149. }
  150. private void ManualOpen()
  151. {
  152. ActionManage.GetInstance.Register(new Action<object>((o) =>
  153. {
  154. if (o != null)
  155. {
  156. if (o.ToString().Contains("升降气缸"))
  157. {
  158. int index = Convert.ToInt16(o.ToString().Substring(o.ToString().Length - 1));
  159. switch (index)
  160. {
  161. case 1:
  162. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX0.0", true);
  163. break;
  164. case 2:
  165. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX0.1", true);
  166. break;
  167. case 3:
  168. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX0.2", true);
  169. break;
  170. case 4:
  171. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX0.3", true);
  172. break;
  173. case 5:
  174. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX0.4", true);
  175. break;
  176. case 6:
  177. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX0.5", true);
  178. break;
  179. case 7:
  180. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX0.6", true);
  181. break;
  182. case 8:
  183. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX0.7", true);
  184. break;
  185. case 9:
  186. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX1.0", true);
  187. break;
  188. case 10:
  189. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX1.1", true);
  190. break;
  191. case 11:
  192. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX1.2", true);
  193. break;
  194. case 12:
  195. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX1.3", true);
  196. break;
  197. case 13:
  198. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX1.4", true);
  199. break;
  200. case 14:
  201. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX1.5", true);
  202. break;
  203. case 15:
  204. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX1.6", true);
  205. break;
  206. default:
  207. break;
  208. }
  209. }
  210. else if (o.ToString().Contains("阻挡气缸"))
  211. {
  212. int index = Convert.ToInt16(o.ToString().Substring(o.ToString().Length - 1));
  213. switch (index)
  214. {
  215. case 1:
  216. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX1.7", true);
  217. break;
  218. case 2:
  219. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX2.0", true);
  220. break;
  221. case 3:
  222. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX2.1", true);
  223. break;
  224. case 4:
  225. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX2.2", true);
  226. break;
  227. case 5:
  228. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX2.3", true);
  229. break;
  230. case 6:
  231. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX2.4", true);
  232. break;
  233. case 7:
  234. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX2.5", true);
  235. break;
  236. case 8:
  237. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX2.6", true);
  238. break;
  239. case 9:
  240. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX2.7", true);
  241. break;
  242. case 10:
  243. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX3.0", true);
  244. break;
  245. case 11:
  246. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX3.1", true);
  247. break;
  248. case 12:
  249. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX3.2", true);
  250. break;
  251. case 13:
  252. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX3.3", true);
  253. break;
  254. case 14:
  255. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX3.4", true);
  256. break;
  257. case 15:
  258. HKDevice.HK_PLC_S7.Write<bool>("DB5.DBX3.5", true);
  259. break;
  260. default:
  261. break;
  262. }
  263. }
  264. else if (o.ToString().Contains("进料桶顶升气缸"))
  265. {
  266. HKDevice.HK_PLC_S7.Write("DB5.DBX3.6", true);
  267. }
  268. else if (o.ToString().Contains("出料桶顶升气缸1"))
  269. {
  270. HKDevice.HK_PLC_S7.Write("DB5.DBX3.7", true);
  271. }
  272. else if (o.ToString().Contains("出料桶顶升气缸2"))
  273. {
  274. HKDevice.HK_PLC_S7.Write("DB5.DBX4.0", true);
  275. }
  276. else if (o.ToString().Contains("出料桶顶升气缸3"))
  277. {
  278. HKDevice.HK_PLC_S7.Write("DB5.DBX4.1", true);
  279. }
  280. else if (o.ToString().Contains("托盘气缸1_1"))
  281. {
  282. HKDevice.HK_PLC_S7.Write("DB5.DBX4.2", true);
  283. }
  284. else if (o.ToString().Contains("托盘气缸1_2"))
  285. {
  286. HKDevice.HK_PLC_S7.Write("DB5.DBX4.3", true);
  287. }
  288. else if (o.ToString().Contains("托盘气缸2_1"))
  289. {
  290. HKDevice.HK_PLC_S7.Write("DB5.DBX4.4", true);
  291. }
  292. else if (o.ToString().Contains("托盘气缸2_2"))
  293. {
  294. HKDevice.HK_PLC_S7.Write("DB5.DBX4.5", true);
  295. }
  296. }
  297. }), "ManualOpen", true);//根据下发的配方ID将 托盘的位置信息添加到配方中
  298. }
  299. private void ManualClose()
  300. {
  301. ActionManage.GetInstance.Register(new Action<object>((o) =>
  302. {
  303. if (o != null)
  304. {
  305. if (o.ToString().Contains("升降气缸"))
  306. {
  307. int index = Convert.ToInt16(o.ToString().Substring(o.ToString().Length - 1));
  308. switch (index)
  309. {
  310. case 1:
  311. HKDevice.HK_PLC_S7.Write("DB5.DBX0.0", false);
  312. break;
  313. case 2:
  314. HKDevice.HK_PLC_S7.Write("DB5.DBX0.1", false);
  315. break;
  316. case 3:
  317. HKDevice.HK_PLC_S7.Write("DB5.DBX0.2", false);
  318. break;
  319. case 4:
  320. HKDevice.HK_PLC_S7.Write("DB5.DBX0.3", false);
  321. break;
  322. case 5:
  323. HKDevice.HK_PLC_S7.Write("DB5.DBX0.4", false);
  324. break;
  325. case 6:
  326. HKDevice.HK_PLC_S7.Write("DB5.DBX0.5", false);
  327. break;
  328. case 7:
  329. HKDevice.HK_PLC_S7.Write("DB5.DBX0.6", false);
  330. break;
  331. case 8:
  332. HKDevice.HK_PLC_S7.Write("DB5.DBX0.7", false);
  333. break;
  334. case 9:
  335. HKDevice.HK_PLC_S7.Write("DB5.DBX1.0", false);
  336. break;
  337. case 10:
  338. HKDevice.HK_PLC_S7.Write("DB5.DBX1.1", false);
  339. break;
  340. case 11:
  341. HKDevice.HK_PLC_S7.Write("DB5.DBX1.2", false);
  342. break;
  343. case 12:
  344. HKDevice.HK_PLC_S7.Write("DB5.DBX1.3", false);
  345. break;
  346. case 13:
  347. HKDevice.HK_PLC_S7.Write("DB5.DBX1.4", false);
  348. break;
  349. case 14:
  350. HKDevice.HK_PLC_S7.Write("DB5.DBX1.5", false);
  351. break;
  352. case 15:
  353. HKDevice.HK_PLC_S7.Write("DB5.DBX1.6", false);
  354. break;
  355. default:
  356. break;
  357. }
  358. }
  359. else if (o.ToString().Contains("阻挡气缸"))
  360. {
  361. int index = Convert.ToInt16(o.ToString().Substring(o.ToString().Length - 1));
  362. switch (index)
  363. {
  364. case 1:
  365. HKDevice.HK_PLC_S7.Write("DB5.DBX1.7", false);
  366. break;
  367. case 2:
  368. HKDevice.HK_PLC_S7.Write("DB5.DBX2.0", false);
  369. break;
  370. case 3:
  371. HKDevice.HK_PLC_S7.Write("DB5.DBX2.1", false);
  372. break;
  373. case 4:
  374. HKDevice.HK_PLC_S7.Write("DB5.DBX2.2", false);
  375. break;
  376. case 5:
  377. HKDevice.HK_PLC_S7.Write("DB5.DBX2.3", false);
  378. break;
  379. case 6:
  380. HKDevice.HK_PLC_S7.Write("DB5.DBX2.4", false);
  381. break;
  382. case 7:
  383. HKDevice.HK_PLC_S7.Write("DB5.DBX2.5", false);
  384. break;
  385. case 8:
  386. HKDevice.HK_PLC_S7.Write("DB5.DBX2.6", false);
  387. break;
  388. case 9:
  389. HKDevice.HK_PLC_S7.Write("DB5.DBX2.7", false);
  390. break;
  391. case 10:
  392. HKDevice.HK_PLC_S7.Write("DB5.DBX3.0", false);
  393. break;
  394. case 11:
  395. HKDevice.HK_PLC_S7.Write("DB5.DBX3.1", false);
  396. break;
  397. case 12:
  398. HKDevice.HK_PLC_S7.Write("DB5.DBX3.2", false);
  399. break;
  400. case 13:
  401. HKDevice.HK_PLC_S7.Write("DB5.DBX3.3", false);
  402. break;
  403. case 14:
  404. HKDevice.HK_PLC_S7.Write("DB5.DBX3.4", false);
  405. break;
  406. case 15:
  407. HKDevice.HK_PLC_S7.Write("DB5.DBX3.5", false);
  408. break;
  409. default:
  410. break;
  411. }
  412. }
  413. else if (o.ToString().Contains("进料桶顶升气缸"))
  414. {
  415. HKDevice.HK_PLC_S7.Write("DB5.DBX3.6", false);
  416. }
  417. else if (o.ToString().Contains("出料桶顶升气缸1"))
  418. {
  419. HKDevice.HK_PLC_S7.Write("DB5.DBX3.7", false);
  420. }
  421. else if (o.ToString().Contains("出料桶顶升气缸2"))
  422. {
  423. HKDevice.HK_PLC_S7.Write("DB5.DBX4.0", false);
  424. }
  425. else if (o.ToString().Contains("出料桶顶升气缸3"))
  426. {
  427. HKDevice.HK_PLC_S7.Write("DB5.DBX4.1", false);
  428. }
  429. else if (o.ToString().Contains("托盘气缸1_1"))
  430. {
  431. HKDevice.HK_PLC_S7.Write("DB5.DBX4.2", false);
  432. }
  433. else if (o.ToString().Contains("托盘气缸1_2"))
  434. {
  435. HKDevice.HK_PLC_S7.Write("DB5.DBX4.3", false);
  436. }
  437. else if (o.ToString().Contains("托盘气缸2_1"))
  438. {
  439. HKDevice.HK_PLC_S7.Write("DB5.DBX4.4", false);
  440. }
  441. else if (o.ToString().Contains("托盘气缸2_2"))
  442. {
  443. HKDevice.HK_PLC_S7.Write("DB5.DBX4.5", false);
  444. }
  445. }
  446. }), "ManualClose", true);//根据下发的配方ID将 托盘的位置信息添加到配方中
  447. }
  448. /// <summary>
  449. /// AGV进站送货
  450. /// </summary>
  451. private void AgvGetInDelivery()
  452. {
  453. switch (SmallStation.AgvDeliveryPosition)
  454. {
  455. case 0:
  456. if (RTrig.GetInstance("").Start(SiemensDevice.Siemens_PLC_S7.Read<object>(SiemensCommAddress.DeliveryAGVApply) is bool))
  457. {
  458. SmallStation.AgvDeliveryPosition = 1;
  459. HKDevice.HK_PLC_S7.Write(HKPlcCommAddress.DeliveryAGVApply, true);
  460. }
  461. break;
  462. case 1:
  463. if (RTrig.GetInstance("").Start(HKDevice.HK_PLC_S7.Read<bool>(HKPlcCommAddress.DeliveryAGVIsApply)))
  464. {
  465. SmallStation.AgvDeliveryPosition = 2;
  466. SiemensDevice.Siemens_PLC_S7.Write(SiemensCommAddress.DeliveryAGVIsApply, true);
  467. }
  468. break;
  469. case 2:
  470. if (RTrig.GetInstance("").Start(SiemensDevice.Siemens_PLC_S7.Read<object>(SiemensCommAddress.DeliveryAGVApplyJack) is bool))
  471. {
  472. SmallStation.AgvDeliveryPosition = 3;
  473. HKDevice.HK_PLC_S7.Write(HKPlcCommAddress.DeliveryAGVApplyJack, true);
  474. }
  475. break;
  476. case 3:
  477. if (RTrig.GetInstance("").Start(HKDevice.HK_PLC_S7.Read<bool>(HKPlcCommAddress.DeliveryAGVIsApplyJack)))
  478. {
  479. SmallStation.AgvDeliveryPosition = 4;
  480. SiemensDevice.Siemens_PLC_S7.Write(SiemensCommAddress.DeliveryAGVIsApplyJack, true);
  481. }
  482. break;
  483. case 4:
  484. if (RTrig.GetInstance("").Start(SiemensDevice.Siemens_PLC_S7.Read<object>(SiemensCommAddress.DeliveryAGVFinsih) is bool))
  485. {
  486. SmallStation.AgvDeliveryPosition = 5;
  487. HKDevice.HK_PLC_S7.Write(HKPlcCommAddress.DeliveryAGVFinsih, true);
  488. }
  489. break;
  490. case 5:
  491. if (RTrig.GetInstance("").Start(HKDevice.HK_PLC_S7.Read<bool>(HKPlcCommAddress.StationHaveCargo)))
  492. {
  493. SmallStation.AgvDeliveryPosition = 0;
  494. SiemensDevice.Siemens_PLC_S7.Write(SiemensCommAddress.StationHaveCargo, true);
  495. }
  496. break;
  497. default:
  498. break;
  499. }
  500. //获取工位上是否有小车
  501. SiemensDevice.Siemens_PLC_S7.Write(SiemensCommAddress.StationIsExistCar, (bool)
  502. HKDevice.HK_PLC_S7.Read<bool>(HKPlcCommAddress.StationIsExistTray));
  503. }
  504. /// <summary>
  505. /// AGV进站取货
  506. /// </summary>
  507. private void AgvGetInPickUp()
  508. {
  509. switch (SmallStation.AgvPickUpPosition)
  510. {
  511. case 0:
  512. if (RTrig.GetInstance("").Start(SiemensDevice.Siemens_PLC_S7.Read<bool>(SiemensCommAddress.PickAGVApply)))
  513. {
  514. SmallStation.AgvPickUpPosition = 1;
  515. HKDevice.HK_PLC_S7.Write(HKPlcCommAddress.PickAGVApply, true);
  516. }
  517. break;
  518. case 1:
  519. if (RTrig.GetInstance("").Start(HKDevice.HK_PLC_S7.Read<bool>(HKPlcCommAddress.PickAGVIsApply)))
  520. {
  521. SmallStation.AgvPickUpPosition = 2;
  522. SiemensDevice.Siemens_PLC_S7.Write(SiemensCommAddress.PickAGVIsApply, true);
  523. }
  524. break;
  525. case 2:
  526. if (RTrig.GetInstance("").Start(SiemensDevice.Siemens_PLC_S7.Read<object>(SiemensCommAddress.PickCargoAGVFinish) is bool))
  527. {
  528. SmallStation.AgvPickUpPosition = 3;
  529. HKDevice.HK_PLC_S7.Write(HKPlcCommAddress.PickCargoAGVFinish, true);
  530. }
  531. break;
  532. case 3:
  533. if (RTrig.GetInstance("").Start(HKDevice.HK_PLC_S7.Read<bool>(HKPlcCommAddress.PickAGVFinish)))
  534. {
  535. SmallStation.AgvPickUpPosition = 0;
  536. SiemensDevice.Siemens_PLC_S7.Write(SiemensCommAddress.PickAGVFinish, true);
  537. }
  538. break;
  539. default:
  540. break;
  541. }
  542. }
  543. /// <summary>
  544. /// 将配方添加到配方队列中
  545. /// </summary>
  546. private void ReceviceData()
  547. {
  548. RemoteRecipes = Json<RemoteRecipeDataColl>.Data.Recipes;
  549. if (RemoteRecipes.Count > 0)
  550. {
  551. foreach (var data in RemoteRecipes)
  552. {
  553. if (data.TrayCode == 1)
  554. {
  555. if (!(RecipeQueue.Contains(data.RecipeCode)))
  556. {
  557. RecipeQueue.Enqueue(data.RecipeCode);
  558. }
  559. }
  560. else if(data.TrayCode == 2)
  561. {
  562. if (!(RecipeQueueTray2.Contains(data.RecipeCode)))
  563. {
  564. RecipeQueueTray2.Enqueue(data.RecipeCode);
  565. }
  566. }
  567. }
  568. }
  569. else
  570. {
  571. RecipeQueue.Clear();
  572. RecipeQueueTray2.Clear();
  573. SmallStation.RecipeStatusID = 0;
  574. SmallStation.RecipeStatusIDTray2 = 0;
  575. }
  576. }
  577. /// <summary>
  578. /// 执行配方队列中的第一个配方
  579. /// </summary>
  580. private void RecipeInfoToHKPLC()
  581. {
  582. if (RecipeQueue.Count > 0)
  583. {
  584. int index = Array.FindIndex(RemoteRecipes.ToArray(), p => p.RecipeCode == RecipeQueue.ElementAt(0));
  585. if (index >= 0 && index < RemoteRecipes.Count)
  586. {
  587. string code = RemoteRecipes.ElementAt(index).RecipeCode;
  588. int trayCode = RemoteRecipes.ElementAt(index).TrayCode;
  589. string recipeName = RemoteRecipes.ElementAt(index).RecipeName;
  590. if (trayCode == 1)
  591. {
  592. if (SmallStation.RecipeStatusID == 0)
  593. {
  594. foreach (var item in RemoteRecipes.ElementAt(index).RawMaterial)
  595. {
  596. HKDevice.StockBinPar((uint)item.RawMaterialBarrelNum, (ushort)item.RawMaterialLocation);
  597. }
  598. HKDevice.HK_PLC_S7.Write("DB4.DBX1.3", true);
  599. SmallStation.RecipeStatusID = 1;
  600. MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},下发完成");
  601. HKDevice.HK_PLC_S7.Write("DB3.DBX1.3", true);
  602. }
  603. if (HKDevice.HK_PLC_S7.Read<bool>("DB3.DBX1.3") && SmallStation.RecipeStatusID == 1)
  604. {
  605. HKDevice.HK_PLC_S7.Write("DB3.DBX1.3", false);
  606. SmallStation.RecipeStatusID = 2;
  607. MessageLog.GetInstance.ShowRunLog($"托盘1,{ recipeName}plc端 配方接收完成");
  608. }
  609. if (SmallStation.RecipeStatusID == 2)
  610. {
  611. if (RTrig.GetInstance("DB3.DBX50.0").Start(HKDevice.HK_PLC_S7.Read<bool>("DB3.DBX50.0")))
  612. {
  613. var res = HKDevice.HK_PLC_S7.Read<float>("DB3.DBD10");
  614. MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},1号桶,{res}料仓,允许配料");
  615. if (res > 0 && res is float loc)
  616. {
  617. //int decimalNum = Convert.ToInt32(loc.ToString().Substring(loc.ToString().IndexOf(".") + 1));
  618. int loc_index = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == loc);
  619. double weight = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(loc_index).RawMaterialWeight;
  620. if (loc_index >= 0)
  621. {
  622. DeviceInquire.GetInstance.GetDevice((int)loc)?.Start((uint)weight);//根据料仓编号 启动并写入每个原料重量
  623. SmallStation.StockInIsWork = (int)loc;
  624. //HKDevice.HK_PLC_S7.Write("DB3.DBX50.0", false);
  625. }
  626. SmallStation.DosingTray1 = true;
  627. MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},1号桶,{(int)loc}号仓,配料完成");
  628. }
  629. }
  630. else if(RTrig.GetInstance("DB3.DBX50.1").Start(HKDevice.HK_PLC_S7.Read<bool>("DB3.DBX50.1")))
  631. {
  632. var res = HKDevice.HK_PLC_S7.Read<float>("DB3.DBD14");
  633. MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},2号桶,{res}料仓,允许配料");
  634. if (res > 0 && res is float loc)
  635. {
  636. int loc_index = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == loc);
  637. double weight = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(loc_index).RawMaterialWeight;
  638. if (loc_index >= 0)
  639. {
  640. DeviceInquire.GetInstance.GetDevice((int)loc)?.Start((uint)weight);//启动并写入每个原料重量
  641. SmallStation.StockInIsWork = (int)loc;
  642. //HKDevice.HK_PLC_S7.Write("DB3.DBX50.1", false);
  643. }
  644. SmallStation.DosingTray1 = true;
  645. MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},2号桶,{(int)loc}号仓,配料完成");
  646. }
  647. }
  648. else if (RTrig.GetInstance("DB3.DBX50.2").Start(HKDevice.HK_PLC_S7.Read<bool>("DB3.DBX50.2")))
  649. {
  650. var res = HKDevice.HK_PLC_S7.Read<float>("DB3.DBD18");
  651. MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},3号桶,{res}料仓,允许配料");
  652. if (res > 0 && res is float loc)
  653. {
  654. int loc_index = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == loc);
  655. double weight = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(loc_index).RawMaterialWeight;
  656. if (loc_index >= 0)
  657. {
  658. DeviceInquire.GetInstance.GetDevice((int)loc)?.Start((uint)weight);//启动并写入每个原料重量
  659. SmallStation.StockInIsWork = (int)loc;
  660. //HKDevice.HK_PLC_S7.Write("DB3.DBX50.2", false);
  661. }
  662. SmallStation.DosingTray1 = true;
  663. MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},3号桶,{(int)loc}号仓,配料完成");
  664. }
  665. }
  666. else if (RTrig.GetInstance("DB3.DBX50.3").Start(HKDevice.HK_PLC_S7.Read<bool>("DB3.DBX50.3")))
  667. {
  668. var res = HKDevice.HK_PLC_S7.Read<float>("DB3.DBD22");
  669. MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},4号桶,{res}料仓,允许配料");
  670. if (res > 0 && res is float loc)
  671. {
  672. int loc_index = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == loc);
  673. double weight = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(loc_index).RawMaterialWeight;
  674. if (loc_index >= 0)
  675. {
  676. DeviceInquire.GetInstance.GetDevice((int)loc)?.Start((uint)weight);//启动并写入每个原料重量
  677. SmallStation.StockInIsWork = (int)loc;
  678. //HKDevice.HK_PLC_S7.Write("DB3.DBX50.3", false);
  679. }
  680. SmallStation.DosingTray1 = true;
  681. MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},4号桶,{(int)loc}号仓,配料完成");
  682. }
  683. }
  684. if (SmallStation.DosingTray1) {
  685. for (int i = 1; i < 16; i++)
  686. {
  687. if (RTrig.GetInstance("柔性味魔方配料完成").Start(DeviceInquire.GetInstance.GetDevice(i).deviceStatus.RunStatus == 3))
  688. {
  689. MessageLog.GetInstance.ShowRunLog($"柔性味魔方,托盘1,配方:{recipeName},{i}号仓,配料完成");
  690. int res = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == i);
  691. RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(res).Laying_Off_Weight = DeviceInquire.GetInstance.GetDevice(i).deviceStatus.CutWeightFeedback;
  692. DeviceInquire.GetInstance.GetDevice(i).StatusReset();
  693. if (i >= 1 && i <= 8)
  694. {
  695. HKDevice.HK_PLC_S7.Write("DB4.DBX30." + (i - 1), true);
  696. }
  697. else if (i >= 9 && i <= 15)
  698. {
  699. HKDevice.HK_PLC_S7.Write("DB4.DBX31." + (i - 9), true);
  700. }
  701. SmallStation.DosingTray1 = false;
  702. }
  703. }
  704. }
  705. if (RTrig.GetInstance("配方配料完成").Start(HKDevice.HK_PLC_S7.Read<bool>("DB3.DBX1.1")))
  706. {
  707. var res = Json<RemoteRecipeDataColl>.Data.Recipes.FirstOrDefault(p => p.RecipeCode == code);
  708. MessageLog.GetInstance.ShowRunLog($"托盘1,配方{res.RecipeName},配料完成");
  709. RecipeFinishInfo.Order_No = RemoteRecipes.ElementAt(index).RecipeCode;
  710. RecipeFinishInfo.Product_Code = RemoteRecipes.ElementAt(index).RecipeName;
  711. for (int i = 0; i < RemoteRecipes.ElementAt(index).RawMaterial.Count; i++)
  712. {
  713. RecipeFinishInfo.Material[i] = new UDT1();
  714. RecipeFinishInfo.Material[i].Material_Name = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(i).RawMaterialName;
  715. RecipeFinishInfo.Material[i].Material_BarrelNum = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(i).RawMaterialBarrelNum;
  716. RecipeFinishInfo.Material[i].Material_Laying_Off_Weight = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(i).Laying_Off_Weight;
  717. }
  718. if (SiemensDevice.IsConnected)
  719. {
  720. SiemensDevice.Siemens_PLC_S7.WriteClass<XL_Finish_DB>(RecipeFinishInfo, 3);
  721. }
  722. RecipeFinishInfo.Ask_For_Finish = true;
  723. App.Current.Dispatcher.Invoke(() =>
  724. {
  725. Json<RemoteRecipeDataColl>.Data.Recipes.Remove(res);
  726. });
  727. RecipeQueue.TryDequeue(out code);
  728. HKDevice.HK_PLC_S7.Write("DB3.DBX1.1", false);
  729. SmallStation.RecipeStatusID = 0;
  730. }
  731. }
  732. }
  733. }
  734. }
  735. if (RecipeQueueTray2.Count > 0)
  736. {
  737. int index = Array.FindIndex(RemoteRecipes.ToArray(), p => p.RecipeCode == RecipeQueueTray2.ElementAt(0));
  738. if (index >= 0 && index < RemoteRecipes.Count)
  739. {
  740. string code = RemoteRecipes.ElementAt(index).RecipeCode;
  741. int trayCode = RemoteRecipes.ElementAt(index).TrayCode;
  742. string recipeName = RemoteRecipes.ElementAt(index).RecipeName;
  743. if (trayCode == 2)
  744. {
  745. if (SmallStation.RecipeStatusIDTray2 == 0)
  746. {
  747. MessageLog.GetInstance.ShowRunLog($"托盘2,配方:{recipeName},初始化");
  748. foreach (var item in RemoteRecipes.ElementAt(index).RawMaterial)
  749. {
  750. HKDevice.StockBinPar((uint)item.RawMaterialBarrelNum, (ushort)item.RawMaterialLocation, 2);
  751. }
  752. HKDevice.HK_PLC_S7.Write("DB4.DBX1.4", true);
  753. SmallStation.RecipeStatusIDTray2 = 1;
  754. MessageLog.GetInstance.ShowRunLog($"托盘2,配方:{recipeName},下发完成");
  755. HKDevice.HK_PLC_S7.Write("DB3.DBX1.4", true);
  756. }
  757. if (HKDevice.HK_PLC_S7.Read<bool>("DB3.DBX1.4") && SmallStation.RecipeStatusIDTray2 == 1)
  758. {
  759. HKDevice.HK_PLC_S7.Write("DB3.DBX1.4", false);
  760. SmallStation.RecipeStatusIDTray2 = 2;
  761. MessageLog.GetInstance.ShowRunLog($"配方:{recipeName},plc端 配方接收完成");
  762. }
  763. if (SmallStation.RecipeStatusIDTray2 == 2)
  764. {
  765. if (RTrig.GetInstance("DB3.DBX50.4").Start(HKDevice.HK_PLC_S7.Read<bool>("DB3.DBX50.4")))
  766. {
  767. var res = HKDevice.HK_PLC_S7.Read<float>("DB3.DBD26");
  768. MessageLog.GetInstance.ShowRunLog($"托盘2,配方:{recipeName},1号桶,{res}料仓,允许配料");
  769. if (res > 0 && res is float loc)
  770. {
  771. //int decimalNum = Convert.ToInt32(loc.ToString().Substring(loc.ToString().IndexOf(".") + 1));
  772. int loc_index = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == loc);
  773. double weight = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(loc_index).RawMaterialWeight;
  774. if (loc_index >= 0)
  775. {
  776. DeviceInquire.GetInstance.GetDevice((int)loc)?.Start((uint)weight);//根据料仓编号 启动并写入每个原料重量
  777. SmallStation.StockInIsWork = (int)loc;
  778. //HKDevice.HK_PLC_S7.Write("DB3.DBX50.0", false);
  779. }
  780. SmallStation.DosingTray2 = true;
  781. MessageLog.GetInstance.ShowRunLog($"托盘2,配方:{recipeName},1号桶,{(int)loc}号仓,配料完成");
  782. }
  783. }
  784. else if (RTrig.GetInstance("DB3.DBX50.5").Start(HKDevice.HK_PLC_S7.Read<bool>("DB3.DBX50.5")))
  785. {
  786. var res = HKDevice.HK_PLC_S7.Read<float>("DB3.DBD30");
  787. MessageLog.GetInstance.ShowRunLog($"托盘2,配方:{recipeName},2号桶,{res}料仓,允许配料");
  788. if (res > 0 && res is float loc)
  789. {
  790. int loc_index = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == loc);
  791. double weight = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(loc_index).RawMaterialWeight;
  792. if (loc_index >= 0)
  793. {
  794. DeviceInquire.GetInstance.GetDevice((int)loc)?.Start((uint)weight);//启动并写入每个原料重量
  795. SmallStation.StockInIsWork = (int)loc;
  796. //HKDevice.HK_PLC_S7.Write("DB3.DBX50.5", false);
  797. }
  798. SmallStation.DosingTray2 = true;
  799. MessageLog.GetInstance.ShowRunLog($"托盘2,配方:{recipeName},2号桶,{(int)loc}号仓,配料完成");
  800. }
  801. }
  802. else if (RTrig.GetInstance("DB3.DBX50.6").Start(HKDevice.HK_PLC_S7.Read<bool>("DB3.DBX50.6")))
  803. {
  804. var res = HKDevice.HK_PLC_S7.Read<float>("DB3.DBD34");
  805. MessageLog.GetInstance.ShowRunLog($"托盘2,配方:{recipeName},3号桶,{res}料仓,允许配料");
  806. if (res > 0 && res is float loc)
  807. {
  808. int loc_index = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == loc);
  809. double weight = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(loc_index).RawMaterialWeight;
  810. if (loc_index >= 0)
  811. {
  812. DeviceInquire.GetInstance.GetDevice((int)loc)?.Start((uint)weight);//启动并写入每个原料重量
  813. SmallStation.StockInIsWork = (int)loc;
  814. //HKDevice.HK_PLC_S7.Write("DB3.DBX50.6", false);
  815. }
  816. SmallStation.DosingTray2 = true;
  817. MessageLog.GetInstance.ShowRunLog($"托盘2,配方:{recipeName},3号桶,{(int)loc}号仓,配料完成");
  818. }
  819. }
  820. else if (RTrig.GetInstance("DB3.DBX50.7").Start(HKDevice.HK_PLC_S7.Read<bool>("DB3.DBX50.7")))
  821. {
  822. var res = HKDevice.HK_PLC_S7.Read<float>("DB3.DBD38");
  823. MessageLog.GetInstance.ShowRunLog($"托盘2,配方:{recipeName},4号桶,{res}料仓,允许配料");
  824. if (res > 0 && res is float loc)
  825. {
  826. int loc_index = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == loc);
  827. double weight = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(loc_index).RawMaterialWeight;
  828. if (loc_index >= 0)
  829. {
  830. DeviceInquire.GetInstance.GetDevice((int)loc)?.Start((uint)weight);//启动并写入每个原料重量
  831. SmallStation.StockInIsWork = (int)loc;
  832. //HKDevice.HK_PLC_S7.Write("DB3.DBX50.7", false);
  833. }
  834. SmallStation.DosingTray2 = true;
  835. MessageLog.GetInstance.ShowRunLog($"托盘2,配方:{recipeName},4号桶,{(int)loc}号仓,配料完成");
  836. }
  837. }
  838. if (SmallStation.DosingTray2 == true)
  839. {
  840. for (int i = 1; i < 16; i++)
  841. {
  842. if (RTrig.GetInstance("柔性味魔方配料完成").Start(DeviceInquire.GetInstance.GetDevice(i).deviceStatus.RunStatus == 3))
  843. {
  844. MessageLog.GetInstance.ShowRunLog($"柔性味魔方,托盘2,配方:{recipeName},{i}号仓,配料完成");
  845. int res = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == i);
  846. RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(res).Laying_Off_Weight = DeviceInquire.GetInstance.GetDevice(i).deviceStatus.CutWeightFeedback;
  847. DeviceInquire.GetInstance.GetDevice(i).StatusReset();
  848. if (i >= 1 && i <= 8)
  849. {
  850. HKDevice.HK_PLC_S7.Write("DB4.DBX30." + (i - 1), true);
  851. }
  852. else if (i >= 9 && i <= 15)
  853. {
  854. HKDevice.HK_PLC_S7.Write("DB4.DBX31." + (i - 9), true);
  855. }
  856. SmallStation.DosingTray2 = false;
  857. }
  858. }
  859. }
  860. if (RTrig.GetInstance("DB3.DBX1.2").Start(HKDevice.HK_PLC_S7.Read<bool>("DB3.DBX1.2")))
  861. {
  862. var res = Json<RemoteRecipeDataColl>.Data.Recipes.FirstOrDefault(p => p.RecipeCode == code);
  863. MessageLog.GetInstance.ShowRunLog($"托盘2配方{res.RecipeName}配料完成");
  864. RecipeFinishInfo.Order_No = RemoteRecipes.ElementAt(index).RecipeCode;
  865. RecipeFinishInfo.Product_Code = RemoteRecipes.ElementAt(index).RecipeName;
  866. for (int i = 0; i < RemoteRecipes.ElementAt(index).RawMaterial.Count; i++)
  867. {
  868. RecipeFinishInfo.Material[i] = new UDT1();
  869. RecipeFinishInfo.Material[i].Material_Name = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(i).RawMaterialName;
  870. RecipeFinishInfo.Material[i].Material_BarrelNum = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(i).RawMaterialBarrelNum;
  871. RecipeFinishInfo.Material[i].Material_Laying_Off_Weight = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(i).Laying_Off_Weight;
  872. }
  873. RecipeFinishInfo.Ask_For_Finish = true;
  874. if (SiemensDevice.IsConnected)
  875. {
  876. SiemensDevice.Siemens_PLC_S7.WriteClass<XL_Finish_DB>(RecipeFinishInfo, 3);
  877. }
  878. App.Current.Dispatcher.Invoke(() =>
  879. {
  880. Json<RemoteRecipeDataColl>.Data.Recipes.Remove(res);
  881. });
  882. RecipeQueueTray2.TryDequeue(out code);
  883. HKDevice.HK_PLC_S7.Write("DB3.DBX1.2", false);
  884. SmallStation.RecipeStatusIDTray2 = 0;
  885. }
  886. }
  887. }
  888. }
  889. }
  890. }
  891. }
  892. }