终端一体化运控平台
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

271 líneas
13 KiB

  1. using BPASmartClient.Helper;
  2. using BPASmartClient.JXJFoodSmallStation.Model.HK_PLC;
  3. using BPASmartClient.JXJFoodSmallStation.Model.Siemens;
  4. using BPASmartClient.Modbus;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Configuration;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. namespace BPASmartClient.JXJFoodSmallStation.Model
  15. {
  16. public class ProcessControl
  17. {
  18. private volatile static ProcessControl _Instance;
  19. public static ProcessControl GetInstance => _Instance ?? (_Instance = new ProcessControl());
  20. private ProcessControl() { }
  21. public ObservableCollection<RemoteRecipeRawMaterial> RawMaterials { get; set; } = new ObservableCollection<RemoteRecipeRawMaterial>();
  22. public SiemensDeviceStatus SiemensDevice = new SiemensDeviceStatus();
  23. public HKDeviceStatus HKDevice = new HKDeviceStatus();
  24. GVL_SmallStation SmallStation = new GVL_SmallStation();
  25. /// <summary>
  26. /// 接收原料数据
  27. /// </summary>
  28. public RecipeRawMaterial RawMaterial;
  29. public void Init()
  30. {
  31. ActionManage.GetInstance.Register(new Action(() =>
  32. {
  33. if (SiemensDevice.IsConnected)
  34. {
  35. var res = SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.RecipeName);
  36. var res1 = SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.RecipeID);
  37. int res2 = SiemensDevice.Siemens_PLC_S7.ReadClass(RawMaterial, 0, 0);
  38. if ((res != null && res is string recipeName) &&
  39. (res1 != null && res1 is uint recipeID) &&
  40. (res2 > 0))
  41. {
  42. RawMaterials.Clear();
  43. for (int i = 0; i < 15; i++)
  44. {
  45. RawMaterials.Add(new RemoteRecipeRawMaterial()
  46. {
  47. RawMaterialBarrelNum = RawMaterial.RawMaterialBarrelNum[i],
  48. RawMaterialLocation = RawMaterial.RawMaterialLocation[i],
  49. RawMaterialWeight = RawMaterial.RawMaterialWeight[i]
  50. });
  51. }
  52. Json<RemoteRecipeDataColl>.Data.Recipes.Add(new RemoteRecipeData()
  53. {
  54. RecipeName = recipeName,
  55. RecipeCode = recipeID,
  56. RawMaterial = RawMaterials
  57. });
  58. }
  59. }
  60. }), "西门子下发配方", true);
  61. ActionManage.GetInstance.Register(new Action(() =>
  62. {
  63. if (SiemensDevice.IsConnected)
  64. {
  65. var res = SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.TrayLocationNum);
  66. var res1 = SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.RecipeID);
  67. if (res != null && res is int TrayLocation && res1 != null && res1 is int recipeId)
  68. {
  69. int index = Array.FindIndex(Json<RemoteRecipeDataColl>.Data.Recipes.ToArray(), p => p.RecipeCode == recipeId);
  70. if (index >= 0 && index < Json<RemoteRecipeDataColl>.Data.Recipes.Count)
  71. {
  72. Json<RemoteRecipeDataColl>.Data.Recipes.ElementAt(index).TrayCode = TrayLocation;
  73. }
  74. }
  75. }
  76. }), "AGV到位信号", true);//根据下发的配方ID将 托盘的位置信息添加到配方中
  77. string HK_PLC_IP = ConfigurationManager.AppSettings["HKPlc_IP"];
  78. string Siemens_PLC_IP = ConfigurationManager.AppSettings["Siemens_IP"];
  79. try
  80. {
  81. //HKDevice.HK_PLC_S7.Connect(S7.Net.CpuType.S7200Smart, HK_PLC_IP);
  82. //SiemensDevice.Siemens_PLC_S7.Connect(S7.Net.CpuType.S71500, Siemens_PLC_IP);
  83. if (HKDevice.IsConnected)
  84. {
  85. HKDevice.Init();
  86. }
  87. if (SiemensDevice.IsConnected)
  88. {
  89. SiemensDevice.Init();
  90. }
  91. }
  92. catch(Exception ex)
  93. {
  94. }
  95. SmallStation.RecipeQueue.Clear();
  96. Json<RemoteRecipeDataColl>.Data.Recipes = TestData.GetInstance.Recipes;//添加测试数据
  97. ThreadManage.GetInstance().StartLong(new Action(() =>
  98. {
  99. ReceviceData();
  100. RecipeInfoToHKPLC();
  101. Thread.Sleep(10);
  102. }), "流程处理", true);
  103. ThreadManage.GetInstance().StartLong(new Action(() => {
  104. if (SiemensDevice.IsConnected && HKDevice.IsConnected)
  105. {
  106. AgvGetInDelivery();
  107. AgvGetInPickUp();
  108. }
  109. Thread.Sleep(10);
  110. }), "AGV进站送取货", true);
  111. }
  112. /// <summary>
  113. /// AGV进站送货
  114. /// </summary>
  115. private void AgvGetInDelivery()
  116. {
  117. switch (SmallStation.AgvDeliveryPosition)
  118. {
  119. case 0:
  120. if (RTrig.GetInstance("").Start(SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.DeliveryAGVApply) is bool))
  121. {
  122. SmallStation.AgvDeliveryPosition = 1;
  123. HKDevice.HK_PLC_S7.Write(HKPlcCommAddress.DeliveryAGVApply, true);
  124. }
  125. break;
  126. case 1:
  127. if (RTrig.GetInstance("").Start(HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.DeliveryAGVIsApply) is bool))
  128. {
  129. SmallStation.AgvDeliveryPosition = 2;
  130. SiemensDevice.Siemens_PLC_S7.Write(SiemensCommAddress.DeliveryAGVIsApply, true);
  131. }
  132. break;
  133. case 2:
  134. if (RTrig.GetInstance("").Start(SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.DeliveryAGVApplyJack) is bool))
  135. {
  136. SmallStation.AgvDeliveryPosition = 3;
  137. HKDevice.HK_PLC_S7.Write(HKPlcCommAddress.DeliveryAGVApplyJack, true);
  138. }
  139. break;
  140. case 3:
  141. if (RTrig.GetInstance("").Start(HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.DeliveryAGVIsApplyJack) is bool))
  142. {
  143. SmallStation.AgvDeliveryPosition = 4;
  144. SiemensDevice.Siemens_PLC_S7.Write(SiemensCommAddress.DeliveryAGVIsApplyJack, true);
  145. }
  146. break;
  147. case 4:
  148. if (RTrig.GetInstance("").Start(SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.DeliveryAGVFinsih) is bool))
  149. {
  150. SmallStation.AgvDeliveryPosition = 5;
  151. HKDevice.HK_PLC_S7.Write(HKPlcCommAddress.DeliveryAGVFinsih, true);
  152. }
  153. break;
  154. case 5:
  155. if (RTrig.GetInstance("").Start(HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.StationHaveCargo) is bool))
  156. {
  157. SmallStation.AgvDeliveryPosition = 0;
  158. SiemensDevice.Siemens_PLC_S7.Write(SiemensCommAddress.StationHaveCargo, true);
  159. }
  160. break;
  161. default:
  162. break;
  163. }
  164. //获取工位上是否有小车
  165. SiemensDevice.Siemens_PLC_S7.Write(SiemensCommAddress.StationIsExistCar, (bool)
  166. HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.StationIsExistCar));
  167. }
  168. /// <summary>
  169. /// AGV进站取货
  170. /// </summary>
  171. private void AgvGetInPickUp()
  172. {
  173. switch (SmallStation.AgvPickUpPosition)
  174. {
  175. case 0:
  176. if (RTrig.GetInstance("").Start(SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.PickAGVApply) is bool))
  177. {
  178. SmallStation.AgvPickUpPosition = 1;
  179. HKDevice.HK_PLC_S7.Write(HKPlcCommAddress.PickAGVApply, true);
  180. }
  181. break;
  182. case 1:
  183. if (RTrig.GetInstance("").Start(HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.PickAGVIsApply) is bool))
  184. {
  185. SmallStation.AgvPickUpPosition = 2;
  186. SiemensDevice.Siemens_PLC_S7.Write(SiemensCommAddress.PickAGVIsApply, true);
  187. }
  188. break;
  189. case 2:
  190. if (RTrig.GetInstance("").Start(SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.PickCargoAGVFinish) is bool))
  191. {
  192. SmallStation.AgvPickUpPosition = 3;
  193. HKDevice.HK_PLC_S7.Write(HKPlcCommAddress.PickCargoAGVFinish, true);
  194. }
  195. break;
  196. case 3:
  197. if (RTrig.GetInstance("").Start(HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.PickAGVFinish) is bool))
  198. {
  199. SmallStation.AgvPickUpPosition = 0;
  200. SiemensDevice.Siemens_PLC_S7.Write(SiemensCommAddress.PickAGVFinish, true);
  201. }
  202. break;
  203. default:
  204. break;
  205. }
  206. }
  207. private void ReceviceData()
  208. {
  209. SmallStation.RemoteRecipes = Json<RemoteRecipeDataColl>.Data.Recipes;
  210. if (Json<RemoteRecipeDataColl>.Data.Recipes.Count > 0)
  211. {
  212. foreach (var data in Json<RemoteRecipeDataColl>.Data.Recipes)
  213. {
  214. if(!(SmallStation.RecipeQueue.Contains(data.RecipeCode)))
  215. SmallStation.RecipeQueue.Enqueue(data.RecipeCode);
  216. }
  217. }
  218. }
  219. private void RecipeInfoToHKPLC()
  220. {
  221. if (SmallStation.RecipeQueue.Count > 0)
  222. {
  223. int index = Array.FindIndex(SmallStation.RemoteRecipes.ToArray(), p => p.RecipeCode == SmallStation.RecipeQueue.ElementAt(0));
  224. if (index >= 0 && index <= SmallStation.RemoteRecipes.Count)
  225. {
  226. long code = SmallStation.RemoteRecipes.ElementAt(index).RecipeCode;
  227. if (RTrig.GetInstance("IsAllowDosing").Start(HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.IsAllowDosing) is bool a))
  228. {
  229. foreach (var item in SmallStation.RemoteRecipes.ElementAt(index).RawMaterial)
  230. {
  231. HKDevice.StockBinPar((uint)item.RawMaterialBarrelNum, (ushort)item.RawMaterialLocation);
  232. }
  233. HKDevice.AllowDosingSignReset();
  234. SmallStation.IssueRecipeFinish = true;
  235. }
  236. if (RTrig.GetInstance("StockState").Start(HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.StockState) is bool) && SmallStation.IssueRecipeFinish)
  237. {
  238. var res = HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.StockBinLocation);
  239. if (res != null && res is int loc)
  240. {
  241. int loc_index = Array.FindIndex(SmallStation.RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == loc);
  242. double weight = SmallStation.RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(loc_index).RawMaterialWeight;
  243. if (loc_index >= 1 && loc_index <= 15)
  244. {
  245. DeviceInquire.GetInstance.GetDeviceObject(loc_index)?.Start((uint)weight);//启动并写入每个原料重量
  246. SmallStation.StockInIsWork = loc_index;
  247. HKDevice.StockStateSignReset();
  248. }
  249. }
  250. }
  251. if (RTrig.GetInstance("OutRawMaterialFinish").Start(DeviceInquire.GetInstance.GetDeviceObject(SmallStation.StockInIsWork).modbusTcp.Read(DeviceAddress.OutRawMaterialFinish) is bool))
  252. {
  253. HKDevice.SingleDosing((uint)SmallStation.StockInIsWork);
  254. DeviceInquire.GetInstance.GetDeviceObject(SmallStation.StockInIsWork).OutRawMaterialReset();
  255. }
  256. if (RTrig.GetInstance("StockState").Start(HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.RecipeDosingFinish) is bool))
  257. {
  258. HKDevice.RecipeDosingFinishReset();
  259. SmallStation.RecipeQueue.TryDequeue(out code);
  260. Json<RemoteRecipeDataColl>.Data.Recipes.RemoveAt(index);//制作完成,移除当前配方
  261. }
  262. }
  263. }
  264. }
  265. }
  266. }