From f9b1e15c3b442afac913344fc5b7458d1577a253 Mon Sep 17 00:00:00 2001 From: yahaha Date: Mon, 7 Nov 2022 17:26:14 +0800 Subject: [PATCH] =?UTF-8?q?morkf=5F=E5=A2=9E=E5=8A=A0=E6=9C=BA=E6=A2=B0?= =?UTF-8?q?=E8=87=82=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BPASmartClient.Helper/ThreadManage.cs | 10 + BPASmartClient.Modbus/ModbusTcp.cs | 26 +- BPASmartClient.MorkF/Control_MorkF.cs | 405 +++++++++++------- BPASmartClient.MorkF/View/DebugView.xaml | 52 +-- .../ViewModel/DebugViewModel.cs | 3 +- 5 files changed, 304 insertions(+), 192 deletions(-) diff --git a/BPASmartClient.Helper/ThreadManage.cs b/BPASmartClient.Helper/ThreadManage.cs index 8ba59126..25f0cd24 100644 --- a/BPASmartClient.Helper/ThreadManage.cs +++ b/BPASmartClient.Helper/ThreadManage.cs @@ -190,6 +190,16 @@ namespace BPASmartClient.Helper return false; } + /// + /// 根据key获取取消状态,为start()开启的task在外部停止使用。 + /// + /// + /// + public bool IsCanncel(string key) + { + if(CancellationTokenSources.ContainsKey(guid + key)) return CancellationTokenSources[guid + key].IsCancellationRequested; + return false; + } } } diff --git a/BPASmartClient.Modbus/ModbusTcp.cs b/BPASmartClient.Modbus/ModbusTcp.cs index 05f138b3..2d21d09d 100644 --- a/BPASmartClient.Modbus/ModbusTcp.cs +++ b/BPASmartClient.Modbus/ModbusTcp.cs @@ -144,7 +144,7 @@ namespace BPASmartClient.Modbus } } - else if (address.ToUpper().Contains("VW") && address.Length >= 3) + else if ((address.ToUpper().Contains("VW") || address.ToUpper().Contains("VD")) && address.Length >= 3) { var res = address.Substring(2); if (res != null && int.TryParse(res, out int tempAddress)) @@ -246,6 +246,17 @@ namespace BPASmartClient.Modbus commandType = CommandType.Coils; return master.ReadCoils(slaveAddress, startAddress, len); } + else if (address.ToUpper().Contains("VD")) + { + commandType = CommandType.HoldingRegisters; + var var1 = master.ReadHoldingRegisters(slaveAddress, startAddress, len); + var var2 = master.ReadHoldingRegisters(slaveAddress, (ushort)(startAddress + 1), len); + int dest = 0; + dest |= (var2[0] & 0x0000ffff); + dest = (dest << 16) | (var1[0] & 0x0000ffff); + return dest; + + } else if (address.ToUpper().Contains("VW") || address.ToUpper().Contains("LW") || address.ToUpper().Contains("D")) { commandType = CommandType.HoldingRegisters; @@ -333,6 +344,19 @@ namespace BPASmartClient.Modbus else if (value is bool[] boolsValue) master.WriteMultipleCoils(slaveAddress, startAddress, boolsValue); } + else if (address.ToUpper().Contains("VD")) + { + commandType = CommandType.HoldingRegisters; + if (value is int intValue) + { + + var val1 = (UInt16)intValue; + var val2 = Convert.ToUInt16(intValue >> 16 & 0x0000ffff); + master.WriteSingleRegister(slaveAddress, startAddress, val1); + master.WriteSingleRegister(slaveAddress, (ushort)(startAddress + 1), val2); + } + + } else if (address.ToUpper().Contains("VW") || address.ToUpper().Contains("LW") || address.ToUpper().Contains("D")) { commandType = CommandType.HoldingRegisters; diff --git a/BPASmartClient.MorkF/Control_MorkF.cs b/BPASmartClient.MorkF/Control_MorkF.cs index fe38c9ca..4469134d 100644 --- a/BPASmartClient.MorkF/Control_MorkF.cs +++ b/BPASmartClient.MorkF/Control_MorkF.cs @@ -51,6 +51,13 @@ namespace BPASmartClient.MorkF /// private ConcurrentQueue StirFryGoodsQuenes = new ConcurrentQueue(); + private const String striConst = "炒锅{0}炒制{1}线程"; + + /// + /// 当前炒制菜品 + /// + private OrderLocInfo nowStirFryGood = null; + List resultorder = new List();//调试变量 /// @@ -58,7 +65,13 @@ namespace BPASmartClient.MorkF /// public override void DoMain() { - for(int i = 0;i< count;i++) + + WriteControl("VD836", 0); + WriteControl("VD840", 0); + Thread.Sleep(400); + WriteControl("M0.0", true); + + for (int i = 0;i< count;i++) { morkFs.Add(i, new GVL_MorkF()); DataParse(i);//数据解析 @@ -67,6 +80,8 @@ namespace BPASmartClient.MorkF CommandRegist();//调试 ServerInit(); DeviceProcessLogShow("MORKF 设备初始化完成"); + + ReadData(); } #region 调试代码 public void CommandRegist() @@ -75,6 +90,7 @@ namespace BPASmartClient.MorkF ActionManage.GetInstance.Register(PLCInite, "InitCommand"); ActionManage.GetInstance.Register(StartOrder, "StartOrder"); ActionManage.GetInstance.Register(StartLocalOrder, "StartLocalOrder"); + ActionManage.GetInstance.Register(StopLocalOrder, "StopLocalOrder"); #endregion #region 菜品库 @@ -317,7 +333,7 @@ namespace BPASmartClient.MorkF } foreach (SeasoningList seasoning in seasoningLists) { - FirePot1_Write(morkF.StartPassWay[seasoning.Loc], false); + FirePot1_Write(morkFs[num].StartPassWay[seasoning.Loc], false, num); Thread.Sleep(300); } @@ -585,6 +601,14 @@ namespace BPASmartClient.MorkF MessageLog.GetInstance.Show($"添加本地模拟的订单{Json.Data.LocalstirFryGoods.GoodsKey}"); } } + + private void StopLocalOrder() + { + //判断当前是否有炒制菜品 + if (nowStirFryGood == null) return; + + ThreadManage.GetInstance().StopTask(String.Format(striConst, fryIndex.ToString(), nowStirFryGood.GoodName), new Action(() => { Plc1Reset(fryIndex); })); + } #endregion public override void ResetProgram() @@ -606,41 +630,46 @@ namespace BPASmartClient.MorkF for (int i = 0; i < morkFs.Count; i++) { - GetStatus("LB50", new Action((bools) => + GetStatus("LB50", new Action((objects) => { if (!morkFs.ContainsKey(i)) { return; } + if(objects is bool[] bools) + { + morkFs[i].FryPot1_InitialComplete = bools[0]; + morkFs[i].FryPot1_HOBTPut = bools[1]; + morkFs[i].FryPot1_HOBTGet = bools[2]; + morkFs[i].FryPot1_MaterialIntoPot = bools[3]; + morkFs[i].OutFoodCompelete = bools[4]; + morkFs[i].CanOutFood = bools[5]; + morkFs[i].GetFoodCompelete = bools[6]; + morkFs[i].CanOutPotWashingWater = bools[7]; + morkFs[i].ArmOnOrigin = bools[8]; + morkFs[i].ArmOnWorking = bools[9]; + morkFs[i].PotOnOrigin = bools[10]; + } - morkFs[i].FryPot1_InitialComplete = bools[0]; - morkFs[i].FryPot1_HOBTPut = bools[1]; - morkFs[i].FryPot1_HOBTGet = bools[2]; - morkFs[i].FryPot1_MaterialIntoPot = bools[3]; - morkFs[i].OutFoodCompelete = bools[4]; - morkFs[i].CanOutFood = bools[5]; - morkFs[i].GetFoodCompelete = bools[6]; - morkFs[i].CanOutPotWashingWater = bools[7]; - morkFs[i].ArmOnOrigin = bools[8]; - morkFs[i].ArmOnWorking = bools[9]; - morkFs[i].PotOnOrigin = bools[10]; }), i); } for (int j = 0; j < morkFs.Count; j++) { - GetStatus("LB74", new Action((bools) => + GetStatus("LB74", new Action((objects) => { if (!morkFs.ContainsKey(j)) { return; } - for (int i = 0; i < 14; i++) + if (objects is bool[] bools) { - morkFs[j].PassWay1_Compelete[i] = bools[i]; + for (int i = 0; i < 14; i++) + { + morkFs[j].PassWay1_Compelete[i] = bools[i]; + } } - }), j); } for (int i = 0; i < morkFs.Count; i++) @@ -650,14 +679,70 @@ namespace BPASmartClient.MorkF return; } - GetStatus("LB90", new Action((bools) => + GetStatus("LB90", new Action((objects) => { - morkFs[i].AutoMode = bools[0]; + if (objects is bool[] bools) + { + morkFs[i].AutoMode = bools[0]; + } }), i); } + for (int j = 0; j < morkFs.Count; j++) + { + GetStatus("VD808", new Action((objects) => + { + if (!morkFs.ContainsKey(j)) + { + return; + } + + if (objects is object[] bools) + { + for (int i = 0; i < 1; i++) + { + var a = bools[i]; + } + } + + + }), j); + GetStatus("VD814", new Action((objects) => + { + if (!morkFs.ContainsKey(j)) + { + return; + } + + if (objects is object[] bools) + { + for (int i = 0; i < 1; i++) + { + var a = bools[i]; + } + } + + + }), j); + GetStatus("VD816", new Action((objects) => + { + if (!morkFs.ContainsKey(j)) + { + return; + } + + if (objects is object[] bools) + { + for (int i = 0; i < 1; i++) + { + var a = bools[i]; + } + } + + }), j); + } } private void ServerInit() @@ -905,161 +990,167 @@ namespace BPASmartClient.MorkF //待炒小炒队列出队列 if (StirFryGoodsQuenes.TryDequeue(out var res)) { - try - { - + //设置当前炒制菜品 + nowStirFryGood = res; //炒锅工作状态置为正在工作中 morkFs[i].FryWorking = true; //空闲炒锅入队列 morkFs[i].StirFryGoodsQuenes.Enqueue(res); - MessageLog.GetInstance.Show($"菜品{res.GoodName}开始炒制"); + MessageLog.GetInstance.Show($"炒锅{i}开始炒制菜品{res.GoodName}"); //开启线程进行炒制 ThreadManage.GetInstance().Start(new Action(() => { - foreach (var potActions in res.StirPotActions) + try { - switch (potActions.Actions) + foreach (var potActions in res.StirPotActions) { - case nameof(StirFryPotActionEnum.加热开启): - StartFire(i); - MessageLog.GetInstance.Show(potActions.Actions); - break; - case nameof(StirFryPotActionEnum.设置加热挡位1): - SetFire(new List { 1 }, i); - MessageLog.GetInstance.Show(potActions.Actions); - break; - case nameof(StirFryPotActionEnum.设置加热挡位2): - SetFire(new List { 2 }, i); - MessageLog.GetInstance.Show(potActions.Actions); - break; - case nameof(StirFryPotActionEnum.设置加热挡位3): - SetFire(new List { 3 }, i); - MessageLog.GetInstance.Show(potActions.Actions); - break; - case nameof(StirFryPotActionEnum.设置加热挡位4): - SetFire(new List { 4 }, i); - MessageLog.GetInstance.Show(potActions.Actions); - break; - case nameof(StirFryPotActionEnum.设置加热挡位5): - SetFire(new List { 5 }, i); - MessageLog.GetInstance.Show(potActions.Actions); - break; - case nameof(StirFryPotActionEnum.设置加热挡位6): - SetFire(new List { 6 }, i); - MessageLog.GetInstance.Show(potActions.Actions); - break; - case nameof(StirFryPotActionEnum.设置加热挡位7): - SetFire(new List { 7 }, i); - MessageLog.GetInstance.Show(potActions.Actions); - break; - case nameof(StirFryPotActionEnum.设置加热挡位8): - SetFire(new List { 8 }, i); - MessageLog.GetInstance.Show(potActions.Actions); - break; - case nameof(StirFryPotActionEnum.设置加热挡位9): - SetFire(new List { 9 }, i); - MessageLog.GetInstance.Show(potActions.Actions); - break; - case nameof(StirFryPotActionEnum.设置加热挡位10): - SetFire(new List { 10 }, i); - MessageLog.GetInstance.Show(potActions.Actions); - break; - case nameof(StirFryPotActionEnum.停止加热): - StopFire(i); - MessageLog.GetInstance.Show(potActions.Actions); - break; - case nameof(StirFryPotActionEnum.加调料): - OutSeasonings(potActions.SeasoningLists, i); - MessageLog.GetInstance.Show(potActions.Actions); + if (ThreadManage.GetInstance().IsCanncel(String.Format(striConst, i.ToString(), nowStirFryGood.GoodName))) + { break; - case nameof(StirFryPotActionEnum.取原料): - MessageLog.GetInstance.Show(potActions.Actions); - break; - case nameof(StirFryPotActionEnum.开启搅拌): - StartStir(i); - MessageLog.GetInstance.Show(potActions.Actions); - break; - case nameof(StirFryPotActionEnum.设置搅拌挡位1): - SetStir(new List { 1 }, i); - MessageLog.GetInstance.Show(potActions.Actions); - break; - case nameof(StirFryPotActionEnum.设置搅拌挡位2): - SetStir(new List { 2 }, i); - MessageLog.GetInstance.Show(potActions.Actions); - break; - case nameof(StirFryPotActionEnum.设置搅拌挡位3): - SetStir(new List { 3 }, i); - MessageLog.GetInstance.Show(potActions.Actions); - break; - case nameof(StirFryPotActionEnum.关闭搅拌): - StopStir(i); - MessageLog.GetInstance.Show(potActions.Actions); - break; - case nameof(StirFryPotActionEnum.出餐启动): - MessageLog.GetInstance.Show(potActions.Actions); - break; - case nameof(StirFryPotActionEnum.道菜启动): - OutFood(i); - break; - case nameof(StirFryPotActionEnum.炒制菜品): - Thread.Sleep(potActions.During * 1000); - break; - case nameof(StirFryPotActionEnum.搅拌臂原点位): - StirArmGoOrigin(i); - MessageLog.GetInstance.Show(potActions.Actions); - break; - case nameof(StirFryPotActionEnum.搅拌臂炒制位): - StirArmGoWork(i); - MessageLog.GetInstance.Show(potActions.Actions); - break; - case nameof(StirFryPotActionEnum.洗锅): - MessageLog.GetInstance.Show(potActions.Actions); - break; - default: - break; - + } + + switch (potActions.Actions) + { + case nameof(StirFryPotActionEnum.加热开启): + StartFire(i); + MessageLog.GetInstance.Show(potActions.Actions); + break; + case nameof(StirFryPotActionEnum.设置加热挡位1): + SetFire(new List { 1 }, i); + MessageLog.GetInstance.Show(potActions.Actions); + break; + case nameof(StirFryPotActionEnum.设置加热挡位2): + SetFire(new List { 2 }, i); + MessageLog.GetInstance.Show(potActions.Actions); + break; + case nameof(StirFryPotActionEnum.设置加热挡位3): + SetFire(new List { 3 }, i); + MessageLog.GetInstance.Show(potActions.Actions); + break; + case nameof(StirFryPotActionEnum.设置加热挡位4): + SetFire(new List { 4 }, i); + MessageLog.GetInstance.Show(potActions.Actions); + break; + case nameof(StirFryPotActionEnum.设置加热挡位5): + SetFire(new List { 5 }, i); + MessageLog.GetInstance.Show(potActions.Actions); + break; + case nameof(StirFryPotActionEnum.设置加热挡位6): + SetFire(new List { 6 }, i); + MessageLog.GetInstance.Show(potActions.Actions); + break; + case nameof(StirFryPotActionEnum.设置加热挡位7): + SetFire(new List { 7 }, i); + MessageLog.GetInstance.Show(potActions.Actions); + break; + case nameof(StirFryPotActionEnum.设置加热挡位8): + SetFire(new List { 8 }, i); + MessageLog.GetInstance.Show(potActions.Actions); + break; + case nameof(StirFryPotActionEnum.设置加热挡位9): + SetFire(new List { 9 }, i); + MessageLog.GetInstance.Show(potActions.Actions); + break; + case nameof(StirFryPotActionEnum.设置加热挡位10): + SetFire(new List { 10 }, i); + MessageLog.GetInstance.Show(potActions.Actions); + break; + case nameof(StirFryPotActionEnum.停止加热): + StopFire(i); + MessageLog.GetInstance.Show(potActions.Actions); + break; + case nameof(StirFryPotActionEnum.加调料): + OutSeasonings(potActions.SeasoningLists, i); + MessageLog.GetInstance.Show(potActions.Actions); + break; + case nameof(StirFryPotActionEnum.取原料): + MessageLog.GetInstance.Show(potActions.Actions); + break; + case nameof(StirFryPotActionEnum.开启搅拌): + StartStir(i); + MessageLog.GetInstance.Show(potActions.Actions); + break; + case nameof(StirFryPotActionEnum.设置搅拌挡位1): + SetStir(new List { 1 }, i); + MessageLog.GetInstance.Show(potActions.Actions); + break; + case nameof(StirFryPotActionEnum.设置搅拌挡位2): + SetStir(new List { 2 }, i); + MessageLog.GetInstance.Show(potActions.Actions); + break; + case nameof(StirFryPotActionEnum.设置搅拌挡位3): + SetStir(new List { 3 }, i); + MessageLog.GetInstance.Show(potActions.Actions); + break; + case nameof(StirFryPotActionEnum.关闭搅拌): + StopStir(i); + MessageLog.GetInstance.Show(potActions.Actions); + break; + case nameof(StirFryPotActionEnum.出餐启动): + MessageLog.GetInstance.Show(potActions.Actions); + break; + case nameof(StirFryPotActionEnum.道菜启动): + OutFood(i); + break; + case nameof(StirFryPotActionEnum.炒制菜品): + Thread.Sleep(potActions.During * 1000); + break; + case nameof(StirFryPotActionEnum.搅拌臂原点位): + StirArmGoOrigin(i); + MessageLog.GetInstance.Show(potActions.Actions); + break; + case nameof(StirFryPotActionEnum.搅拌臂炒制位): + StirArmGoWork(i); + MessageLog.GetInstance.Show(potActions.Actions); + break; + case nameof(StirFryPotActionEnum.洗锅): + MessageLog.GetInstance.Show(potActions.Actions); + break; + default: + break; + + } } - } - Plc1Reset(i);//复位 - Thread.Sleep(3000); - //回原点位 - StirArmGoOrigin(i); + Plc1Reset(i);//复位 + Thread.Sleep(3000); + //回原点位 + StirArmGoOrigin(i); - for (int i = 0; i < sleepCount && !morkFs[i].ArmOnOrigin; i++) - { - Thread.Sleep(sleepTime); - if (i >= sleepCount - 1) + for (int i = 0; i < sleepCount && !morkFs[i].ArmOnOrigin; i++) { - MessageLog.GetInstance.Show($"炒锅{i}炒制过程完成后,搅拌臂去原点位超时"); + Thread.Sleep(sleepTime); + if (i >= sleepCount - 1) + { + MessageLog.GetInstance.Show($"炒锅{i}炒制过程完成后,搅拌臂去原点位超时"); + } } - } - - //while (!morkFs[i].ArmOnOrigin) - //{ - // Thread.Sleep(100); - //} - //出餐 - //OutMeal(i); - MessageLog.GetInstance.Show($"菜品{res.GoodName}完成"); + //while (!morkFs[i].ArmOnOrigin) + //{ + // Thread.Sleep(100); + //} + //出餐 + //OutMeal(i); + MessageLog.GetInstance.Show($"菜品{res.GoodName}完成"); - }), $"炒锅{i}炒制线程"); - } - catch(Exception ex) - { - ThreadManage.GetInstance().StopTask($"炒锅{i}炒制线程"); - MessageLog.GetInstance.Show($"菜品{res.GoodName}出错,错误信息:" + ex.Message); - } - finally - { - //炒完后出队列 - morkFs[i].StirFryGoodsQuenes.TryDequeue(out var orderQueue); - morkFs[i].FryWorking = false; - } + } + catch (Exception ex) + { + ThreadManage.GetInstance().StopTask($"炒锅{i}{res.GoodName}炒制线程"); + MessageLog.GetInstance.Show($"炒锅{i}炒制菜品{res.GoodName}出错,错误信息:" + ex.Message); + } + finally + { + nowStirFryGood = null; + //炒完后出队列 + morkFs[i].StirFryGoodsQuenes.TryDequeue(out var orderQueue); + morkFs[i].FryWorking = false; + } + }), String.Format(striConst, i.ToString(), nowStirFryGood.GoodName)/*$"炒锅{i}炒制{res.GoodName}线程"*/); } } break; @@ -1167,13 +1258,13 @@ namespace BPASmartClient.MorkF /// /// /// 炒锅编号 - private void GetStatus(string key, Action action,int num) + private void GetStatus(string key, Action action,int num) { if(dicPort2peripheralStatus.ContainsKey(num)) { if (dicPort2peripheralStatus[num].ContainsKey(key)) { - action((bool[])dicPort2peripheralStatus[num][key]);//获取PLC指定地址的状态值 + action((object)dicPort2peripheralStatus[num][key]);//获取PLC指定地址的状态值 } } } diff --git a/BPASmartClient.MorkF/View/DebugView.xaml b/BPASmartClient.MorkF/View/DebugView.xaml index cfbb3f90..26a99eca 100644 --- a/BPASmartClient.MorkF/View/DebugView.xaml +++ b/BPASmartClient.MorkF/View/DebugView.xaml @@ -20,17 +20,16 @@ - + + - + - - - + @@ -39,41 +38,28 @@