using BPA.Helper; using BPASmartClient.Modbus; using System; using System.Collections.Generic; using System.Linq; using System.Text; using BPASmartClient.CustomResource.Pages.Model; using System.Threading.Tasks; using BPASmartClient.S7Net; using System.Threading; using BPASmartClient.JXJFoodBigStation.Model.Siemens; namespace BPASmartClient.JXJFoodBigStation.Model.HK_PLC { public class HKDeviceStatus { public SiemensHelper HK_PLC_S7 = new SiemensHelper(); public bool IsConnected => HK_PLC_S7.IsConnected; public DB_Read PlcRead = new DB_Read(); public DL_DataColl_DB DataColl = new DL_DataColl_DB(); public void Init() { try { DB_Write PlcWrite = new DB_Write(); HK_PLC_S7.WriteClass(PlcWrite, 99); } catch (Exception ex) { MessageNotify.GetInstance.ShowRunLog("DB99块初始化值 失败"); } TaskManage.GetInstance.StartLong(new Action(() => {/* var res = HK_PLC_S7.ReadClass(97);//料仓里原料的编码 if (res != null && res is StockBinName data9) { GVL_BigStation.stockBinName = data9; }*/ if (IsConnected) { var res1 = HK_PLC_S7.ReadClass(98); var res2 = HK_PLC_S7.ReadClass(97);//料仓里原料的编码 if (res1 != null && res1 is DB_Read data1) { PlcRead = data1; GVL_BigStation.HKPlc_Read = data1; } if (res2 != null && res2 is StockBinName data2) { GVL_BigStation.stockBinName = data2; } var res3 = HK_PLC_S7.ReadClass(48); if (res3 != null && res3 is DL_DataColl_DB data) { DataColl = data; } } Thread.Sleep(10); }), "海科数据交互", true); } /// /// 往plc下发配方数据 /// public void WritePlcRecipeData(RecipeData recipe) { if (IsConnected) { if (recipe != null) { HK_PLC_S7.Write("DB99.DBW2.0", Convert.ToInt16(recipe.TrayCode)); HK_PLC_S7.Write("DB99.DBW4.0", Convert.ToInt16(recipe.TrayCode)); MessageNotify.GetInstance.ShowRunLog($"配方编号:{recipe.RecipeCode},托盘编号DB99.DBW2.0:{recipe.TrayCode},下发完成"); for (int barrel = 1; barrel < 6; barrel++) { if (barrel != 3)//桶的编号不为3 只为1,2,4,5号桶 { for (int loc = 1; loc < 15; loc++) { int index = Array.FindIndex(recipe.RawMaterial.ToArray(), p => p.RawMaterialBarrelNum == barrel && p.RawMaterialLocation == loc); int x = 0; if (barrel < 3) x = (barrel - 1) * 56 + 6; else if (barrel > 3) x = (barrel - 2) * 56 + 6; string address = "DB99.DBD" + (x + 4 * (loc - 1)); if (index == -1) { HK_PLC_S7.Write(address, 0); } else { HK_PLC_S7.Write(address, recipe.RawMaterial.ElementAt(index).RawMaterialWeight); MessageNotify.GetInstance.ShowRunLog($"配方编号:{recipe.RecipeCode},托盘编号:{recipe.TrayCode},桶号:{barrel},位置:{loc},重量:{recipe.RawMaterial.ElementAt(index).RawMaterialWeight}"); } } } } } //验证配方数据是否下发 Thread.Sleep(200); if (!ReadPlcRecipeData(recipe)) { App.Current.Dispatcher.Invoke(() => { MessageNotify.GetInstance.ShowDialog($"配方{recipe.RecipeCode},plc配方数据和西门子配方数据不一致!", DialogType.Error); }); } MessageNotify.GetInstance.ShowRunLog($"配方编号:{recipe.RecipeCode},配方数据验证成功"); } } /// /// 读取plc收到的配方数据 /// /// /// public bool ReadPlcRecipeData(RecipeData recipe) { if (IsConnected) { if (recipe != null) { ushort Code1 = HK_PLC_S7.Read("DB99.DBW2.0"); ushort Code2 = HK_PLC_S7.Read("DB99.DBW4.0"); if (recipe.TrayCode != Code1 || recipe.TrayCode != Code2) { MessageNotify.GetInstance.ShowRunLog($"下发的配方数据,DB99.DBW2.0:{recipe.TrayCode},DB99.DBW2.0:{recipe.TrayCode}"); return false; } MessageNotify.GetInstance.ShowRunLog($"配方编号:{recipe.RecipeCode},托盘编号:{recipe.TrayCode},下发完成"); for (int barrel = 1; barrel < 6; barrel++) { if (barrel != 3)//桶的编号不为3 只为1,2,4,5号桶 { for (int loc = 1; loc < 15; loc++) { int index = Array.FindIndex(recipe.RawMaterial.ToArray(), p => p.RawMaterialBarrelNum == barrel && p.RawMaterialLocation == loc); if (index == -1) { float weight0 = (float)0.0; float ReadWeight = (float)0.0; string Address = ""; switch (barrel) { case 1: Address = "DB99.DBD" + (6 + 4 * (loc - 1)); ReadWeight = HK_PLC_S7.Read(Address); break; case 2: Address = "DB99.DBD" + (62 + 4 * (loc - 1)); ReadWeight = HK_PLC_S7.Read(Address); break; case 4: Address = "DB99.DBD" + (118 + 4 * (loc - 1)); ReadWeight = HK_PLC_S7.Read(Address); break; case 5: Address = "DB99.DBD" + (174 + 4 * (loc - 1)); ReadWeight = HK_PLC_S7.Read(Address); break; default: break; } if (Math.Round(weight0, 1) != Math.Round(ReadWeight, 1)) { MessageNotify.GetInstance.ShowRunLog($"地址:{Address},配方重量为0,读取plc值{ReadWeight}"); return false; } } else { string Address = ""; float ReadWeight = (float)0.0; switch (barrel) { case 1: Address = "DB99.DBD" + (6 + 4 * (loc - 1)); ReadWeight = HK_PLC_S7.Read(Address); break; case 2: Address = "DB99.DBD" + (62 + 4 * (loc - 1)); ReadWeight = HK_PLC_S7.Read(Address); break; case 4: Address = "DB99.DBD" + (118 + 4 * (loc - 1)); ReadWeight = HK_PLC_S7.Read(Address); break; case 5: Address = "DB99.DBD" + (174 + 4 * (loc - 1)); ReadWeight = HK_PLC_S7.Read(Address); break; default: break; } MessageNotify.GetInstance.ShowRunLog($"配方编号:{recipe.RecipeCode},托盘编号:{recipe.TrayCode},桶号:{barrel},地址:{Address},重量:{ReadWeight}"); if (Math.Floor(recipe.RawMaterial.ElementAt(index).RawMaterialWeight) != Math.Floor(ReadWeight)) { return false; } } } } } return true; } return false; } return false; } } }