using BPASmartClient.Helper; using BPASmartClient.JXJFoodSmallStation.Model.HK_PLC; using BPASmartClient.JXJFoodSmallStation.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; namespace BPASmartClient.JXJFoodSmallStation.Model { public class ProcessControl { private volatile static ProcessControl _Instance; public static ProcessControl GetInstance => _Instance ?? (_Instance = new ProcessControl()); private ProcessControl() { } public ObservableCollection RawMaterials { get; set; } = new ObservableCollection(); public SiemensDeviceStatus SiemensDevice = new SiemensDeviceStatus(); public HKDeviceStatus HKDevice = new HKDeviceStatus(); GVL_SmallStation SmallStation = new GVL_SmallStation(); /// /// 接收原料数据 /// public RecipeRawMaterial RawMaterial; public void Init() { ActionManage.GetInstance.Register(new Action(() => { if (SiemensDevice.IsConnected) { var res = SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.RecipeName); var res1 = SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.RecipeID); int res2 = SiemensDevice.Siemens_PLC_S7.ReadClass(RawMaterial, 0, 0); if ((res != null && res is string recipeName) && (res1 != null && res1 is uint recipeID) && (res2 > 0)) { RawMaterials.Clear(); for (int i = 0; i < 15; i++) { RawMaterials.Add(new RemoteRecipeRawMaterial() { RawMaterialBarrelNum = RawMaterial.RawMaterialBarrelNum[i], RawMaterialLocation = RawMaterial.RawMaterialLocation[i], RawMaterialWeight = RawMaterial.RawMaterialWeight[i] }); } Json.Data.Recipes.Add(new RemoteRecipeData() { RecipeName = recipeName, RecipeCode = recipeID, RawMaterial = RawMaterials }); } } }), "西门子下发配方", true); ActionManage.GetInstance.Register(new Action(() => { if (SiemensDevice.IsConnected) { var res = SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.TrayLocationNum); var res1 = SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.RecipeID); if (res != null && res is int TrayLocation && res1 != null && res1 is int recipeId) { int index = Array.FindIndex(Json.Data.Recipes.ToArray(), p => p.RecipeCode == recipeId); if (index >= 0 && index < Json.Data.Recipes.Count) { Json.Data.Recipes.ElementAt(index).TrayCode = TrayLocation; } } } }), "AGV到位信号", true);//根据下发的配方ID将 托盘的位置信息添加到配方中 string HK_PLC_IP = ConfigurationManager.AppSettings["HKPlc_IP"]; string Siemens_PLC_IP = ConfigurationManager.AppSettings["Siemens_IP"]; try { //HKDevice.HK_PLC_S7.Connect(S7.Net.CpuType.S7200Smart, HK_PLC_IP); //SiemensDevice.Siemens_PLC_S7.Connect(S7.Net.CpuType.S71500, Siemens_PLC_IP); if (HKDevice.IsConnected) { HKDevice.Init(); } if (SiemensDevice.IsConnected) { SiemensDevice.Init(); } } catch(Exception ex) { } SmallStation.RecipeQueue.Clear(); Json.Data.Recipes = TestData.GetInstance.Recipes;//添加测试数据 ThreadManage.GetInstance().StartLong(new Action(() => { ReceviceData(); RecipeInfoToHKPLC(); Thread.Sleep(10); }), "流程处理", true); ThreadManage.GetInstance().StartLong(new Action(() => { if (SiemensDevice.IsConnected && HKDevice.IsConnected) { AgvGetInDelivery(); AgvGetInPickUp(); } Thread.Sleep(10); }), "AGV进站送取货", true); } /// /// AGV进站送货 /// private void AgvGetInDelivery() { switch (SmallStation.AgvDeliveryPosition) { case 0: if (RTrig.GetInstance("").Start(SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.DeliveryAGVApply) is bool)) { SmallStation.AgvDeliveryPosition = 1; HKDevice.HK_PLC_S7.Write(HKPlcCommAddress.DeliveryAGVApply, true); } break; case 1: if (RTrig.GetInstance("").Start(HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.DeliveryAGVIsApply) is bool)) { SmallStation.AgvDeliveryPosition = 2; SiemensDevice.Siemens_PLC_S7.Write(SiemensCommAddress.DeliveryAGVIsApply, true); } break; case 2: if (RTrig.GetInstance("").Start(SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.DeliveryAGVApplyJack) is bool)) { SmallStation.AgvDeliveryPosition = 3; HKDevice.HK_PLC_S7.Write(HKPlcCommAddress.DeliveryAGVApplyJack, true); } break; case 3: if (RTrig.GetInstance("").Start(HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.DeliveryAGVIsApplyJack) is bool)) { SmallStation.AgvDeliveryPosition = 4; SiemensDevice.Siemens_PLC_S7.Write(SiemensCommAddress.DeliveryAGVIsApplyJack, true); } break; case 4: if (RTrig.GetInstance("").Start(SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.DeliveryAGVFinsih) is bool)) { SmallStation.AgvDeliveryPosition = 5; HKDevice.HK_PLC_S7.Write(HKPlcCommAddress.DeliveryAGVFinsih, true); } break; case 5: if (RTrig.GetInstance("").Start(HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.StationHaveCargo) is bool)) { SmallStation.AgvDeliveryPosition = 0; SiemensDevice.Siemens_PLC_S7.Write(SiemensCommAddress.StationHaveCargo, true); } break; default: break; } //获取工位上是否有小车 SiemensDevice.Siemens_PLC_S7.Write(SiemensCommAddress.StationIsExistCar, (bool) HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.StationIsExistCar)); } /// /// AGV进站取货 /// private void AgvGetInPickUp() { switch (SmallStation.AgvPickUpPosition) { case 0: if (RTrig.GetInstance("").Start(SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.PickAGVApply) is bool)) { SmallStation.AgvPickUpPosition = 1; HKDevice.HK_PLC_S7.Write(HKPlcCommAddress.PickAGVApply, true); } break; case 1: if (RTrig.GetInstance("").Start(HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.PickAGVIsApply) is bool)) { SmallStation.AgvPickUpPosition = 2; SiemensDevice.Siemens_PLC_S7.Write(SiemensCommAddress.PickAGVIsApply, true); } break; case 2: if (RTrig.GetInstance("").Start(SiemensDevice.Siemens_PLC_S7.Read(SiemensCommAddress.PickCargoAGVFinish) is bool)) { SmallStation.AgvPickUpPosition = 3; HKDevice.HK_PLC_S7.Write(HKPlcCommAddress.PickCargoAGVFinish, true); } break; case 3: if (RTrig.GetInstance("").Start(HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.PickAGVFinish) is bool)) { SmallStation.AgvPickUpPosition = 0; SiemensDevice.Siemens_PLC_S7.Write(SiemensCommAddress.PickAGVFinish, true); } break; default: break; } } private void ReceviceData() { SmallStation.RemoteRecipes = Json.Data.Recipes; if (Json.Data.Recipes.Count > 0) { foreach (var data in Json.Data.Recipes) { if(!(SmallStation.RecipeQueue.Contains(data.RecipeCode))) SmallStation.RecipeQueue.Enqueue(data.RecipeCode); } } } private void RecipeInfoToHKPLC() { if (SmallStation.RecipeQueue.Count > 0) { int index = Array.FindIndex(SmallStation.RemoteRecipes.ToArray(), p => p.RecipeCode == SmallStation.RecipeQueue.ElementAt(0)); if (index >= 0 && index <= SmallStation.RemoteRecipes.Count) { long code = SmallStation.RemoteRecipes.ElementAt(index).RecipeCode; if (RTrig.GetInstance("IsAllowDosing").Start(HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.IsAllowDosing) is bool a)) { foreach (var item in SmallStation.RemoteRecipes.ElementAt(index).RawMaterial) { HKDevice.StockBinPar((uint)item.RawMaterialBarrelNum, (ushort)item.RawMaterialLocation); } HKDevice.AllowDosingSignReset(); SmallStation.IssueRecipeFinish = true; } if (RTrig.GetInstance("StockState").Start(HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.StockState) is bool) && SmallStation.IssueRecipeFinish) { var res = HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.StockBinLocation); if (res != null && res is int loc) { int loc_index = Array.FindIndex(SmallStation.RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == loc); double weight = SmallStation.RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(loc_index).RawMaterialWeight; if (loc_index >= 1 && loc_index <= 15) { DeviceInquire.GetInstance.GetDeviceObject(loc_index)?.Start((uint)weight);//启动并写入每个原料重量 SmallStation.StockInIsWork = loc_index; HKDevice.StockStateSignReset(); } } } if (RTrig.GetInstance("OutRawMaterialFinish").Start(DeviceInquire.GetInstance.GetDeviceObject(SmallStation.StockInIsWork).modbusTcp.Read(DeviceAddress.OutRawMaterialFinish) is bool)) { HKDevice.SingleDosing((uint)SmallStation.StockInIsWork); DeviceInquire.GetInstance.GetDeviceObject(SmallStation.StockInIsWork).OutRawMaterialReset(); } if (RTrig.GetInstance("StockState").Start(HKDevice.HK_PLC_S7.Read(HKPlcCommAddress.RecipeDosingFinish) is bool)) { HKDevice.RecipeDosingFinishReset(); SmallStation.RecipeQueue.TryDequeue(out code); Json.Data.Recipes.RemoveAt(index);//制作完成,移除当前配方 } } } } } }