|
- using BPASmartClient.CustomResource.Pages.Model;
- using BPASmartClient.Helper;
- using BPASmartClient.JXJFoodBigStation.Model.HK_PLC;
- using BPASmartClient.JXJFoodBigStation.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.JXJFoodBigStation.Model
- {
- public class ProcessControl
- {
- private volatile static ProcessControl _Instance;
- public static ProcessControl GetInstance => _Instance ?? (_Instance = new ProcessControl());
- private ProcessControl() { }
-
-
- public SiemensDeviceStatus SiemensDevice = new SiemensDeviceStatus();
- public HKDeviceStatus HKDevice = new HKDeviceStatus();
- GVL_BigStation BigStation = new GVL_BigStation();
-
- /// <summary>
- /// 配方数据
- /// </summary>
- public ObservableCollection<RecipeData> RemoteRecipes = new ObservableCollection<RecipeData>();
- /// <summary>
- /// 配方数据
- /// </summary>
- public ObservableCollection<RecipeData> IssuedComplete = new ObservableCollection<RecipeData>();
- /// <summary>
- /// 原料的名称和料仓的位置对应
- /// </summary>
- public Dictionary<string, short> RawMaterialsNamePos = new Dictionary<string, short>();
- /// <summary>
- /// 配方队列
- /// </summary>
- public ConcurrentQueue<string> RecipeQueue = new ConcurrentQueue<string>();
- /// <summary>
- /// AGV到达工站队列
- /// </summary>
- public ConcurrentQueue<string> AGVToWorkStationQueue = new ConcurrentQueue<string>();
- /// <summary>
- /// 接收原料数据
- /// </summary>
- public RecipeRawMaterial RawMaterial;
- public void Init()
- {
- ActionManage.GetInstance.Register(new Action<object>((res) =>
- {
- ObservableCollection<RawMaterial> RawMaterials = new ObservableCollection<RawMaterial>();
- if (SiemensDevice.IsConnected)
- {
- if (res != null && res is DL_Start_DB recipe)
- {
- RawMaterials.Clear();
- for (int i = 0; i < 15; i++)
- {
- if (RawMaterialsNamePos.ContainsKey(recipe.RecipeName))
- {
- RawMaterials.Add(new RawMaterial()
- {
- RawMaterialName = recipe.Material[i].Material_Name,
- RawMaterialBarrelNum = recipe.Material[i].Material_BarrelNum,
- RawMaterialWeight = recipe.Material[i].Material_Weight,
- RawMaterialLocation = (int)RawMaterialsNamePos[recipe.Material[i].Material_Name]
- });
- }
- else
- {
- //报警,配方的原料名称下发和设备不一致
- }
- }
- Json<RemoteRecipe>.Data.Recipes.Add(new RecipeData()
- {
- RecipeName = recipe.RecipeName,
- RecipeCode = recipe.RecipeCode,
- RawMaterial = RawMaterials,
- TrayCode = recipe.TrayCode
- });
- }
- }
- }), "SiemensSendRecipe", true);
- 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.S71500, 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)
- {
-
- }
- RecipeQueue.Clear();
- //Json<RemoteRecipeDataColl>.Data.Recipes = TestData.GetInstance.Recipes;//添加测试数据
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
- ReadPLCDeviceInfo();
- ReceviceData();
- RecipeInfoToHKPLC();
- Thread.Sleep(10);
- }), "流程处理", true);
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
- if (HKDevice.IsConnected && SiemensDevice.IsConnected)
- {
- AgvGetInOut();
- }
- Thread.Sleep(10);
- }), "AGV进站送取货", true);
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
- ReadSiemensCommData();
- ReadHKPLCCommData();
- Thread.Sleep(10);
- }), "读取西门子和海科PLC的数据", true);
- }
- /// <summary>
- /// AGV送货
- /// </summary>
- /// <param name="bitNum"></param>
- private void AGV_Delivery(int bitNum)
- {
- if ((ReadSiemens("M4002." + bitNum) is bool) && BigStation.AgvDeliveryPosition == 0)
- {
- BigStation.AgvDeliveryPosition = 1;
- WriteHKPLC("M4002." + bitNum, true);
- WriteSiemens("M4002." + bitNum, false);
- }
- else if (ReadHK("M5002." + bitNum) is bool && BigStation.AgvDeliveryPosition == 1)
- {
- BigStation.AgvDeliveryPosition = 2;
- WriteSiemens("M5002." + bitNum, true);
- WriteHKPLC("M5002." + bitNum, false);
- }
- else if (ReadSiemens("M4004." + bitNum) is bool && BigStation.AgvDeliveryPosition == 2)
- {
- BigStation.AgvDeliveryPosition = 3;
- WriteHKPLC("M4004." + bitNum, true);
- WriteSiemens("M4004." + bitNum, false);
- }
- else if (ReadHK("M5004." + bitNum) is bool && BigStation.AgvDeliveryPosition == 3)
- {
- BigStation.AgvDeliveryPosition = 4;
- WriteSiemens("M5004." + bitNum, true);
- WriteHKPLC("M5004." + bitNum, false);
- }
- else if (ReadSiemens("M4005." + bitNum) is bool && BigStation.AgvDeliveryPosition == 4)
- {
- BigStation.AgvDeliveryPosition = 5;
- WriteHKPLC("M4005." + bitNum, true);
- WriteSiemens("M4005." + bitNum, false);
- }
- else if (ReadHK("M5005." + bitNum) is bool && BigStation.AgvDeliveryPosition == 5)
- {
- WriteSiemens("M5005." + bitNum, true);
- WriteHKPLC("M5005." + bitNum, false);
- }
- }
- /// <summary>
- /// AGV取货
- /// </summary>
- /// <param name="bitNum"></param>
- private void AGV_Pick(int bitNum)
- {
- if (ReadSiemens("M4003" + bitNum) is bool && BigStation.AgvPickUpPosition == 0)
- {
- BigStation.AgvPickUpPosition = 1;
- WriteHKPLC("M4003." + bitNum, true);
- WriteSiemens("M4003." + bitNum, false);
- }
- if (ReadHK("M5003" + bitNum) is bool && BigStation.AgvPickUpPosition == 1)
- {
- BigStation.AgvPickUpPosition = 2;
- WriteSiemens("M5003" + bitNum, true);
- WriteHKPLC("M5003" + bitNum, false);
- }
- if (ReadSiemens("M4006." + bitNum) is bool && BigStation.AgvPickUpPosition == 2)
- {
- BigStation.AgvPickUpPosition = 3;
- WriteHKPLC("M4006." + bitNum, true);
- WriteSiemens("M4006." + bitNum, false);
- }
- if (ReadHK("M5006." + bitNum) is bool && BigStation.AgvPickUpPosition == 3)
- {
- WriteSiemens("M5006." + bitNum, true);
- WriteHKPLC("M5006." + bitNum, false);
- }
- }
- /// <summary>
- /// AGV进站送货
- /// </summary>
- private void AgvGetInOut()
- {
- //获取工位上是否有小车
- SiemensDevice.Siemens_PLC_S7.Write<bool>(SiemensCommAddress.StationIsExistCar, (bool)
- HKDevice.HK_PLC_S7.Read<bool>("M4007.0"));
- //检测AGV到站信号
- if (AGVToWorkStationQueue.Count > 0)
- {
- int index = Array.FindIndex(RemoteRecipes.ToArray(), p => p.RecipeCode == AGVToWorkStationQueue.ElementAt(0));
- int TrayLocation = RemoteRecipes.ElementAt(index).TrayCode;//根据配方编号,找到托盘的ID 托盘ID1-6
- string RecipeCode = (string)RemoteRecipes.ElementAt(index).RecipeCode;
- if (TrayLocation > 0 && TrayLocation < 7)
- {
- AGV_Delivery(TrayLocation - 1);
- if (BigStation.AgvDeliveryPosition == 5)
- {
- BigStation.AgvDeliveryPosition = 0;
- }
- AGV_Pick(TrayLocation - 1);
- if (BigStation.AgvPickUpPosition == 3)
- {
- BigStation.AgvPickUpPosition = 0;
- AGVToWorkStationQueue.TryDequeue(out RecipeCode);
- }
- }
- }
- }
- private void ReceviceData()
- {
- if (!BigStation.IsAllowManual && RemoteRecipes.Count == 0)//一个配方执行完成后,再获取配方数据
- {
- RemoteRecipes = Json<RemoteRecipe>.Data.Recipes;
- }
- else if (BigStation.IsAllowManual && RemoteRecipes.Count == 0)
- {
- RemoteRecipes = Json<LocalRecipeDataColl>.Data.Recipes;
- }
- if (RemoteRecipes.Count > 0)
- {
- foreach (var data in RemoteRecipes)
- {
- if (!(RecipeQueue.Contains(data.RecipeCode)))
- RecipeQueue.Enqueue(data.RecipeCode);
- }
- }
- else
- {
- RecipeQueue.Clear();
- BigStation.RecipeDosingStatus = 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;
- int trayCode = RemoteRecipes.ElementAt(index).TrayCode;
- if (RTrig.GetInstance("M5001.0").Start(HKDevice.HK_PLC_S7.Read<bool>("M5001.0")) && BigStation.RecipeDosingStatus == 0)//配方1是否允许下发配发
- {
- HKDevice.StockBinPar(RemoteRecipes.ElementAt(index));
- HKDevice.HK_PLC_S7.Write<bool>("M4001.0", true);//配发下发完成,to plc
- HKDevice.HK_PLC_S7.Write<bool>("M5001.0", false);//复位允许下发配方1信号
- BigStation.RecipeDosingStatus = 1;
- MessageLog.GetInstance.ShowRunLog($"配方状态:{code}下发完成");
- }
- if (HKDevice.HK_PLC_S7.Read<bool>("M5001.4") && BigStation.RecipeDosingStatus == 1)
- {
- BigStation.RecipeDosingStatus = 2;
- HKDevice.HK_PLC_S7.Write<bool>("M5001.4", false);//
- MessageLog.GetInstance.ShowRunLog($"配方状态:{code}配方配料");
- }
- if (BigStation.RecipeDosingStatus == 2)
- {
- for (int i = 0; i < 12; i++)
- {
- if (i >= 0 && i < 8)
- {
- if (HKDevice.HK_PLC_S7.Read<bool>("M5008." + (i)))//根据配料完成信号,
- {
- int a = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == (i+1));
- if (a >= 0)
- {
- int barrelNum = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(a).RawMaterialBarrelNum;
- string address = "MD" + 5060 + i * 4 + (barrelNum - 1) * 48;
- RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(a).Laying_Off_Weight = HKDevice.HK_PLC_S7.Read<float>(address);//料仓配料完成后的出料重量
- }
- }
- }
- else
- {
- if (HKDevice.HK_PLC_S7.Read<bool>("M5009." + (i - 8)))
- {
- int a = Array.FindIndex(RemoteRecipes.ElementAt(index).RawMaterial.ToArray(), p => p.RawMaterialLocation == i);
- if (a >= 0)
- {
- int barrelNum = RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(a).RawMaterialBarrelNum;
- string address = "MD" + 5060 + i * 4 + (barrelNum - 1) * 48;
- RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(a).Laying_Off_Weight = HKDevice.HK_PLC_S7.Read<float>(address);
- }
- }
- }
- }
- if (RTrig.GetInstance("StockState").Start(HKDevice.HK_PLC_S7.Read<bool>("M5007.0")))
- {
- BigStation.RecipeDosingStatus = 3;
- MessageLog.GetInstance.ShowRunLog($"配方状态:{code}配料完成");
- HKDevice.HK_PLC_S7.Write<bool>("M5007.0",false);
- RecipeQueue.TryDequeue(out code);
- IssuedComplete.Add(RemoteRecipes.ElementAt(index));//将该配方添加到下
- if (!BigStation.IsAllowManual)
- {
- Json<RemoteRecipe>.Data.Recipes.RemoveAt(index);//制作完成,移除当前配方
- }
- else
- {
- Json<LocalRecipeDataColl>.Data.Recipes.RemoveAt(index);//制作完成,移除当前配方
- }
- BigStation.RecipeDosingStatus = 0;
- }
- }
- }
- }
- }
- public ConcurrentDictionary<string, Object> ReadHKPLCData = new ConcurrentDictionary<string, object>();
- public ConcurrentDictionary<string, Object> ReadSiemensData = new ConcurrentDictionary<string, object>();
-
-
- private void ReadSiemensCommData()
- {
- if (SiemensDevice.IsConnected)
- {
- GetSiemensStatus("", new Action<object>((obj) =>
- {
- if (obj is bool[] bools && bools.Length > 0)
- {
-
- }
- }));
- }
- }
- private void ReadHKPLCCommData()
- {
- if (HKDevice.IsConnected)
- {
- GetHKStatus("", new Action<object>((obj) =>
- {
- if (obj is bool[] bools && bools.Length > 0)
- {
-
- }
- }));
- }
- }
- private void GetHKStatus(string key, Action<object> action)
- {
- if (ReadHKPLCData.ContainsKey(key))
- {
- if (ReadHKPLCData[key] != null)
- {
- action?.Invoke(ReadHKPLCData[key]);
- }
- }
- }
- private void GetSiemensStatus(string key, Action<object> action)
- {
-
- if (ReadSiemensData.ContainsKey(key))
- {
- if (ReadSiemensData[key] != null)
- {
- action?.Invoke(ReadSiemensData[key]);
- }
- }
- }
- private void WriteSiemens(string Address, object Value)
- {
- if (SiemensDevice.IsConnected)
- {
- SiemensDevice.Siemens_PLC_S7.Write<object>(Address, Value);
- }
- }
- private void WriteHKPLC(string Address, object Value)
- {
- HKDevice.HK_PLC_S7.Write<object>(Address, Value);
- }
- private object ReadSiemens(string Address)
- {
- if (SiemensDevice.IsConnected)
- {
- return SiemensDevice.Siemens_PLC_S7.Read<object>(Address);
- }
- else
- {
- return null;
- }
- }
- private bool[] ReadBoolsSiemens(int address, int count)
- {
- if (SiemensDevice.IsConnected)
- {
- return SiemensDevice.Siemens_PLC_S7.ReadBools(address, count);
- }
- else
- {
- return null;
- }
- }
- private object ReadHK(string Address)
- {
- if (HKDevice.IsConnected)
- {
- return HKDevice.HK_PLC_S7.Read<object>(Address);
- }
- else
- {
- return null;
- }
- }
- private bool[] ReadBoolsHK(int address, int count)
- {
- if (HKDevice.IsConnected)
- {
- return HKDevice.HK_PLC_S7.ReadBools(address, count);
- }
- else
- {
- return null;
- }
- }
- /// <summary>
- /// 获取料仓的原料名称和原料位置
- /// </summary>
- private void ReadPLCDeviceInfo()
- {
- for (int i = 0; i < 12; i++)
- {
- string RawMaterialName = HKDevice.HK_PLC_S7.Read<string>("");
- short RawMaterialLocation = HKDevice.HK_PLC_S7.Read<short>("");
- if (RawMaterialsNamePos.ContainsKey(RawMaterialName))
- RawMaterialsNamePos.Add(RawMaterialName, RawMaterialLocation);
- }
- }
- }
- }
|