From 604667d2aa017fbf1e00ebe585d5022976cbb3e7 Mon Sep 17 00:00:00 2001 From: lxw Date: Fri, 24 May 2024 16:12:38 +0800 Subject: [PATCH] PlcMgr --- Entity/Constant/CloudEnum.cs | 3 +++ Service/EquipmentInit.cs | 13 ++++----- Service/Execute/Api/CloudApi.cs | 2 +- Service/Execute/Api/PlcApi.cs | 38 +++++++++++---------------- Service/Execute/Step/SwapDoneState.cs | 8 +++--- Service/Plc/PlcMgr.cs | 27 +++++++++++++++++++ 6 files changed, 58 insertions(+), 33 deletions(-) create mode 100644 Service/Plc/PlcMgr.cs diff --git a/Entity/Constant/CloudEnum.cs b/Entity/Constant/CloudEnum.cs index 82554bb..0a261be 100644 --- a/Entity/Constant/CloudEnum.cs +++ b/Entity/Constant/CloudEnum.cs @@ -1,5 +1,8 @@ namespace Entity.Constant; +/// +/// +/// public class CloudEnum { diff --git a/Service/EquipmentInit.cs b/Service/EquipmentInit.cs index b708a5b..23bf3a3 100644 --- a/Service/EquipmentInit.cs +++ b/Service/EquipmentInit.cs @@ -5,6 +5,7 @@ using HybirdFrameworkServices.Plc; using Newtonsoft.Json.Linq; using Service.Execute.Api; using Service.Init.Entity; +using Service.Plc; namespace Service { @@ -55,8 +56,7 @@ namespace Service if (connected) { - PlcApi.master = master; - PlcApi.con = true; + PlcMgr.ModbusTcpMaster = master; break; } @@ -71,28 +71,29 @@ namespace Service { if (PlcApi.con) { + + var bytes01 = master.BatchReadHolderRegister(1, 115); if (bytes01 != null) { PlcApi.DataValidityTime = DateTime.Now; - ModbusDecoder.Decode(bytes01, PlcApi.hostToPlc); + ModbusDecoder.Decode(bytes01, PlcMgr.HostToPlcData); } var bytes02 = master.BatchReadHolderRegister(201, 222); if (bytes02 != null) { - ModbusDecoder.Decode(bytes02, PlcApi.plcToHost); + ModbusDecoder.Decode(bytes02, PlcMgr.PlcToHostData); } var bytes03 = master.BatchReadHolderRegister(701, 10); if (bytes03 != null) { PlcApi.DataValidityTime = DateTime.Now; - ModbusDecoder.Decode(bytes03, PlcApi.plcFault); + ModbusDecoder.Decode(bytes03, PlcMgr.PlcFaultData); } //OperateResult result2 = ModbusTcpNet.Read("x=3;201", 222); } - } } } \ No newline at end of file diff --git a/Service/Execute/Api/CloudApi.cs b/Service/Execute/Api/CloudApi.cs index 164f620..377670e 100644 --- a/Service/Execute/Api/CloudApi.cs +++ b/Service/Execute/Api/CloudApi.cs @@ -11,7 +11,7 @@ namespace Service.Execute.Api; public class CloudApi { - private const int TimeSpan = 30; + private const int TimeSpan = 60; /// /// 云平台车辆认证 diff --git a/Service/Execute/Api/PlcApi.cs b/Service/Execute/Api/PlcApi.cs index fc0d445..6c7cace 100644 --- a/Service/Execute/Api/PlcApi.cs +++ b/Service/Execute/Api/PlcApi.cs @@ -3,15 +3,14 @@ using HybirdFrameworkServices.Plc; using OfficeOpenXml.FormulaParsing.Excel.Functions.Text; using Service.Cloud.Msg.Cloud.Req; using Service.Init.Entity; +using Service.Plc; namespace Service.Execute.Api; public class PlcApi { - public static HostToPlc hostToPlc = new HostToPlc(); - public static PlcToHost plcToHost = new PlcToHost(); - public static PlcFault plcFault = new PlcFault(); - public static ModbusTcpMaster master; + private const int TimeSpan = 60; + /// /// PLC是否连接 @@ -36,7 +35,7 @@ public class PlcApi { if ((DateTime.Now - DataValidityTime).Seconds <= DataTimeSeconds) { - return plcToHost.RemoteLocalControlState.Value == 1010 ? true : false; + return PlcMgr.PlcToHostData.RemoteLocalControlState.Value == 1010 ? true : false; } else { @@ -52,7 +51,7 @@ public class PlcApi { if ((DateTime.Now - DataValidityTime).Seconds <= DataTimeSeconds) { - return plcToHost.RadarStatesIn.Value == 1000 ? true : false; + return PlcMgr.PlcToHostData.RadarStatesIn.Value == 1000 ? true : false; } else { @@ -68,7 +67,7 @@ public class PlcApi { if ((DateTime.Now - DataValidityTime).Seconds <= DataTimeSeconds) { - return plcToHost.RadarStatesOut.Value == 1000 ? true : false; + return PlcMgr.PlcToHostData.RadarStatesOut.Value == 1000 ? true : false; } else { @@ -85,13 +84,8 @@ public class PlcApi /// public static bool WriteEntranceLamp(int data) { - if (master != null) - { - HostToPlc writeHostToPlc = new HostToPlc(); - writeHostToPlc.LightIn.Value = (ushort)data; - return master.WriteValue(writeHostToPlc.LightIn); - } - return false; + return PlcMgr.WriteEntranceLamp((byte)data); + } /// @@ -103,12 +97,12 @@ public class PlcApi /// public static bool WriteExistLamp(int data) { - if (master != null) + /*if (master != null) { HostToPlc writeHostToPlc = new HostToPlc(); writeHostToPlc.LightOut.Value = (ushort)data; return master.WriteValue(writeHostToPlc.LightIn); - } + }*/ return false; } @@ -120,7 +114,7 @@ public class PlcApi /// public static bool DistributeSelectPack(string inBinNo, string outBinNo) { - if (master != null) + /*if (master != null) { HostToPlc writeHostToPlc = new HostToPlc(); writeHostToPlc.EntrySelection.Value = Convert.ToUInt16(inBinNo); @@ -128,7 +122,7 @@ public class PlcApi bool write1 = master.WriteValue(writeHostToPlc.EntrySelection); bool write2 = master.WriteValue(writeHostToPlc.ExitSelection); return write1 && write2; - } + }*/ return false; } @@ -139,7 +133,7 @@ public class PlcApi /// public static bool StartSwapping() { - if (master != null) + /*if (master != null) { if (plcToHost.TaskType.Value == 0)//是否允许换电 return false; @@ -150,7 +144,7 @@ public class PlcApi bool write1 = master.WriteValue(writeHostToPlc.TaskType); bool write2 = master.WriteValue(writeHostToPlc.TaskEnablement); return write1 && write2; - } + }*/ return false; } @@ -170,10 +164,10 @@ public class PlcApi /// 6未连接 public static int ReadPlcTaskStatus() { - if (master != null) + /*if (master != null) { return plcToHost.TaskType.Value; - } + }*/ return (-1); } diff --git a/Service/Execute/Step/SwapDoneState.cs b/Service/Execute/Step/SwapDoneState.cs index 162f82e..9c6cd42 100644 --- a/Service/Execute/Step/SwapDoneState.cs +++ b/Service/Execute/Step/SwapDoneState.cs @@ -29,7 +29,7 @@ public class SwapDoneState : IState public StateResult Handle(SwappingStateMachine machine) { //更新换电订单 - machine.SwapOrder.SwapResult = machine.SwapStatus; + machine.SwapOrder!.SwapResult = machine.SwapStatus; machine.SwapOrder.SwapEndTime = DateTime.Now; machine.SwapOrder.FailReason = machine.SwapFailReason; _swapOrderRepository.Update(machine.SwapOrder); @@ -79,7 +79,7 @@ public class SwapDoneState : IState { //查询车辆锁止状态 Task carInfo = TBoxApi.GetCarInfo(); - if (carInfo.Result.HeartBeatMsg.LockStatus == 2) + if (carInfo.Result.HeartBeatMsg!.LockStatus == 2) { //设置出口的是绿灯 if (PlcApi.WriteExistLamp(1000)) @@ -95,7 +95,7 @@ public class SwapDoneState : IState _swapOrderStepService.InsertSwapStepForSwapMain(InfoEnum.BusinessSwappingStep.VelLockFlag, - machine.StepSort++, machine.SwapOrder.Sn); + machine.StepSort++, machine.SwapOrder!.Sn); } } } @@ -113,7 +113,7 @@ public class SwapDoneState : IState return Invoker.Invoke("wait exist radar", 1000, 5, machine.IsCanceled, () => machine.RadarOutFlag, () => { - if (!PlcApi.EntranceRadar()) + if (!PlcApi.ExitRadar()) { _log.Info("exist radar false"); } diff --git a/Service/Plc/PlcMgr.cs b/Service/Plc/PlcMgr.cs new file mode 100644 index 0000000..d7097ad --- /dev/null +++ b/Service/Plc/PlcMgr.cs @@ -0,0 +1,27 @@ +using HybirdFrameworkDriver.ModbusTcpMaster; +using HybirdFrameworkServices.Plc; + +namespace Service.Plc; + +public abstract class PlcMgr +{ + public static ModbusTcpMaster ModbusTcpMaster; + public static readonly HostToPlc HostToPlcData = new HostToPlc(); + public static readonly PlcToHost PlcToHostData = new PlcToHost(); + public static readonly PlcFault PlcFaultData = new PlcFault(); + + + + /// + /// 写入口灯 + /// 红灯:1020 + /// 绿灯:1000 + /// + /// + /// + public static bool WriteEntranceLamp(byte value) + { + HostToPlcData.LightIn.Value = value; + return ModbusTcpMaster.WriteValue(HostToPlcData.LightIn); + } +} \ No newline at end of file