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

211 lines
10 KiB

  1. using BPA.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. TaskManage.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. int x = 0;
  80. if (barrel < 3) x = (barrel - 1) * 56 + 6;
  81. else if (barrel > 3) x = (barrel - 2) * 56 + 6;
  82. string address = "DB99.DBD" + (x + 4 * (loc - 1));
  83. if (index == -1)
  84. {
  85. HK_PLC_S7.Write(address, 0);
  86. }
  87. else
  88. {
  89. HK_PLC_S7.Write(address, recipe.RawMaterial.ElementAt(index).RawMaterialWeight);
  90. MessageNotify.GetInstance.ShowRunLog($"配方编号:{recipe.RecipeCode},托盘编号:{recipe.TrayCode},桶号:{barrel},位置:{loc},重量:{recipe.RawMaterial.ElementAt(index).RawMaterialWeight}");
  91. }
  92. }
  93. }
  94. }
  95. }
  96. //验证配方数据是否下发
  97. Thread.Sleep(200);
  98. if (!ReadPlcRecipeData(recipe))
  99. {
  100. App.Current.Dispatcher.Invoke(() =>
  101. {
  102. MessageNotify.GetInstance.ShowDialog($"配方{recipe.RecipeCode},plc配方数据和西门子配方数据不一致!", DialogType.Error);
  103. });
  104. }
  105. MessageNotify.GetInstance.ShowRunLog($"配方编号:{recipe.RecipeCode},配方数据验证成功");
  106. }
  107. }
  108. /// <summary>
  109. /// 读取plc收到的配方数据
  110. /// </summary>
  111. /// <param name="recipe"></param>
  112. /// <returns></returns>
  113. public bool ReadPlcRecipeData(RecipeData recipe)
  114. {
  115. if (IsConnected)
  116. {
  117. if (recipe != null)
  118. {
  119. ushort Code1 = HK_PLC_S7.Read<ushort>("DB99.DBW2.0");
  120. ushort Code2 = HK_PLC_S7.Read<ushort>("DB99.DBW4.0");
  121. if (recipe.TrayCode != Code1 || recipe.TrayCode != Code2)
  122. {
  123. MessageNotify.GetInstance.ShowRunLog($"下发的配方数据,DB99.DBW2.0:{recipe.TrayCode},DB99.DBW2.0:{recipe.TrayCode}");
  124. return false;
  125. }
  126. MessageNotify.GetInstance.ShowRunLog($"配方编号:{recipe.RecipeCode},托盘编号:{recipe.TrayCode},下发完成");
  127. for (int barrel = 1; barrel < 6; barrel++)
  128. {
  129. if (barrel != 3)//桶的编号不为3 只为1,2,4,5号桶
  130. {
  131. for (int loc = 1; loc < 15; loc++)
  132. {
  133. int index = Array.FindIndex(recipe.RawMaterial.ToArray(), p => p.RawMaterialBarrelNum == barrel && p.RawMaterialLocation == loc);
  134. if (index == -1)
  135. {
  136. float weight0 = (float)0.0;
  137. float ReadWeight = (float)0.0;
  138. string Address = "";
  139. switch (barrel)
  140. {
  141. case 1:
  142. Address = "DB99.DBD" + (6 + 4 * (loc - 1));
  143. ReadWeight = HK_PLC_S7.Read<float>(Address);
  144. break;
  145. case 2:
  146. Address = "DB99.DBD" + (62 + 4 * (loc - 1));
  147. ReadWeight = HK_PLC_S7.Read<float>(Address);
  148. break;
  149. case 4:
  150. Address = "DB99.DBD" + (118 + 4 * (loc - 1));
  151. ReadWeight = HK_PLC_S7.Read<float>(Address);
  152. break;
  153. case 5:
  154. Address = "DB99.DBD" + (174 + 4 * (loc - 1));
  155. ReadWeight = HK_PLC_S7.Read<float>(Address);
  156. break;
  157. default:
  158. break;
  159. }
  160. if (Math.Round(weight0, 1) != Math.Round(ReadWeight, 1))
  161. {
  162. MessageNotify.GetInstance.ShowRunLog($"地址:{Address},配方重量为0,读取plc值{ReadWeight}");
  163. return false;
  164. }
  165. }
  166. else
  167. {
  168. string Address = "";
  169. float ReadWeight = (float)0.0;
  170. switch (barrel)
  171. {
  172. case 1:
  173. Address = "DB99.DBD" + (6 + 4 * (loc - 1));
  174. ReadWeight = HK_PLC_S7.Read<float>(Address);
  175. break;
  176. case 2:
  177. Address = "DB99.DBD" + (62 + 4 * (loc - 1));
  178. ReadWeight = HK_PLC_S7.Read<float>(Address);
  179. break;
  180. case 4:
  181. Address = "DB99.DBD" + (118 + 4 * (loc - 1));
  182. ReadWeight = HK_PLC_S7.Read<float>(Address);
  183. break;
  184. case 5:
  185. Address = "DB99.DBD" + (174 + 4 * (loc - 1));
  186. ReadWeight = HK_PLC_S7.Read<float>(Address);
  187. break;
  188. default:
  189. break;
  190. }
  191. MessageNotify.GetInstance.ShowRunLog($"配方编号:{recipe.RecipeCode},托盘编号:{recipe.TrayCode},桶号:{barrel},地址:{Address},重量:{ReadWeight}");
  192. if (Math.Floor(recipe.RawMaterial.ElementAt(index).RawMaterialWeight) != Math.Floor(ReadWeight))
  193. {
  194. return false;
  195. }
  196. }
  197. }
  198. }
  199. }
  200. return true;
  201. }
  202. return false;
  203. }
  204. return false;
  205. }
  206. }
  207. }