|
- 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 ObservableCollection<RemoteRecipeRawMaterial> RawMaterials { get; set; } = new ObservableCollection<RemoteRecipeRawMaterial>();
- public SiemensDeviceStatus SiemensDevice = new SiemensDeviceStatus();
- public HKDeviceStatus HKDevice = new HKDeviceStatus();
- GVL_BigStation BigStation = new GVL_BigStation();
-
- /// <summary>
- /// 配方数据
- /// </summary>
- public ObservableCollection<RemoteRecipeData> RemoteRecipes = new ObservableCollection<RemoteRecipeData>();
- /// <summary>
- /// 配方数据
- /// </summary>
- public ObservableCollection<RemoteRecipeData> IssuedComplete = new ObservableCollection<RemoteRecipeData>();
- /// <summary>
- /// 配方队列
- /// </summary>
- public ConcurrentQueue<long> RecipeQueue = new ConcurrentQueue<long>();
- /// <summary>
- /// AGV到达工站队列
- /// </summary>
- public ConcurrentQueue<int> AGVToWorkStationQueue = new ConcurrentQueue<int>();
- /// <summary>
- /// 接收原料数据
- /// </summary>
- public RecipeRawMaterial RawMaterial;
- public void Init()
- {
- ActionManage.GetInstance.Register(new Action(() =>
- {
- if (SiemensDevice.IsConnected)
- {
- var res = SiemensDevice.Siemens_PLC_S7.Read<string>(SiemensCommAddress.RecipeName);
- var res1 = SiemensDevice.Siemens_PLC_S7.Read<uint>(SiemensCommAddress.RecipeID);
- var res2 = SiemensDevice.Siemens_PLC_S7.ReadClass<RecipeRawMaterial>(0, 0);
- if ((res != null && res is string recipeName) && (res1 != null && res1 is uint recipeID))
- {
- int index = Array.FindIndex(Json<RemoteRecipeDataColl>.Data.Recipes.ToArray(), p => p.RecipeCode == recipeID);
- if (index == -1)
- {
- RawMaterials.Clear();
- for (int i = 0; i < 15; i++)
- {
- RawMaterials.Add(new RemoteRecipeRawMaterial()
- {
- RawMaterialBarrelNum = res2.RawMaterialBarrelNum[i],
- RawMaterialLocation = res2.RawMaterialLocation[i],
- RawMaterialWeight = res2.RawMaterialWeight[i]
- });
- }
- Json<RemoteRecipeDataColl>.Data.Recipes.Add(new RemoteRecipeData()
- {
- RecipeName = recipeName,
- RecipeCode = recipeID,
- RawMaterial = RawMaterials
- });
- }
- else
- {
- MessageLog.GetInstance.AlarmLog("配方列表中存在该配方ID");
- }
- }
- }
- }), "西门子下发配方", true);
- ActionManage.GetInstance.Register(new Action(() =>
- {
- if (SiemensDevice.IsConnected)
- {
- var res = SiemensDevice.Siemens_PLC_S7.Read<int>(SiemensCommAddress.TrayLocationNum);
- var res1 = SiemensDevice.Siemens_PLC_S7.Read<int>(SiemensCommAddress.RecipeID);
- if (res != null && res is int TrayLocation && res1 != null && res1 is int recipeId)
- {
- int index = Array.FindIndex(RemoteRecipes.ToArray(), p => p.RecipeCode == recipeId);
- if (index >= 0 && index < RemoteRecipes.Count)
- {
- RemoteRecipes.ElementAt(index).TrayCode = TrayLocation;
- MessageLog.GetInstance.RunLog($"接收到AGV进站信号=>配方编码:{recipeId} 、托盘位置:{TrayLocation}");
- AGVToWorkStationQueue.Enqueue(recipeId);
- }
- }
- }
- }), "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.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(() =>
- {
- 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();
- }), "读取西门子和海科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(SiemensCommAddress.StationIsExistCar, (bool)
- HKDevice.HK_PLC_S7.Read<bool>(HKPlcCommAddress.StationIsExistCar));
- //检测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
- int RecipeCode = (int)RemoteRecipes.ElementAt(index).RecipeCode;
- if (TrayLocation > 0 && TrayLocation < 7)
- {
- AGV_Delivery(TrayLocation - 1);
- if (BigStation.AgvDeliveryPosition == 5)
- {
- BigStation.AgvDeliveryPosition = 0;
- //AGVToWorkStationQueue.TryDequeue(out RecipeCode);
- }
- AGV_Pick(TrayLocation - 1);
- if (BigStation.AgvPickUpPosition == 3)
- {
- BigStation.AgvPickUpPosition = 0;
- AGVToWorkStationQueue.TryDequeue(out RecipeCode);
- }
- }
- }
- }
- private void ReceviceData()
- {
- RemoteRecipes = Json<RemoteRecipeDataColl>.Data.Recipes;
- if (RemoteRecipes.Count > 0)
- {
- foreach (var data in RemoteRecipes)
- {
- if (!(RecipeQueue.Contains(data.RecipeCode)))
- RecipeQueue.Enqueue(data.RecipeCode);
- }
- }
- }
- ushort[] BarrelNum = new ushort[15];
- ushort[] Location = new ushort[15];
- ushort[] Weight = new ushort[15];
- 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)
- {
- long code = RemoteRecipes.ElementAt(index).RecipeCode;
- if (HKDevice.HK_PLC_S7.Read<bool>("M5001.0") is bool)//配方1是否允许下发配发
- {
- for (int i = 0; i < RemoteRecipes.ElementAt(index).RawMaterial.Count; i++)
- {
- BarrelNum[i] = (ushort)RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(i).RawMaterialBarrelNum;
- Location[i] = (ushort)RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(i).RawMaterialLocation;
- Weight[i] = (ushort)RemoteRecipes.ElementAt(index).RawMaterial.ElementAt(i).RawMaterialWeight;
- }
- HKDevice.StockBinPar(BarrelNum, Location, Weight);
- HKDevice.HK_PLC_S7.Write("M4001.0", 1);//配发下发完成,to plc
- HKDevice.HK_PLC_S7.Write("M5001.0", 0);//复位允许下发配方1信号
- //BigStation.IssueRecipeFinish = true;
- }
- if (RTrig.GetInstance("StockState").Start(HKDevice.HK_PLC_S7.Read<bool>(HKPlcCommAddress.RecipeDosingFinish) is bool))
- {
- HKDevice.RecipeDosingFinishReset();
- RecipeQueue.TryDequeue(out code);
- IssuedComplete.Add(RemoteRecipes.ElementAt(index));//将该配方添加到下
- Json<RemoteRecipeDataColl>.Data.Recipes.RemoveAt(index);//制作完成,移除当前配方
- }
- }
- }
- }
- 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)
- {
- GetSiemensStatus("", 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(Address, Value);
- }
- }
- private void WriteHKPLC(string Address, object Value)
- {
- HKDevice.HK_PLC_S7.Write(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;
- }
- }
- }
- }
|