终端一体化运控平台
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

270 строки
15 KiB

  1. using BPASmartClient.Helper;
  2. using BPASmartClient.Modbus;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using BPASmartClient.CustomResource.Pages.Model;
  8. using System.Threading.Tasks;
  9. using BPASmartClient.S7Net;
  10. using System.Threading;
  11. using BPASmartClient.JXJFoodBigStation.Model.Siemens;
  12. namespace BPASmartClient.JXJFoodBigStation.Model.HK_PLC
  13. {
  14. public class HKDeviceStatus
  15. {
  16. public SiemensHelper HK_PLC_S7 = new SiemensHelper();
  17. public bool IsConnected => HK_PLC_S7.IsConnected;
  18. public DB_Read PlcRead = new DB_Read();
  19. public DL_DataColl_DB DataColl = new DL_DataColl_DB();
  20. public void Init()
  21. {
  22. try
  23. {
  24. DB_Write PlcWrite = new DB_Write();
  25. HK_PLC_S7.WriteClass<DB_Write>(PlcWrite, 99);
  26. }
  27. catch (Exception ex)
  28. {
  29. MessageNotify.GetInstance.ShowRunLog("DB99块初始化值 失败");
  30. }
  31. ThreadManage.GetInstance().StartLong(new Action(() =>
  32. {/*
  33. var res = HK_PLC_S7.ReadClass<StockBinName>(97);//料仓里原料的编码
  34. if (res != null && res is StockBinName data9)
  35. {
  36. GVL_BigStation.stockBinName = data9;
  37. }*/
  38. if (IsConnected)
  39. {
  40. var res1 = HK_PLC_S7.ReadClass<DB_Read>(98);
  41. var res2 = HK_PLC_S7.ReadClass<StockBinName>(97);//料仓里原料的编码
  42. if (res1 != null && res1 is DB_Read data1)
  43. {
  44. PlcRead = data1;
  45. GVL_BigStation.HKPlc_Read = data1;
  46. }
  47. if (res2 != null && res2 is StockBinName data2)
  48. {
  49. GVL_BigStation.stockBinName = data2;
  50. }
  51. var res3 = HK_PLC_S7.ReadClass<DL_DataColl_DB>(48);
  52. if (res3 != null && res3 is DL_DataColl_DB data)
  53. {
  54. DataColl = data;
  55. }
  56. }
  57. Thread.Sleep(10);
  58. }), "海科数据交互", true);
  59. }
  60. /// <summary>
  61. /// 往plc下发配方数据
  62. /// </summary>
  63. public void WritePlcRecipeData(RecipeData recipe)
  64. {
  65. if (IsConnected)
  66. {
  67. if (recipe != null)
  68. {
  69. HK_PLC_S7.Write("DB99.DBW2.0", Convert.ToInt16(recipe.TrayCode));
  70. HK_PLC_S7.Write("DB99.DBW4.0", Convert.ToInt16(recipe.TrayCode));
  71. MessageNotify.GetInstance.ShowRunLog($"配方编号:{recipe.RecipeCode},托盘编号DB99.DBW2.0:{recipe.TrayCode},下发完成");
  72. for (int barrel = 1; barrel < 6; barrel++)
  73. {
  74. if (barrel != 3)//桶的编号不为3 只为1,2,4,5号桶
  75. {
  76. for (int loc = 1; loc < 15; loc++)
  77. {
  78. int index = Array.FindIndex(recipe.RawMaterial.ToArray(), p => p.RawMaterialBarrelNum == barrel && p.RawMaterialLocation == loc);
  79. #region 新版本
  80. int x = 0;
  81. if (barrel < 3) x = (barrel - 1) * 56 + 6;
  82. else if (barrel > 3) x = (barrel - 2) * 56 + 6;
  83. string address = "DB99.DBD" + (x + 4 * (loc - 1));
  84. if (index == -1)
  85. {
  86. HK_PLC_S7.Write(address, 0);
  87. }
  88. else
  89. {
  90. HK_PLC_S7.Write(address, recipe.RawMaterial.ElementAt(index).RawMaterialWeight);
  91. MessageNotify.GetInstance.ShowRunLog($"配方编号:{recipe.RecipeCode},托盘编号:{recipe.TrayCode},桶号:{barrel},位置:{loc},重量:{recipe.RawMaterial.ElementAt(index).RawMaterialWeight}");
  92. }
  93. #endregion
  94. #region 旧版本
  95. //if (index == -1)
  96. //{
  97. // switch (barrel)
  98. // {
  99. // case 1:
  100. // string address = "DB99.DBD" + (6 + 4 * (loc - 1));
  101. // HK_PLC_S7.Write(address, 0);
  102. // break;
  103. // case 2:
  104. // string address1 = "DB99.DBD" + (62 + 4 * (loc - 1));
  105. // HK_PLC_S7.Write(address1, 0);
  106. // break;
  107. // case 4:
  108. // string address2 = "DB99.DBD" + (118 + 4 * (loc - 1));
  109. // HK_PLC_S7.Write(address2, 0);
  110. // break;
  111. // case 5:
  112. // string address3 = "DB99.DBD" + (174 + 4 * (loc - 1));
  113. // HK_PLC_S7.Write(address3, 0);
  114. // break;
  115. // default:
  116. // break;
  117. // }
  118. //}
  119. //else
  120. //{
  121. // switch (barrel)
  122. // {
  123. // case 1:
  124. // string address = "DB99.DBD" + (6 + 4 * (loc - 1));
  125. // HK_PLC_S7.Write(address, recipe.RawMaterial.ElementAt(index).RawMaterialWeight);
  126. // MessageNotify.GetInstance.ShowRunLog($"配方编号:{recipe.RecipeCode},托盘编号:{recipe.TrayCode},桶号:{barrel},位置:{loc},重量:{recipe.RawMaterial.ElementAt(index).RawMaterialWeight}");
  127. // break;
  128. // case 2:
  129. // string address1 = "DB99.DBD" + (62 + 4 * (loc - 1));
  130. // HK_PLC_S7.Write(address1, recipe.RawMaterial.ElementAt(index).RawMaterialWeight);
  131. // MessageNotify.GetInstance.ShowRunLog($"配方编号:{recipe.RecipeCode},托盘编号:{recipe.TrayCode},桶号:{barrel},位置:{loc},重量:{recipe.RawMaterial.ElementAt(index).RawMaterialWeight}");
  132. // break;
  133. // case 4:
  134. // string address2 = "DB99.DBD" + (118 + 4 * (loc - 1));
  135. // HK_PLC_S7.Write(address2, recipe.RawMaterial.ElementAt(index).RawMaterialWeight);
  136. // MessageNotify.GetInstance.ShowRunLog($"配方编号:{recipe.RecipeCode},托盘编号:{recipe.TrayCode},桶号:{barrel},位置:{loc},重量:{recipe.RawMaterial.ElementAt(index).RawMaterialWeight}");
  137. // break;
  138. // case 5:
  139. // string address3 = "DB99.DBD" + (174 + 4 * (loc - 1));
  140. // HK_PLC_S7.Write(address3, recipe.RawMaterial.ElementAt(index).RawMaterialWeight);
  141. // MessageNotify.GetInstance.ShowRunLog($"配方编号:{recipe.RecipeCode},托盘编号:{recipe.TrayCode},桶号:{barrel},位置:{loc},重量:{recipe.RawMaterial.ElementAt(index).RawMaterialWeight}");
  142. // break;
  143. // default:
  144. // break;
  145. // }
  146. //}
  147. #endregion
  148. }
  149. }
  150. }
  151. }
  152. //验证配方数据是否下发
  153. Thread.Sleep(200);
  154. if (!ReadPlcRecipeData(recipe))
  155. {
  156. App.Current.Dispatcher.Invoke(() =>
  157. {
  158. MessageNotify.GetInstance.ShowDialog($"配方{recipe.RecipeCode},plc配方数据和西门子配方数据不一致!", DialogType.Error);
  159. });
  160. }
  161. MessageNotify.GetInstance.ShowRunLog($"配方编号:{recipe.RecipeCode},配方数据验证成功");
  162. }
  163. }
  164. /// <summary>
  165. /// 读取plc收到的配方数据
  166. /// </summary>
  167. /// <param name="recipe"></param>
  168. /// <returns></returns>
  169. public bool ReadPlcRecipeData(RecipeData recipe)
  170. {
  171. if (IsConnected)
  172. {
  173. if (recipe != null)
  174. {
  175. ushort Code1 = HK_PLC_S7.Read<ushort>("DB99.DBW2.0");
  176. ushort Code2 = HK_PLC_S7.Read<ushort>("DB99.DBW4.0");
  177. if (recipe.TrayCode != Code1 || recipe.TrayCode != Code2)
  178. {
  179. MessageNotify.GetInstance.ShowRunLog($"下发的配方数据,DB99.DBW2.0:{recipe.TrayCode},DB99.DBW2.0:{recipe.TrayCode}");
  180. return false;
  181. }
  182. MessageNotify.GetInstance.ShowRunLog($"配方编号:{recipe.RecipeCode},托盘编号:{recipe.TrayCode},下发完成");
  183. for (int barrel = 1; barrel < 6; barrel++)
  184. {
  185. if (barrel != 3)//桶的编号不为3 只为1,2,4,5号桶
  186. {
  187. for (int loc = 1; loc < 15; loc++)
  188. {
  189. int index = Array.FindIndex(recipe.RawMaterial.ToArray(), p => p.RawMaterialBarrelNum == barrel && p.RawMaterialLocation == loc);
  190. if (index == -1)
  191. {
  192. float weight0 = (float)0.0;
  193. float ReadWeight = (float)0.0;
  194. string Address = "";
  195. switch (barrel)
  196. {
  197. case 1:
  198. Address = "DB99.DBD" + (6 + 4 * (loc - 1));
  199. ReadWeight = HK_PLC_S7.Read<float>(Address);
  200. break;
  201. case 2:
  202. Address = "DB99.DBD" + (62 + 4 * (loc - 1));
  203. ReadWeight = HK_PLC_S7.Read<float>(Address);
  204. break;
  205. case 4:
  206. Address = "DB99.DBD" + (118 + 4 * (loc - 1));
  207. ReadWeight = HK_PLC_S7.Read<float>(Address);
  208. break;
  209. case 5:
  210. Address = "DB99.DBD" + (174 + 4 * (loc - 1));
  211. ReadWeight = HK_PLC_S7.Read<float>(Address);
  212. break;
  213. default:
  214. break;
  215. }
  216. if (Math.Round(weight0, 1) != Math.Round(ReadWeight, 1))
  217. {
  218. MessageNotify.GetInstance.ShowRunLog($"地址:{Address},配方重量为0,读取plc值{ReadWeight}");
  219. return false;
  220. }
  221. }
  222. else
  223. {
  224. string Address = "";
  225. float ReadWeight = (float)0.0;
  226. switch (barrel)
  227. {
  228. case 1:
  229. Address = "DB99.DBD" + (6 + 4 * (loc - 1));
  230. ReadWeight = HK_PLC_S7.Read<float>(Address);
  231. break;
  232. case 2:
  233. Address = "DB99.DBD" + (62 + 4 * (loc - 1));
  234. ReadWeight = HK_PLC_S7.Read<float>(Address);
  235. break;
  236. case 4:
  237. Address = "DB99.DBD" + (118 + 4 * (loc - 1));
  238. ReadWeight = HK_PLC_S7.Read<float>(Address);
  239. break;
  240. case 5:
  241. Address = "DB99.DBD" + (174 + 4 * (loc - 1));
  242. ReadWeight = HK_PLC_S7.Read<float>(Address);
  243. break;
  244. default:
  245. break;
  246. }
  247. MessageNotify.GetInstance.ShowRunLog($"配方编号:{recipe.RecipeCode},托盘编号:{recipe.TrayCode},桶号:{barrel},地址:{Address},重量:{ReadWeight}");
  248. if (Math.Floor(recipe.RawMaterial.ElementAt(index).RawMaterialWeight) != Math.Floor(ReadWeight))
  249. {
  250. return false;
  251. }
  252. }
  253. }
  254. }
  255. }
  256. return true;
  257. }
  258. return false;
  259. }
  260. return false;
  261. }
  262. }
  263. }