From 21ed823057f933b3f794b8738cadf8c62ff3dc90 Mon Sep 17 00:00:00 2001 From: CZ Date: Fri, 24 May 2024 16:30:42 +0800 Subject: [PATCH] =?UTF-8?q?plc=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Service/EquipmentInit.cs | 7 +- Service/Execute/Api/PlcApi.cs | 71 ++------- Service/Plc/PlcMgr.cs | 149 +++++++++++++++++- .../Debug/net6.0/staticwebassets.build.json | 22 +-- 4 files changed, 168 insertions(+), 81 deletions(-) diff --git a/Service/EquipmentInit.cs b/Service/EquipmentInit.cs index 23bf3a3..e503c4a 100644 --- a/Service/EquipmentInit.cs +++ b/Service/EquipmentInit.cs @@ -69,27 +69,28 @@ namespace Service */ private static void BatchReadPlc(ModbusTcpMaster master) { - if (PlcApi.con) + if (PlcMgr.ModbusTcpMaster!=null) { var bytes01 = master.BatchReadHolderRegister(1, 115); if (bytes01 != null) { - PlcApi.DataValidityTime = DateTime.Now; + PlcMgr.DataValidityTime = DateTime.Now; ModbusDecoder.Decode(bytes01, PlcMgr.HostToPlcData); } var bytes02 = master.BatchReadHolderRegister(201, 222); if (bytes02 != null) { + PlcMgr.DataValidityTime = DateTime.Now; ModbusDecoder.Decode(bytes02, PlcMgr.PlcToHostData); } var bytes03 = master.BatchReadHolderRegister(701, 10); if (bytes03 != null) { - PlcApi.DataValidityTime = DateTime.Now; + PlcMgr.DataValidityTime = DateTime.Now; ModbusDecoder.Decode(bytes03, PlcMgr.PlcFaultData); } //OperateResult result2 = ModbusTcpNet.Read("x=3;201", 222); diff --git a/Service/Execute/Api/PlcApi.cs b/Service/Execute/Api/PlcApi.cs index 6c7cace..488629b 100644 --- a/Service/Execute/Api/PlcApi.cs +++ b/Service/Execute/Api/PlcApi.cs @@ -33,14 +33,7 @@ public class PlcApi /// public static bool IsRemote() { - if ((DateTime.Now - DataValidityTime).Seconds <= DataTimeSeconds) - { - return PlcMgr.PlcToHostData.RemoteLocalControlState.Value == 1010 ? true : false; - } - else - { - return false; - } + return PlcMgr.IsRemote(); } /// @@ -49,14 +42,7 @@ public class PlcApi /// public static bool EntranceRadar() { - if ((DateTime.Now - DataValidityTime).Seconds <= DataTimeSeconds) - { - return PlcMgr.PlcToHostData.RadarStatesIn.Value == 1000 ? true : false; - } - else - { - return false; - } + return PlcMgr.EntranceRadar(); } /// @@ -65,14 +51,7 @@ public class PlcApi /// public static bool ExitRadar() { - if ((DateTime.Now - DataValidityTime).Seconds <= DataTimeSeconds) - { - return PlcMgr.PlcToHostData.RadarStatesOut.Value == 1000 ? true : false; - } - else - { - return false; - } + return PlcMgr.ExitRadar(); } /// @@ -84,8 +63,7 @@ public class PlcApi /// public static bool WriteEntranceLamp(int data) { - return PlcMgr.WriteEntranceLamp((byte)data); - + return PlcMgr.WriteEntranceLamp((byte)data); } /// @@ -97,13 +75,7 @@ public class PlcApi /// public static bool WriteExistLamp(int data) { - /*if (master != null) - { - HostToPlc writeHostToPlc = new HostToPlc(); - writeHostToPlc.LightOut.Value = (ushort)data; - return master.WriteValue(writeHostToPlc.LightIn); - }*/ - return false; + return PlcMgr.WriteExistLamp(data); } /// @@ -114,16 +86,7 @@ public class PlcApi /// public static bool DistributeSelectPack(string inBinNo, string outBinNo) { - /*if (master != null) - { - HostToPlc writeHostToPlc = new HostToPlc(); - writeHostToPlc.EntrySelection.Value = Convert.ToUInt16(inBinNo); - writeHostToPlc.ExitSelection.Value = Convert.ToUInt16(outBinNo); - bool write1 = master.WriteValue(writeHostToPlc.EntrySelection); - bool write2 = master.WriteValue(writeHostToPlc.ExitSelection); - return write1 && write2; - }*/ - return false; + return PlcMgr.DistributeSelectPack(inBinNo, outBinNo); } @@ -133,19 +96,7 @@ public class PlcApi /// public static bool StartSwapping() { - /*if (master != null) - { - if (plcToHost.TaskType.Value == 0)//是否允许换电 - return false; - - HostToPlc writeHostToPlc = new HostToPlc(); - writeHostToPlc.TaskType.Value = (ushort)1; - writeHostToPlc.TaskEnablement.Value = (ushort)1000; - bool write1 = master.WriteValue(writeHostToPlc.TaskType); - bool write2 = master.WriteValue(writeHostToPlc.TaskEnablement); - return write1 && write2; - }*/ - return false; + return PlcMgr.StartSwapping(); } /// @@ -154,7 +105,7 @@ public class PlcApi /// public static byte ChannelStatus() { - return 0; + return PlcMgr.ChannelStatus(); } @@ -164,11 +115,7 @@ public class PlcApi /// 6未连接 public static int ReadPlcTaskStatus() { - /*if (master != null) - { - return plcToHost.TaskType.Value; - }*/ - return (-1); + return PlcMgr.ReadPlcTaskStatus(); } } \ No newline at end of file diff --git a/Service/Plc/PlcMgr.cs b/Service/Plc/PlcMgr.cs index d7097ad..01037d4 100644 --- a/Service/Plc/PlcMgr.cs +++ b/Service/Plc/PlcMgr.cs @@ -11,6 +11,16 @@ public abstract class PlcMgr public static readonly PlcFault PlcFaultData = new PlcFault(); + /// + /// 数据有效时间。单位秒 + /// + static int DataTimeSeconds = 2; + /// + /// 数据刷新时间 + /// + public static DateTime DataValidityTime = DateTime.Now; + + /// /// 写入口灯 @@ -21,7 +31,142 @@ public abstract class PlcMgr /// public static bool WriteEntranceLamp(byte value) { - HostToPlcData.LightIn.Value = value; - return ModbusTcpMaster.WriteValue(HostToPlcData.LightIn); + if (ModbusTcpMaster != null) + { + HostToPlcData.LightIn.Value = value; + return ModbusTcpMaster.WriteValue(HostToPlcData.LightIn); + } + return false; + } + + /// + /// 是否远程模式 + /// + /// + public static bool IsRemote() + { + if ((DateTime.Now - DataValidityTime).Seconds <= DataTimeSeconds) + { + return PlcToHostData.RemoteLocalControlState.Value == 1010 ? true : false; + } + else + { + return false; + } + } + + /// + /// 入口雷达检测 + /// + /// + public static bool EntranceRadar() + { + if ((DateTime.Now - DataValidityTime).Seconds <= DataTimeSeconds) + { + return PlcToHostData.RadarStatesIn.Value == 1000 ? true : false; + } + else + { + return false; + } + } + + /// + /// 出口雷达检测 + /// + /// + public static bool ExitRadar() + { + if ((DateTime.Now - DataValidityTime).Seconds <= DataTimeSeconds) + { + return PlcToHostData.RadarStatesOut.Value == 1000 ? true : false; + } + else + { + return false; + } + } + + /// + /// 写出口灯 + /// 红灯:1020 + /// 绿灯:1000 + /// + /// + /// + public static bool WriteExistLamp(int data) + { + if (ModbusTcpMaster != null) + { + HostToPlc writeHostToPlc = new HostToPlc(); + writeHostToPlc.LightOut.Value = (ushort)data; + return ModbusTcpMaster.WriteValue(writeHostToPlc.LightIn); + } + return false; + } + + /// + /// 下发选包 + /// + /// 入仓位,仓位号 + /// 出仓位,仓位号 + /// + public static bool DistributeSelectPack(string inBinNo, string outBinNo) + { + if (ModbusTcpMaster != null) + { + HostToPlc writeHostToPlc = new HostToPlc(); + writeHostToPlc.EntrySelection.Value = Convert.ToUInt16(inBinNo); + writeHostToPlc.ExitSelection.Value = Convert.ToUInt16(outBinNo); + bool write1 = ModbusTcpMaster.WriteValue(writeHostToPlc.EntrySelection); + bool write2 = ModbusTcpMaster.WriteValue(writeHostToPlc.ExitSelection); + return write1 && write2; + } + return false; + } + + + /// + /// 下发启动换电 + /// + /// + public static bool StartSwapping() + { + if (ModbusTcpMaster != null) + { + if (PlcToHostData.TaskType.Value == 0)//是否允许换电 + return false; + + HostToPlc writeHostToPlc = new HostToPlc(); + writeHostToPlc.TaskType.Value = (ushort)1; + writeHostToPlc.TaskEnablement.Value = (ushort)1000; + bool write1 = ModbusTcpMaster.WriteValue(writeHostToPlc.TaskType); + bool write2 = ModbusTcpMaster.WriteValue(writeHostToPlc.TaskEnablement); + return write1 && write2; + } + return false; + } + + /// + /// 查看拍照状态 + /// + /// + public static byte ChannelStatus() + { + return 0; + } + + + /// + /// 读plc任务状态 + /// + /// 6未连接 + public static int ReadPlcTaskStatus() + { + if (ModbusTcpMaster != null) + { + return PlcToHostData.TaskType.Value; + } + return (-1); } } \ No newline at end of file diff --git a/WebStarter/obj/Debug/net6.0/staticwebassets.build.json b/WebStarter/obj/Debug/net6.0/staticwebassets.build.json index 60f4e29..26839c5 100644 --- a/WebStarter/obj/Debug/net6.0/staticwebassets.build.json +++ b/WebStarter/obj/Debug/net6.0/staticwebassets.build.json @@ -1,6 +1,6 @@ { "Version": 1, - "Hash": "SIbd/wMGDFDb4PVjR2gB4U/BUVTP3CxZPCnoMCemIoA=", + "Hash": "zLyz6C3YNcAfQ4a3PqNREUoaDzuSSMCXv5JiEIrUCxU=", "Source": "WebStarter", "BasePath": "_content/WebStarter", "Mode": "Default", @@ -10,24 +10,22 @@ { "Name": "WebStarter\\wwwroot", "Source": "WebStarter", - "ContentRoot": "D:\\AprojectFile\\huanneng\\huannengMain\\WebStarter\\wwwroot\\", + "ContentRoot": "D:\\项目文件\\项目报表\\19焕能后背式站控\\04项目源码\\主服务\\WebStarter\\wwwroot\\", "BasePath": "_content/WebStarter", "Pattern": "**" } ], "Assets": [ { - "Identity": "D:\\AprojectFile\\huanneng\\huannengMain\\WebStarter\\wwwroot\\20240523\\0.jpg", + "Identity": "D:\\项目文件\\项目报表\\19焕能后背式站控\\04项目源码\\主服务\\WebStarter\\wwwroot\\20240523\\0.jpg", "SourceId": "WebStarter", "SourceType": "Discovered", - "ContentRoot": "D:\\AprojectFile\\huanneng\\huannengMain\\WebStarter\\wwwroot\\", + "ContentRoot": "D:\\项目文件\\项目报表\\19焕能后背式站控\\04项目源码\\主服务\\WebStarter\\wwwroot\\", "BasePath": "_content/WebStarter", "RelativePath": "20240523/0.jpg", "AssetKind": "All", "AssetMode": "All", "AssetRole": "Primary", - "AssetMergeBehavior": "PreferTarget", - "AssetMergeSource": "", "RelatedAsset": "", "AssetTraitName": "", "AssetTraitValue": "", @@ -36,17 +34,15 @@ "OriginalItemSpec": "wwwroot\\20240523\\0.jpg" }, { - "Identity": "D:\\AprojectFile\\huanneng\\huannengMain\\WebStarter\\wwwroot\\20240524\\0.jpg", + "Identity": "D:\\项目文件\\项目报表\\19焕能后背式站控\\04项目源码\\主服务\\WebStarter\\wwwroot\\20240524\\0.jpg", "SourceId": "WebStarter", "SourceType": "Discovered", - "ContentRoot": "D:\\AprojectFile\\huanneng\\huannengMain\\WebStarter\\wwwroot\\", + "ContentRoot": "D:\\项目文件\\项目报表\\19焕能后背式站控\\04项目源码\\主服务\\WebStarter\\wwwroot\\", "BasePath": "_content/WebStarter", "RelativePath": "20240524/0.jpg", "AssetKind": "All", "AssetMode": "All", "AssetRole": "Primary", - "AssetMergeBehavior": "PreferTarget", - "AssetMergeSource": "", "RelatedAsset": "", "AssetTraitName": "", "AssetTraitValue": "", @@ -55,17 +51,15 @@ "OriginalItemSpec": "wwwroot\\20240524\\0.jpg" }, { - "Identity": "D:\\AprojectFile\\huanneng\\huannengMain\\WebStarter\\wwwroot\\20240524\\0.png", + "Identity": "D:\\项目文件\\项目报表\\19焕能后背式站控\\04项目源码\\主服务\\WebStarter\\wwwroot\\20240524\\0.png", "SourceId": "WebStarter", "SourceType": "Discovered", - "ContentRoot": "D:\\AprojectFile\\huanneng\\huannengMain\\WebStarter\\wwwroot\\", + "ContentRoot": "D:\\项目文件\\项目报表\\19焕能后背式站控\\04项目源码\\主服务\\WebStarter\\wwwroot\\", "BasePath": "_content/WebStarter", "RelativePath": "20240524/0.png", "AssetKind": "All", "AssetMode": "All", "AssetRole": "Primary", - "AssetMergeBehavior": "PreferTarget", - "AssetMergeSource": "", "RelatedAsset": "", "AssetTraitName": "", "AssetTraitValue": "",