using BPASmartClient.CustomResource.Pages.Model; using BPASmartClient.Helper; using BPASmartClient.DosingHKProject.Model.HK_PLC; using BPASmartClient.DosingHKProject.Model.Siemens; using BPASmartClient.Modbus; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Configuration; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using BPASmartClient.DosingProject; namespace BPASmartClient.DosingHKProject.Model { public class ProcessControl { private volatile static ProcessControl _Instance; public static ProcessControl GetInstance => _Instance ?? (_Instance = new ProcessControl()); private ProcessControl() { } /// /// 配方数据 /// public ObservableCollection RemoteRecipes = new ObservableCollection(); /// /// 原料的名称和料仓的位置对应 /// public Dictionary RawMaterialsNamePos = new Dictionary(); /// /// 配方队列 /// public ConcurrentQueue RecipeQueue = new ConcurrentQueue(); public ConcurrentQueue RecipeQueueTray2 = new ConcurrentQueue(); public HKDeviceStatus HKDevice = new HKDeviceStatus(); public void Init() { for (int i = 0; i < 16; i++) { if (DeviceInquire.GetInstance.GetDevice(i).DeviceName != null) { if (!RawMaterialsNamePos.ContainsKey(DeviceInquire.GetInstance.GetDevice(i).DeviceName)) { RawMaterialsNamePos.Add(DeviceInquire.GetInstance.GetDevice(i).DeviceName, (short)DeviceInquire.GetInstance.GetDevice(i).deviceStatus.DeviceNum); } } } string HK_PLC_IP = ConfigurationManager.AppSettings["HKPlc_IP"]; try { //HKDevice.HK_PLC_S7.Connect(S7.Net.CpuType.S71200, HK_PLC_IP); if (HKDevice.IsConnected) { HKDevice.Init(); MessageLog.GetInstance.ShowUserLog("海科plc连接成功,并初始化完成"); } } catch(Exception ex) { } RecipeQueue.Clear(); ThreadManage.GetInstance().StartLong(new Action(() => { ReceviceData(); RecipeInfoToHKPLC(); Thread.Sleep(10); }), "流程处理", true); ThreadManage.GetInstance().StartLong(new Action(() => { if (HKDevice.IsConnected) { GetStatus(); ManualOpen(); ManualClose(); } Thread.Sleep(10); }), "手动操作", true); } private void GetStatus() { for (int i = 0; i < 8; i++) { HKDevice.HK_PLC_S7.Read("DB5.DBX0." + i); } for (int i = 0; i < 8; i++) { HKDevice.HK_PLC_S7.Read("DB5.DBX1." + i); } } private void ManualOpen() { ActionManage.GetInstance.Register(new Action((o) => { if (o != null) { if (o.ToString().Contains("升降气缸")) { int index = Convert.ToInt16(o.ToString().Substring(o.ToString().Length - 1)); switch (index) { case 1: HKDevice.HK_PLC_S7.Write("DB5.DBX0.0", true); break; case 2: HKDevice.HK_PLC_S7.Write("DB5.DBX0.1", true); break; case 3: HKDevice.HK_PLC_S7.Write("DB5.DBX0.2", true); break; case 4: HKDevice.HK_PLC_S7.Write("DB5.DBX0.3", true); break; case 5: HKDevice.HK_PLC_S7.Write("DB5.DBX0.4", true); break; case 6: HKDevice.HK_PLC_S7.Write("DB5.DBX0.5", true); break; default: break; } } } }), "ManualOpen", true);//根据下发的配方ID将 托盘的位置信息添加到配方中 } private void ManualClose() { ActionManage.GetInstance.Register(new Action((o) => { if (o != null) { if (o.ToString().Contains("升降气缸")) { int index = Convert.ToInt16(o.ToString().Substring(o.ToString().Length - 1)); switch (index) { case 1: HKDevice.HK_PLC_S7.Write("DB5.DBX0.0", false); break; case 2: HKDevice.HK_PLC_S7.Write("DB5.DBX0.1", false); break; case 3: HKDevice.HK_PLC_S7.Write("DB5.DBX0.2", false); break; case 4: HKDevice.HK_PLC_S7.Write("DB5.DBX0.3", false); break; case 5: HKDevice.HK_PLC_S7.Write("DB5.DBX0.4", false); break; case 6: HKDevice.HK_PLC_S7.Write("DB5.DBX0.5", false); break; default: break; } } } }), "ManualClose", true);//根据下发的配方ID将 托盘的位置信息添加到配方中 } /// /// 将配方添加到配方队列中 /// private void ReceviceData() { RemoteRecipes = Json.Data.Recipes; if (RemoteRecipes.Count > 0) { foreach (var data in RemoteRecipes) { if (!(RecipeQueue.Contains(data.RecipeCode))) { RecipeQueue.Enqueue(data.RecipeCode); } } } else { RecipeQueue.Clear(); GVL_SmallStation.GetInstance.RecipeStatusID = 0; } } int cnt_sensor1 = 0; int cnt_sensor2 = 0; int cnt_sensor3 = 0; int cnt_sensor4 = 0; int cnt_sensor5 = 0; int cnt_sensor6 = 0; int cnt_sensor7 = 0; int cnt_sensor8 = 0; int barrel1 = 0; /// /// 执行配方队列中的第一个配方 /// private void RecipeInfoToHKPLC() { if (RecipeQueue.Count > 0) { int index = Array.FindIndex(RemoteRecipes.ToArray(), p => p.RecipeCode == RecipeQueue.ElementAt(0)); if (index >= 0 && index < RemoteRecipes.Count) { string code = RemoteRecipes.ElementAt(index).RecipeCode; string recipeName = RemoteRecipes.ElementAt(index).RecipeName; #region sensor处理 if (GVL_SmallStation.GetInstance.plcRead.Sensor_Load) { cnt_sensor1++; } else { if (cnt_sensor1 >= 50) { if (barrel1 >= 0 && barrel1 < GVL_SmallStation.MaxBarrelNum) { if (GVL_SmallStation.GetInstance.Barrel_Location[barrel1] == 0) { GVL_SmallStation.GetInstance.Barrel_Location[barrel1] = 1; barrel1++; } } else { //报错,超过最大的桶号数 } } cnt_sensor1 = 0; } if (GVL_SmallStation.GetInstance.plcRead.Sensor_Station1) { cnt_sensor2++; } else { if (cnt_sensor2 >= 50) { for (int i = 0; i < GVL_SmallStation.GetInstance.Barrel_Location.Length; i++) { if (GVL_SmallStation.GetInstance.Barrel_Location[i] == 1) { GVL_SmallStation.GetInstance.Barrel_Location[i] = 2; } } } cnt_sensor2 = 0; } if (GVL_SmallStation.GetInstance.plcRead.Sensor_Station2) { cnt_sensor3++; } else { if (cnt_sensor3 >= 50) { for (int i = 0; i < GVL_SmallStation.GetInstance.Barrel_Location.Length; i++) { if (GVL_SmallStation.GetInstance.Barrel_Location[i] == 2) { GVL_SmallStation.GetInstance.Barrel_Location[i] = 3; } } } cnt_sensor3 = 0; } if (GVL_SmallStation.GetInstance.plcRead.Sensor_Station3) { cnt_sensor4++; } else { if (cnt_sensor4 >= 50) { for (int i = 0; i < GVL_SmallStation.GetInstance.Barrel_Location.Length; i++) { if (GVL_SmallStation.GetInstance.Barrel_Location[i] == 3) { GVL_SmallStation.GetInstance.Barrel_Location[i] = 4; } } } cnt_sensor4 = 0; } if (GVL_SmallStation.GetInstance.plcRead.Sensor_Station4) { cnt_sensor5++; } else { if (cnt_sensor5 >= 50) { for (int i = 0; i < GVL_SmallStation.GetInstance.Barrel_Location.Length; i++) { if (GVL_SmallStation.GetInstance.Barrel_Location[i] == 4) { GVL_SmallStation.GetInstance.Barrel_Location[i] = 5; } } } cnt_sensor5 = 0; } if (GVL_SmallStation.GetInstance.plcRead.Sensor_Station5) { cnt_sensor6++; } else { if (cnt_sensor6 >= 50) { for (int i = 0; i < GVL_SmallStation.GetInstance.Barrel_Location.Length; i++) { if (GVL_SmallStation.GetInstance.Barrel_Location[i] == 5) { GVL_SmallStation.GetInstance.Barrel_Location[i] = 6; } } } cnt_sensor6 = 0; } if (GVL_SmallStation.GetInstance.plcRead.Sensor_Station6) { cnt_sensor7++; } else { if (cnt_sensor7 >= 50) { for (int i = 0; i < GVL_SmallStation.GetInstance.Barrel_Location.Length; i++) { if (GVL_SmallStation.GetInstance.Barrel_Location[i] == 6) { GVL_SmallStation.GetInstance.Barrel_Location[i] = 7; } } } cnt_sensor7 = 0; } if (GVL_SmallStation.GetInstance.plcRead.Sensor_Unload) { HKDevice.HK_PLC_S7.Write("DB2.DBX3.0", false); HKDevice.HK_PLC_S7.Write("DB2.DBX3.1", false); MessageLog.GetInstance.ShowRunLog($"下料桶堵料,线体不运行"); cnt_sensor8++; } else { if (cnt_sensor8 >= 50) { for (int i = 0; i < GVL_SmallStation.GetInstance.Barrel_Location.Length; i++) { if (GVL_SmallStation.GetInstance.Barrel_Location[i] == 7) { GVL_SmallStation.GetInstance.Barrel_Location[i] = 0; } } } cnt_sensor8 = 0; } #endregion for (int i = 0; i < GVL_SmallStation.GetInstance.Barrel_Location.Length; i++) { if (GVL_SmallStation.GetInstance.plcRead.Sensor_Station1 && GVL_SmallStation.GetInstance.Barrel_Location[i] == 1) { foreach (var item in RemoteRecipes.ElementAt(index).RawMaterial) { if ((item.RawMaterialLocation == i+1 || item.RawMaterialLocation == i+7) && item.IsDosingFinish == false && item.RawMaterialBarrelNum == i+1) { GVL_SmallStation.GetInstance.AllowDosing_Pos[i] = true; } } } } if (GVL_SmallStation.GetInstance.plcRead.Sensor_Station1 && GVL_SmallStation.GetInstance.Barrel_Location[0] == 1) { foreach (var item in RemoteRecipes.ElementAt(index).RawMaterial) { if ((item.RawMaterialLocation == 1 || item.RawMaterialLocation == 7) && item.IsDosingFinish == false && item.RawMaterialBarrelNum == 1) { GVL_SmallStation.GetInstance.AllowDosing_Pos[0] = true; } } } if (GVL_SmallStation.GetInstance.plcRead.Sensor_Station2 && GVL_SmallStation.GetInstance.Barrel_Location[1] == 1) { GVL_SmallStation.GetInstance.AllowDosing_Pos[1] = true; } if (GVL_SmallStation.GetInstance.plcRead.Sensor_Station3 && GVL_SmallStation.GetInstance.Barrel_Location[2] == 1) { GVL_SmallStation.GetInstance.AllowDosing_Pos[2] = true; } if (GVL_SmallStation.GetInstance.plcRead.Sensor_Station4 && GVL_SmallStation.GetInstance.Barrel_Location[3] == 1) { GVL_SmallStation.GetInstance.AllowDosing_Pos[3] = true; } if (GVL_SmallStation.GetInstance.plcRead.Sensor_Station5 && GVL_SmallStation.GetInstance.Barrel_Location[4] == 1) { GVL_SmallStation.GetInstance.AllowDosing_Pos[4] = true; } if (GVL_SmallStation.GetInstance.plcRead.Sensor_Station6 && GVL_SmallStation.GetInstance.Barrel_Location[5] == 1) { GVL_SmallStation.GetInstance.AllowDosing_Pos[5] = true; } foreach (var item in RemoteRecipes.ElementAt(index).RawMaterial) { } if (GVL_SmallStation.GetInstance.RecipeStatusID == 0) { if (RunInit() == 1) { GVL_SmallStation.GetInstance.RecipeStatusID = 1; } } if (GVL_SmallStation.GetInstance.RecipeStatusID == 1) { HKDevice.HK_PLC_S7.Write("DB2.DBX3.0", true); HKDevice.HK_PLC_S7.Write("DB2.DBX3.1", true); GVL_SmallStation.GetInstance.RecipeStatusID = 2; } if (HKDevice.HK_PLC_S7.Read("DB2.DBX3.0") && HKDevice.HK_PLC_S7.Read("DB2.DBX3.1") && GVL_SmallStation.GetInstance.RecipeStatusID == 2) { GVL_SmallStation.GetInstance.RecipeStatusID = 3; } if (GVL_SmallStation.GetInstance.RecipeStatusID == 3) { for (int i = 0; i < 6; i++) { } if (GVL_SmallStation.GetInstance.AllowDosing_Pos[0]) { int res = 1; MessageLog.GetInstance.ShowRunLog($"配方:{recipeName},1号桶,{res}料仓,允许配料"); if (res > 0 ) { int loc_index = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == res); float weight = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(loc_index).RawMaterialWeight; if (loc_index >= 0) { DeviceInquire.GetInstance.GetDevice((int)res)?.Start(weight);//根据料仓编号 启动并写入每个原料重量 GVL_SmallStation.GetInstance.StockInIsWork = (int)res; } GVL_SmallStation.GetInstance.DosingTray1 = true; MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},1号桶,{(int)res}号仓,配料完成"); } } else if(RTrig.GetInstance("DB3.DBX50.1").Start(HKDevice.HK_PLC_S7.Read("DB3.DBX50.1"))) { var res = HKDevice.HK_PLC_S7.Read("DB3.DBD14"); MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},2号桶,{res}料仓,允许配料"); if (res > 0 && res is float loc) { int loc_index = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == loc); float weight = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(loc_index).RawMaterialWeight; if (loc_index >= 0) { DeviceInquire.GetInstance.GetDevice((int)loc)?.Start(weight);//启动并写入每个原料重量 GVL_SmallStation.GetInstance.StockInIsWork = (int)loc; } GVL_SmallStation.GetInstance.DosingTray1 = true; MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},2号桶,{(int)loc}号仓,配料完成"); } } else if (RTrig.GetInstance("DB3.DBX50.2").Start(HKDevice.HK_PLC_S7.Read("DB3.DBX50.2"))) { var res = HKDevice.HK_PLC_S7.Read("DB3.DBD18"); MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},3号桶,{res}料仓,允许配料"); if (res > 0 && res is float loc) { int loc_index = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == loc); float weight = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(loc_index).RawMaterialWeight; if (loc_index >= 0) { DeviceInquire.GetInstance.GetDevice((int)loc)?.Start(weight);//启动并写入每个原料重量 GVL_SmallStation.GetInstance.StockInIsWork = (int)loc; } GVL_SmallStation.GetInstance.DosingTray1 = true; MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},3号桶,{(int)loc}号仓,配料完成"); } } else if (RTrig.GetInstance("DB3.DBX50.3").Start(HKDevice.HK_PLC_S7.Read("DB3.DBX50.3"))) { var res = HKDevice.HK_PLC_S7.Read("DB3.DBD22"); MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},4号桶,{res}料仓,允许配料"); if (res > 0 && res is float loc) { int loc_index = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == loc); float weight = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(loc_index).RawMaterialWeight; if (loc_index >= 0) { DeviceInquire.GetInstance.GetDevice((int)loc)?.Start(weight);//启动并写入每个原料重量 GVL_SmallStation.GetInstance.StockInIsWork = (int)loc; } GVL_SmallStation.GetInstance.DosingTray1 = true; MessageLog.GetInstance.ShowRunLog($"托盘1,配方:{recipeName},4号桶,{(int)loc}号仓,配料完成"); } } if (GVL_SmallStation.GetInstance.DosingTray1) { for (int i = 1; i < 16; i++) { if (RTrig.GetInstance("GetDeviceRunStatus").Start(DeviceInquire.GetInstance.GetDevice(i).deviceStatus.RunStatus == 3)) { MessageLog.GetInstance.ShowRunLog($"柔性味魔方,托盘1,配方:{recipeName},{i}号仓,配料完成"); int res = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == i); RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(res).Laying_Off_Weight = DeviceInquire.GetInstance.GetDevice(i).deviceStatus.CutWeightFeedback; DeviceInquire.GetInstance.GetDevice(i).StatusReset(); if (i >= 1 && i <= 8) { HKDevice.HK_PLC_S7.Write("DB4.DBX30." + (i - 1), true); } else if (i >= 9 && i <= 15) { HKDevice.HK_PLC_S7.Write("DB4.DBX31." + (i - 9), true); } GVL_SmallStation.GetInstance.DosingTray1 = false; } } } if (RTrig.GetInstance("配方配料完成").Start(HKDevice.HK_PLC_S7.Read("DB3.DBX1.1")) && (GVL_SmallStation.GetInstance.WindSendDosingFinish || !GVL_SmallStation.GetInstance.IsUseWindSend)) { var res = Json.Data.Recipes.FirstOrDefault(p => p.RecipeCode == code); MessageLog.GetInstance.ShowRunLog($"托盘1,配方{res.RecipeName},配料完成"); App.Current.Dispatcher.Invoke(() => { Json.Data.Recipes.Remove(res); }); RecipeQueue.TryDequeue(out code); HKDevice.HK_PLC_S7.Write("DB3.DBX1.1", false); GVL_SmallStation.GetInstance.RecipeStatusID = 0; } } } } } private int RunInit() { if (HKDevice.IsConnected) { try { HKPlcCommWrite signReset = new HKPlcCommWrite(); HKDevice.HK_PLC_S7.WriteClass(signReset, 2); return 1; } catch (Exception ex) { return 2; } }else { return 2; } } } }