Sfoglia il codice sorgente

20260601001 PLC参数管理模块,暂未设计完成,设计目标:界面能根据参数列表自动生成界面,管理模块可以使用尽可能少的方法来完成

向羽 孟 3 settimane fa
parent
commit
8a3352e539

+ 1 - 0
MvvmScaffoldFrame48.DLL/AlarmTools/AlarmMessageList.cs

@@ -7,5 +7,6 @@
     {
        系统异常 = 0,
        异常测试1 = 1,
+       PLC通讯连接失败 = 2,
     }
 }

+ 15 - 19
MvvmScaffoldFrame48.DLL/AlarmTools/SystemAlarm.cs

@@ -17,40 +17,36 @@ namespace MvvmScaffoldFrame48.DLL.AlarmTools
             new AlarmMessModel()
             {
                 ID = (int)AlarmMessageList.系统异常,
-                AlarmType = "SystemException",
-                AlarmMess = "系统异常",
+                AlarmName = "系统异常",
+                AlarmEnType = "SystemException",
+                AlarmChType = "系统",
+                IsAlarm =false
+            },
+            new AlarmMessModel()
+            {
+                ID = (int)AlarmMessageList.PLC通讯连接失败,
+                AlarmName = "PLC通讯连接失败",
+                AlarmEnType = "SystemException",
+                AlarmChType = "系统",
                 IsAlarm =false
             },
         };
 
-        /// <summary>
-        /// 开启指定警告
-        /// </summary>
-        /// <param name="ID"></param>
-        public static void AlarmAlert(AlarmMessageList ID,string AlarmPath)
+        public static void AlarmAlert(AlarmMessageList ID, string EnMessage, string ChMessage, string AlarmPath)
         {
             var alarm = AlarmMessList.Find(x => x.ID == (int)ID);
-            if (alarm.IsAlarm)
+            if (alarm == null || alarm.IsAlarm)
             {
                 return;
             }
             else
             {
-                errorMessageRecord.InsertErrorMessageRecord(new Model.StorageModel.AuditTrail.ErrorMessageRecordModel()
-                {
-                    Category = "SystemAlarm",
-                    Message = alarm.AlarmMess,
-                    MessageType = alarm.AlarmType,
-                    MessagePath = AlarmPath,
-                    userID = 0,
-                    LogLevel = 0,
-                    DateTime = DateTime.Now,
-                });
-
                 var ChanegAlarm = AlarmMessList.Find(x => x.ID == (int)ID);
                 ChanegAlarm.IsAlarm = true;
                 ChanegAlarm.AlarmDateTime = DateTime.Now;
                 ChanegAlarm.AlarmPath = AlarmPath;
+                ChanegAlarm.AlarmEnMess = EnMessage;
+                ChanegAlarm.AlarmChMess = ChMessage;
             }
         }
 

+ 78 - 0
MvvmScaffoldFrame48.DLL/CommunicationTools/PlcCommunManager.cs

@@ -0,0 +1,78 @@
+//PLC参数管理类
+using MvvmScaffoldFrame48.DLL.AlarmTools;
+using MvvmScaffoldFrame48.DLL.LogTools;
+using MvvmScaffoldFrame48.Model.StorageModel.PlcParameter;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MvvmScaffoldFrame48.DLL.CommunicationTools
+{
+    public class PlcCommunManager
+    {
+        private static readonly Dictionary<int, ushort> BitMasks = new Dictionary<int, ushort>
+        {
+            // 寄存器位掩码
+            {0, (ushort)(1 << 0)},
+            {1, (ushort)(1 << 1)},
+            {2, (ushort)(1 << 2)},
+            {3, (ushort)(1 << 3)},
+            {4, (ushort)(1 << 4)},
+            {5, (ushort)(1 << 5)},
+            {6, (ushort)(1 << 6)},
+            {7, (ushort)(1 << 7)},
+            {8, (ushort)(1 << 8)},
+            {9, (ushort)(1 << 9)},
+            {10, (ushort)(1 << 10)},
+            {11, (ushort)(1 << 11)},
+            {12, (ushort)(1 << 12)},
+            {13, (ushort)(1 << 13)},
+            {14, (ushort)(1 << 14)},
+            {15, (ushort)(1 << 15)},
+        };
+        private static readonly Dictionary<string, PlcParameterModel> RegisterMap = new Dictionary<string, PlcParameterModel>
+        {
+            {"test1",new PlcValueBitParameterModel(0,0) }
+        };
+
+        private bool isConnect = false;
+        public bool IsConnect
+        {
+            get
+            {
+                return isConnect ? modbusTcpClient.IsTcpClientConnected() : isConnect;
+            }
+        }
+
+        public ModbusTcpClient modbusTcpClient = new ModbusTcpClient();
+
+        public PlcCommunManager(string IpAddress)
+        {
+            ConnectModbus(IpAddress);
+        }
+        public void ConnectModbus(string ipAddress)
+        {
+            int i = 10;
+            while (!modbusTcpClient.Connect(ipAddress) && i > 0)
+            {
+                isConnect = false;
+                //SystemAlarm.AlarmAlert(AlarmMessageList.PLC通讯连接失败, $"Modbus通讯连接失败,目标IP:{ipAddress}", "DLL:MainThreadClass-ConnectModbus");
+                SystemAlarm.AlarmAlert(AlarmMessageList.PLC通讯连接失败,
+                    $"PLC communication connection failed, target IP:{ipAddress}",
+                    $"PLC通讯连接失败, IP地址:{ipAddress}",
+                    "DLL:MainThreadClass-ConnectModbus");
+                i--;
+                Task.Delay(1000);
+            }
+            if (modbusTcpClient.Connect(ipAddress))
+            {
+                SystemAlarm.AlarmCancel(AlarmMessageList.PLC通讯连接失败);
+                TxtLog.log("PLC通讯成功", 6);
+                //FaultLog.RecordLogMessage($"Modbus通讯连接成功,目标IP:{ipAddress}", 6);
+                isConnect = true;
+            }
+        }
+    }
+}

+ 1 - 0
MvvmScaffoldFrame48.DLL/MvvmScaffoldFrame48.Dll.csproj

@@ -83,6 +83,7 @@
     <Compile Include="CameraTools\HikCamera.cs" />
     <Compile Include="CameraTools\HikVision.cs" />
     <Compile Include="CommunicationTools\ModbusTcpClient.cs" />
+    <Compile Include="CommunicationTools\PlcCommunManager.cs" />
     <Compile Include="ConfigTools\ConfigService.cs" />
     <Compile Include="ConfigTools\XMLReadWrite.cs" />
     <Compile Include="FileTools\PDFGenerate.cs" />

+ 1 - 0
MvvmScaffoldFrame48.MODEL/MvvmScaffoldFrame48.Model.csproj

@@ -75,6 +75,7 @@
     <Compile Include="StorageModel\Configs\ShuLiConfigClassModel.cs" />
     <Compile Include="StorageModel\ImageAlgorithm\ShuLI\ValidRegionModel.cs" />
     <Compile Include="StorageModel\Configs\ProcessingAlgorithmConfigModel.cs" />
+    <Compile Include="StorageModel\PlcParameter\PlcParameterModel.cs" />
     <Compile Include="StorageModel\SystemAlarm\AlarmMessModel.cs" />
     <Compile Include="StorageModel\SystemAlarm\AlarmTypes.cs" />
     <Compile Include="StorageModel\Configs\CameraProcessConfigModel.cs" />

+ 77 - 0
MvvmScaffoldFrame48.MODEL/StorageModel/PlcParameter/PlcParameterModel.cs

@@ -0,0 +1,77 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MvvmScaffoldFrame48.Model.StorageModel.PlcParameter
+{
+    public class PlcParameterModel
+    {
+        //public string Name { get; set; }
+
+        /// <summary>
+        /// 1:ValueBit
+        /// 2:Int32
+        /// 3:Double
+        /// </summary>
+        public int Type { get; set; } = -1;
+        public int Length { get; set; } = 1;
+        public int Address { get; set; }
+    }
+    public class PlcValueBitParameterModel: PlcParameterModel
+    {
+        public int bitAddress { get; set; }
+        public PlcValueBitParameterModel(int Address,int bitAddress)
+        {
+            this.Type = 1;
+            this.Address = Address;
+            this.bitAddress = bitAddress;
+        }
+    }
+
+    public class PlcInt32ParameterModel:PlcParameterModel
+    {
+        public int MaxValue { get; set; }
+        public int MinValue { get; set; }
+
+        public PlcInt32ParameterModel(int Address)
+        {
+            this.Type = 2;
+            this.Address = Address;
+            this.Length = 2;
+            this.MaxValue = Int32.MaxValue;
+            this.MinValue = Int32.MinValue;
+        }
+        public PlcInt32ParameterModel(int Address,int MaxValue,int MinValue)
+        {
+            this.Type = 2;
+            this.Address = Address;
+            this.Length = 2;
+            this.MaxValue = MaxValue;
+            this.MinValue = MinValue;
+        }
+    }
+
+    public class PlcDouble64ParameterModel : PlcParameterModel
+    {
+        public double MaxValue { get; set; }
+        public double MinValue { get; set; }
+        public PlcDouble64ParameterModel(int Address)
+        {
+            this.Type = 3;
+            this.Address = Address;
+            this.Length = 4;
+            this.MaxValue = double.MaxValue;
+            this.MinValue = double.MinValue;
+        }
+        public PlcDouble64ParameterModel(int Address,double MaxValue,double MinValue)
+        {
+            this.Type = 3;
+            this.Address = Address;
+            this.Length = 4;
+            this.MaxValue = double.MaxValue;
+            this.MinValue = double.MinValue;
+        }
+    }
+}

+ 20 - 6
MvvmScaffoldFrame48.MODEL/StorageModel/SystemAlarm/AlarmMessModel.cs

@@ -14,26 +14,40 @@ namespace MvvmScaffoldFrame48.Model.StorageModel.SystemAlarm
         public int ID { get; set; }
 
         /// <summary>
-        /// 报警类型
+        /// 报警类型-英文
         /// </summary>
-        public string AlarmType { get; set; }
+        public string AlarmEnType { get; set; }
 
         /// <summary>
-        /// 报警信息
+        /// 报警类型-中文
         /// </summary>
-        public string AlarmMess { get; set; }
+        public string AlarmChType { get; set; }
 
+        /// <summary>
+        /// 报警名称
+        /// </summary>
+        public string AlarmName { get; set; }
+
+        /// <summary>
+        /// 报警信息-英文
+        /// </summary>
+        public string AlarmEnMess { get; set; }
+
+        /// <summary>
+        /// 报警信息-中文
+        /// </summary>
+        public string AlarmChMess { get; set; }
         /// <summary>
         /// 报警信息路径
         /// </summary>
         public string AlarmPath { get; set; }
-        
+
         /// <summary>
         /// 报警时间
         /// </summary>
         public DateTime AlarmDateTime { get; set; }
 
-       /// <summary>
+        /// <summary>
         /// 是否处于报警状态
         /// </summary>
         public bool IsAlarm { get; set; } = false;