From e6a01ea2c1112e77b40f0f8d7395f87969397208 Mon Sep 17 00:00:00 2001 From: CZ Date: Tue, 2 Jul 2024 09:13:08 +0800 Subject: [PATCH] =?UTF-8?q?plc=E5=BF=83=E8=B7=B3=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Service/MyTask/PlcHeartTask.cs | 94 +++++++++++++++++++++++++++++++++ Service/Plc/Client/PlcClient.cs | 2 - Service/Plc/Client/PlcMgr.cs | 55 ------------------- 3 files changed, 94 insertions(+), 57 deletions(-) create mode 100644 Service/MyTask/PlcHeartTask.cs diff --git a/Service/MyTask/PlcHeartTask.cs b/Service/MyTask/PlcHeartTask.cs new file mode 100644 index 0000000..ca9ff38 --- /dev/null +++ b/Service/MyTask/PlcHeartTask.cs @@ -0,0 +1,94 @@ +using HybirdFrameworkCore.Autofac; +using HybirdFrameworkCore.Autofac.Attribute; +using HybirdFrameworkCore.AutoTask; +using log4net; +using Repository.Station; +using Service.Plc.Client; +using Service.Plc.Msg; + +namespace Service.MyTask; + +[Scope] +public class PlcHeartTask: ITask +{ + private static readonly ILog Log = LogManager.GetLogger(typeof(PlcHeartTask)); + private volatile bool _stop; + public string Name() + { + return "PlcHeartTask"; + } + + public int Interval() + { + return 1000 * 2; + } + + + private static bool heart = false; + + public BinInfoRepository BinInfoRepository { get; set; } + + public void Handle() + { + if (PlcMgr.PlcClient != null) + { + heart = !heart; + HostToPlc writeHostToPlc = new HostToPlc(); + writeHostToPlc.ProtocolVersion1.Value = 2; + writeHostToPlc.ProtocolVersion2.Value = 0; + writeHostToPlc.ProtocolVersion3.Value = 1; + writeHostToPlc.CommunicationDiagnosis.Value = (ushort)(heart ? 1 : 0); + writeHostToPlc.Years.Value = (ushort)DateTime.Now.Year; + writeHostToPlc.Month.Value = (ushort)DateTime.Now.Month; + writeHostToPlc.Day.Value = (ushort)DateTime.Now.Day; + writeHostToPlc.Hour.Value = (ushort)DateTime.Now.Hour; + writeHostToPlc.Points.Value = (ushort)DateTime.Now.Minute; + writeHostToPlc.Seconds.Value = (ushort)DateTime.Now.Second; + + var lstBinInfo = BinInfoRepository.Query(); + writeHostToPlc.ChargingStatus02.Value = (short)(lstBinInfo[1].ChargeStatus == 1 ? 1010 : 1000); + writeHostToPlc.ChargingStatus03.Value = (short)(lstBinInfo[2].ChargeStatus == 1 ? 1010 : 1000); + writeHostToPlc.ChargingStatus04.Value = (short)(lstBinInfo[3].ChargeStatus == 1 ? 1010 : 1000); + writeHostToPlc.ChargingStatus05.Value = (short)(lstBinInfo[4].ChargeStatus == 1 ? 1010 : 1000); + writeHostToPlc.ChargingStatus06.Value = (short)(lstBinInfo[5].ChargeStatus == 1 ? 1010 : 1000); + writeHostToPlc.ChargingStatus07.Value = (short)(lstBinInfo[6].ChargeStatus == 1 ? 1010 : 1000); + writeHostToPlc.ChargingStatus08.Value = (short)(lstBinInfo[7].ChargeStatus == 1 ? 1010 : 1000); + + + bool write01 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.ProtocolVersion1); + bool write02 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.ProtocolVersion2); + bool write03 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.ProtocolVersion3); + bool write0 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.CommunicationDiagnosis); + bool write1 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.Years); + bool write2 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.Month); + bool write3 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.Day); + bool write4 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.Hour); + bool write5 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.Points); + bool write6 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.Seconds); + bool writeCharge2 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.ChargingStatus02); + bool writeCharge3 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.ChargingStatus03); + bool writeCharge4 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.ChargingStatus04); + bool writeCharge5 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.ChargingStatus05); + bool writeCharge6 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.ChargingStatus06); + bool writeCharge7 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.ChargingStatus07); + bool writeCharge8 = PlcMgr.PlcClient.WriteValue(writeHostToPlc.ChargingStatus08); + + + } + } + + public bool Stoped() + { + return _stop; + } + + public void Stop() + { + _stop = true; + } + + public void ResetStop() + { + _stop = false; + } +} \ No newline at end of file diff --git a/Service/Plc/Client/PlcClient.cs b/Service/Plc/Client/PlcClient.cs index 9be9f5c..9aed50e 100644 --- a/Service/Plc/Client/PlcClient.cs +++ b/Service/Plc/Client/PlcClient.cs @@ -57,8 +57,6 @@ public class PlcClient : ModbusTcpMaster private void BatchRead(ModbusTcpMaster master) { - - PlcMgr.SendHear(); var bytes01 = master.BatchReadHolderRegister(1, 115); if (bytes01 != null) { diff --git a/Service/Plc/Client/PlcMgr.cs b/Service/Plc/Client/PlcMgr.cs index e9ce053..8b53a5c 100644 --- a/Service/Plc/Client/PlcMgr.cs +++ b/Service/Plc/Client/PlcMgr.cs @@ -259,61 +259,6 @@ public class PlcMgr private static BinInfoRepository BinInfoRepository = AppInfo.Container.Resolve(); - /// - /// 发送心跳 - /// - /// - public static bool SendHear() - { - if (PlcClient != null) - { - heart = !heart; - HostToPlc writeHostToPlc = new HostToPlc(); - writeHostToPlc.ProtocolVersion1.Value = 2; - writeHostToPlc.ProtocolVersion2.Value = 0; - writeHostToPlc.ProtocolVersion3.Value = 1; - writeHostToPlc.CommunicationDiagnosis.Value = (ushort)(heart ? 1 : 0); - writeHostToPlc.Years.Value = (ushort)DateTime.Now.Year; - writeHostToPlc.Month.Value = (ushort)DateTime.Now.Month; - writeHostToPlc.Day.Value = (ushort)DateTime.Now.Day; - writeHostToPlc.Hour.Value = (ushort)DateTime.Now.Hour; - writeHostToPlc.Points.Value = (ushort)DateTime.Now.Minute; - writeHostToPlc.Seconds.Value = (ushort)DateTime.Now.Second; - - var lstBinInfo = BinInfoRepository.Query(); - writeHostToPlc.ChargingStatus02.Value = (short)(lstBinInfo[1].ChargeStatus == 1 ? 1010 : 1000); - writeHostToPlc.ChargingStatus03.Value = (short)(lstBinInfo[2].ChargeStatus == 1 ? 1010 : 1000); - writeHostToPlc.ChargingStatus04.Value = (short)(lstBinInfo[3].ChargeStatus == 1 ? 1010 : 1000); - writeHostToPlc.ChargingStatus05.Value = (short)(lstBinInfo[4].ChargeStatus == 1 ? 1010 : 1000); - writeHostToPlc.ChargingStatus06.Value = (short)(lstBinInfo[5].ChargeStatus == 1 ? 1010 : 1000); - writeHostToPlc.ChargingStatus07.Value = (short)(lstBinInfo[6].ChargeStatus == 1 ? 1010 : 1000); - writeHostToPlc.ChargingStatus08.Value = (short)(lstBinInfo[7].ChargeStatus == 1 ? 1010 : 1000); - - - bool write01 = PlcClient.WriteValue(writeHostToPlc.ProtocolVersion1); - bool write02 = PlcClient.WriteValue(writeHostToPlc.ProtocolVersion2); - bool write03 = PlcClient.WriteValue(writeHostToPlc.ProtocolVersion3); - bool write0 = PlcClient.WriteValue(writeHostToPlc.CommunicationDiagnosis); - bool write1 = PlcClient.WriteValue(writeHostToPlc.Years); - bool write2 = PlcClient.WriteValue(writeHostToPlc.Month); - bool write3 = PlcClient.WriteValue(writeHostToPlc.Day); - bool write4 = PlcClient.WriteValue(writeHostToPlc.Hour); - bool write5 = PlcClient.WriteValue(writeHostToPlc.Points); - bool write6 = PlcClient.WriteValue(writeHostToPlc.Seconds); - bool writeCharge2 = PlcClient.WriteValue(writeHostToPlc.ChargingStatus02); - bool writeCharge3 = PlcClient.WriteValue(writeHostToPlc.ChargingStatus03); - bool writeCharge4 = PlcClient.WriteValue(writeHostToPlc.ChargingStatus04); - bool writeCharge5 = PlcClient.WriteValue(writeHostToPlc.ChargingStatus05); - bool writeCharge6 = PlcClient.WriteValue(writeHostToPlc.ChargingStatus06); - bool writeCharge7 = PlcClient.WriteValue(writeHostToPlc.ChargingStatus07); - bool writeCharge8 = PlcClient.WriteValue(writeHostToPlc.ChargingStatus08); - - return write1 && write2 && write3 && write4 && write5 && write6; - } - - return true; - } - /// /// 查看拍照状态 ///