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

296 lines
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 System.Threading.Tasks;
  8. using BPASmartClient.S7Net;
  9. using BPASmartClient.CustomResource.Pages.Model;
  10. using System.Threading;
  11. using System.Collections.ObjectModel;
  12. namespace BPASmartClient.JXJFoodSmallStation.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 PlcReadAddressDB45 DeviceStatus = new PlcReadAddressDB45();
  19. public void Init()
  20. {
  21. ThreadManage.GetInstance().StartLong(new Action(() =>
  22. {
  23. if (IsConnected)
  24. {
  25. var res = this.HK_PLC_S7.ReadClass<PlcReadAddressDB45>(45);
  26. if (res != null && res is PlcReadAddressDB45 data)
  27. {
  28. DeviceStatus = data;
  29. }
  30. var res1 = this.HK_PLC_S7.ReadClass<PlcReadAddressDB3>(3);
  31. if (res1 != null && res1 is PlcReadAddressDB3 data1)
  32. {
  33. GVL_SmallStation.GetInstance.plcReadDataDB3 = data1;
  34. }
  35. }
  36. Thread.Sleep(10);
  37. }), "海科PLC设备状态", true);
  38. }
  39. ///// <summary>
  40. ///// 单个配料机配料完成
  41. ///// </summary>
  42. ///// <param name="value"></param>
  43. //public void SingleDosing(uint value)
  44. //{
  45. // if (IsConnected)
  46. // {
  47. // HK_PLC_S7.Write(HKPlcCommAddress.SingleStockBinLocationToPLC, value);
  48. // HK_PLC_S7.Write(HKPlcCommAddress.SingleDosingFinishToPLC, (ushort)1);
  49. // }
  50. //}
  51. ///// <summary>
  52. ///// 复位plc允许接料信号
  53. ///// </summary>
  54. //public void AllowDosingSignReset()
  55. //{
  56. // HK_PLC_S7.Write(HKPlcCommAddress.IsAllowDosing,(ushort)0);
  57. //}
  58. ///// <summary>
  59. ///// 复位plc到达某料仓信号
  60. ///// </summary>
  61. //public void StockStateSignReset()
  62. //{
  63. // HK_PLC_S7.Write(HKPlcCommAddress.StockState, (ushort)0);
  64. //}
  65. ///// <summary>
  66. ///// 复位一个配方配料完成信号
  67. ///// </summary>
  68. //public void RecipeDosingFinishReset()
  69. //{
  70. // HK_PLC_S7.Write(HKPlcCommAddress.RecipeDosingFinish, (ushort)0);
  71. //}
  72. private void TempStockBinPar(int num, ushort StockBinLocation, string info)
  73. {
  74. if (StockBinLocation >= 1 && StockBinLocation <= 8)
  75. {
  76. HK_PLC_S7.Write<bool>($"DB4.DBX{num}." + (StockBinLocation - 1), true);
  77. }
  78. else if (StockBinLocation >= 9 && StockBinLocation <= 15)
  79. {
  80. HK_PLC_S7.Write<bool>($"DB4.DBX{num + 1}." + (StockBinLocation - 9), true);
  81. }
  82. MessageNotify.GetInstance.ShowRunLog($"托盘{info}号桶在料仓{StockBinLocation}配料");
  83. }
  84. /// <summary>
  85. /// 下发配方数据
  86. /// </summary>
  87. /// <param name="BarrelNum">单个桶的编号</param>
  88. /// <param name="StockBinLocation">单个桶对应的料仓位置</param>
  89. public void StockBinPar(uint BarrelNum, ushort StockBinLocation, int TrayNum = 1)
  90. {
  91. if (IsConnected)
  92. {
  93. if (TrayNum == 1)
  94. {
  95. if (BarrelNum >= 6 && BarrelNum <= 8 && StockBinLocation >= 1 && StockBinLocation <= 15)
  96. {
  97. if (BarrelNum == 6)
  98. {
  99. TempStockBinPar(10, StockBinLocation, "1—1");
  100. //if (StockBinLocation >= 1 && StockBinLocation <= 8)
  101. //{
  102. // HK_PLC_S7.Write<bool>("DB4.DBX10." + (StockBinLocation - 1), true);
  103. //}
  104. //else if (StockBinLocation >= 9 && StockBinLocation <= 15)
  105. //{
  106. // HK_PLC_S7.Write<bool>("DB4.DBX11." + (StockBinLocation - 9), true);
  107. //}
  108. //MessageNotify.GetInstance.ShowRunLog($"托盘1—1号桶在料仓{StockBinLocation}配料");
  109. }
  110. else if (BarrelNum == 7)
  111. {
  112. TempStockBinPar(12, StockBinLocation, "1—2");
  113. //if (StockBinLocation >= 1 && StockBinLocation <= 8)
  114. //{
  115. // HK_PLC_S7.Write<bool>("DB4.DBX12." + (StockBinLocation - 1), true);
  116. //}
  117. //else if (StockBinLocation >= 9 && StockBinLocation <= 15)
  118. //{
  119. // HK_PLC_S7.Write<bool>("DB4.DBX13." + (StockBinLocation - 9), true);
  120. //}
  121. //MessageNotify.GetInstance.ShowRunLog($"托盘1—2号桶在料仓{StockBinLocation}配料");
  122. }
  123. else if (BarrelNum == 8)
  124. {
  125. TempStockBinPar(14, StockBinLocation, "1—3");
  126. //if (StockBinLocation >= 1 && StockBinLocation <= 8)
  127. //{
  128. // HK_PLC_S7.Write<bool>("DB4.DBX14." + (StockBinLocation - 1), true);
  129. //}
  130. //else if (StockBinLocation >= 9 && StockBinLocation <= 15)
  131. //{
  132. // HK_PLC_S7.Write<bool>("DB4.DBX15." + (StockBinLocation - 9), true);
  133. //}
  134. //MessageNotify.GetInstance.ShowRunLog($"托盘1—3号桶在料仓{StockBinLocation}配料");
  135. }
  136. else if (BarrelNum == 4)
  137. {
  138. TempStockBinPar(16, StockBinLocation, "1—4");
  139. //if (StockBinLocation >= 1 && StockBinLocation <= 8)
  140. //{
  141. // HK_PLC_S7.Write<bool>("DB4.DBX16." + (StockBinLocation - 1), true);
  142. //}
  143. //else if (StockBinLocation >= 9 && StockBinLocation <= 15)
  144. //{
  145. // HK_PLC_S7.Write<bool>("DB4.DBX17." + (StockBinLocation - 9), true);
  146. //}
  147. //MessageNotify.GetInstance.ShowRunLog($"托盘1—4号桶在料仓{StockBinLocation}配料");
  148. }
  149. }
  150. }
  151. else if (TrayNum == 2)
  152. {
  153. if (BarrelNum >= 1 && BarrelNum <= 4 && StockBinLocation >= 1 && StockBinLocation <= 15)
  154. {
  155. if (BarrelNum == 6)
  156. {
  157. TempStockBinPar(18, StockBinLocation, "2—1");
  158. //if (StockBinLocation >= 1 && StockBinLocation <= 8)
  159. //{
  160. // HK_PLC_S7.Write<bool>("DB4.DBX18." + (StockBinLocation - 1), true);
  161. //}
  162. //else if (StockBinLocation >= 9 && StockBinLocation <= 15)
  163. //{
  164. // HK_PLC_S7.Write<bool>("DB4.DBX19." + (StockBinLocation - 9), true);
  165. //}
  166. //MessageNotify.GetInstance.ShowRunLog($"托盘2—1号桶在料仓{StockBinLocation}配料");
  167. }
  168. else if (BarrelNum == 7)
  169. {
  170. TempStockBinPar(20, StockBinLocation, "2—2");
  171. //if (StockBinLocation >= 1 && StockBinLocation <= 8)
  172. //{
  173. // HK_PLC_S7.Write<bool>("DB4.DBX20." + (StockBinLocation - 1), true);
  174. //}
  175. //else if (StockBinLocation >= 9 && StockBinLocation <= 15)
  176. //{
  177. // HK_PLC_S7.Write<bool>("DB4.DBX21." + (StockBinLocation - 9), true);
  178. //}
  179. //MessageNotify.GetInstance.ShowRunLog($"托盘2—2号桶在料仓{StockBinLocation}配料");
  180. }
  181. else if (BarrelNum == 8)
  182. {
  183. TempStockBinPar(22, StockBinLocation, "2—3");
  184. //if (StockBinLocation >= 1 && StockBinLocation <= 8)
  185. //{
  186. // HK_PLC_S7.Write<bool>("DB4.DBX22." + (StockBinLocation - 1), true);
  187. //}
  188. //else if (StockBinLocation >= 9 && StockBinLocation <= 15)
  189. //{
  190. // HK_PLC_S7.Write<bool>("DB4.DBX23." + (StockBinLocation - 9), true);
  191. //}
  192. //MessageNotify.GetInstance.ShowRunLog($"托盘2—3号桶在料仓{StockBinLocation}配料");
  193. }
  194. else if (BarrelNum == 4)
  195. {
  196. TempStockBinPar(24, StockBinLocation, "2—4");
  197. //if (StockBinLocation >= 1 && StockBinLocation <= 8)
  198. //{
  199. // HK_PLC_S7.Write<bool>("DB4.DBX24." + (StockBinLocation - 1), true);
  200. //}
  201. //else if (StockBinLocation >= 9 && StockBinLocation <= 15)
  202. //{
  203. // HK_PLC_S7.Write<bool>("DB4.DBX25." + (StockBinLocation - 9), true);
  204. //}
  205. //MessageNotify.GetInstance.ShowRunLog($"托盘2—4号桶在料仓{StockBinLocation}配料");
  206. }
  207. }
  208. }
  209. }
  210. }
  211. public void IssueRecipeToPlc(ObservableCollection<RemoteRecipeRawMaterial> rawMaterials, int recipeLoction)
  212. {
  213. bool[] barrel = new bool[16];
  214. bool[] barrel1 = new bool[16];
  215. bool[] barrel2 = new bool[16];
  216. bool[] barrel3 = new bool[16];
  217. ushort barrel1_short = 0;
  218. ushort barrel2_short = 0;
  219. ushort barrel3_short = 0;
  220. foreach (var item in rawMaterials)
  221. {
  222. switch (item.RawMaterialBarrelNum)
  223. {
  224. case 6:
  225. barrel[item.RawMaterialLocation - 1] = true;
  226. barrel1[item.RawMaterialLocation - 1] = true;
  227. break;
  228. case 7:
  229. barrel[item.RawMaterialLocation - 1] = true;
  230. barrel2[item.RawMaterialLocation - 1] = true;
  231. break;
  232. case 8:
  233. barrel[item.RawMaterialLocation - 1] = true;
  234. barrel3[item.RawMaterialLocation - 1] = true;
  235. break;
  236. default:
  237. break;
  238. }
  239. }
  240. for (int i = 0; i < 16; i++)
  241. {
  242. if (recipeLoction == 0)
  243. {
  244. if (i >= 8)
  245. {
  246. HK_PLC_S7.Write<bool>("DB4.DBX15." + (i - 8), barrel1[i]);
  247. HK_PLC_S7.Write<bool>("DB4.DBX17." + (i - 8), barrel2[i]);
  248. HK_PLC_S7.Write<bool>("DB4.DBX19." + (i - 8), barrel3[i]);
  249. }
  250. else
  251. {
  252. HK_PLC_S7.Write<bool>("DB4.DBX14." + i, barrel1[i]);
  253. HK_PLC_S7.Write<bool>("DB4.DBX16." + i, barrel2[i]);
  254. HK_PLC_S7.Write<bool>("DB4.DBX18." + i, barrel3[i]);
  255. }
  256. barrel1_short = barrel1_short.SetBitValue((byte)(i + 1), barrel1[i]);
  257. barrel2_short = barrel2_short.SetBitValue((byte)(i + 1), barrel2[i]);
  258. barrel3_short = barrel3_short.SetBitValue((byte)(i + 1), barrel3[i]);
  259. GVL_SmallStation.GetInstance.RecipeStockBinDosing[recipeLoction] = GVL_SmallStation.GetInstance.RecipeStockBinDosing[recipeLoction].SetBitValue((byte)(i + 1), barrel[i]);
  260. }
  261. else if (recipeLoction == 1)
  262. {
  263. if (i >= 8)
  264. {
  265. HK_PLC_S7.Write<bool>("DB4.DBX21." + (i - 8), barrel1[i]);
  266. HK_PLC_S7.Write<bool>("DB4.DBX23." + (i - 8), barrel2[i]);
  267. HK_PLC_S7.Write<bool>("DB4.DBX25." + (i - 8), barrel3[i]);
  268. }
  269. else
  270. {
  271. HK_PLC_S7.Write<bool>("DB4.DBX20." + i, barrel1[i]);
  272. HK_PLC_S7.Write<bool>("DB4.DBX22." + i, barrel2[i]);
  273. HK_PLC_S7.Write<bool>("DB4.DBX24." + i, barrel3[i]);
  274. }
  275. barrel1_short = barrel1_short.SetBitValue((byte)(i + 1), barrel1[i]);
  276. barrel2_short = barrel2_short.SetBitValue((byte)(i + 1), barrel2[i]);
  277. barrel3_short = barrel3_short.SetBitValue((byte)(i + 1), barrel3[i]);
  278. GVL_SmallStation.GetInstance.RecipeStockBinDosing[recipeLoction] = GVL_SmallStation.GetInstance.RecipeStockBinDosing[recipeLoction].SetBitValue((byte)(i + 1), barrel[i]);
  279. }
  280. }
  281. //HK_PLC_S7.Write<bool[]>(AddresBarrel1, barrel1);
  282. //HK_PLC_S7.Write<bool[]>(AddresBarrel2, barrel2);
  283. //HK_PLC_S7.Write<bool[]>(AddresBarrel3, barrel3);
  284. MessageNotify.GetInstance.ShowRunLog($"配方下发,plc配方位置{recipeLoction},桶1料仓配料{barrel1_short},配料位置{barrel1_short.ToBinString()}");
  285. MessageNotify.GetInstance.ShowRunLog($"配方下发,plc配方位置{recipeLoction},桶2料仓配料{barrel2_short},配料位置{barrel2_short.ToBinString()}");
  286. MessageNotify.GetInstance.ShowRunLog($"配方下发,plc配方位置{recipeLoction},桶3料仓配料{barrel3_short},配料位置{barrel3_short.ToBinString()}");
  287. MessageNotify.GetInstance.ShowRunLog($"配方下发,plc配方位置{recipeLoction},料仓配料{GVL_SmallStation.GetInstance.RecipeStockBinDosing[recipeLoction]},配料位置{GVL_SmallStation.GetInstance.RecipeStockBinDosing[recipeLoction].ToBinString()}");
  288. }
  289. }
  290. }