From 2aef482fa7431fb47eef94081ff23ec220f89820 Mon Sep 17 00:00:00 2001
From: hutongqing <hutongqing@hnkhzn.com>
Date: 星期五, 24 一月 2025 09:10:31 +0800
Subject: [PATCH] 1

---
 WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs |  131 ++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 114 insertions(+), 17 deletions(-)

diff --git a/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs b/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs
index f6173d7..c695b74 100644
--- a/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs
+++ b/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs
@@ -20,6 +20,7 @@
 using HslCommunication.LogNet;
 using HslCommunication.Profinet.Siemens;
 using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
 using System;
 using System.Collections;
 using System.Collections.Generic;
@@ -83,6 +84,9 @@
         /// </summary>
         public override string Name => _name;
 
+        /// <summary>
+        /// PLC璇诲啓鏃ュ織璁板綍
+        /// </summary>
         public override ILogNet LogNet => _logNet;
         #endregion Public Member
 
@@ -96,7 +100,7 @@
         public SiemensS7(string ipAddress, int port, string name)
         {
             string path = AppDomain.CurrentDomain.BaseDirectory + $"Log_PLCReadWrite\\{name}";
-            _logNet = new LogNetFileSize(path, 3 * 1024 * 1024, 100);
+            _logNet = new LogNetFileSize(path, 10 * 1024 * 1024, 100);
 
             bool ipCheck = IPAddress.TryParse(ipAddress, out IPAddress? address);
             if (!ipCheck)
@@ -147,6 +151,10 @@
         /// <exception cref="CommunicationException"></exception>
         private bool GetResult<T>(OperateResult operateResult, string address, T value) where T : notnull
         {
+            if (value is Array)
+            {
+                return operateResult.IsSuccess;
+            }
             StringBuilder stringBuilder = new StringBuilder();
             try
             {
@@ -161,7 +169,7 @@
                     for (int i = 0; i < 5; i++)
                     {
                         T readValue = Read<T>(address);
-                        stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAfterRead, readValue, value));
+                        stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAfterRead, address, value));
                         obj = readValue;
                         if (readValue.Equals(value))
                         {
@@ -186,7 +194,6 @@
             {
                 LogNet.WriteInfo(Name, stringBuilder.ToString());
             }
-
         }
 
         /// <summary>
@@ -223,6 +230,38 @@
                     case TypeCode.Char:
                         return plc.Write(address, Convert.ToChar(value));
                     default:
+                        if (value is int[])
+                        {
+                            return plc.Write(address, value as int[]);
+                        }
+                        else if (value is uint[])
+                        {
+                            return plc.Write(address, value as uint[]);
+                        }
+                        else if (value is short[])
+                        {
+                            return plc.Write(address, value as short[]);
+                        }
+                        else if (value is ushort[])
+                        {
+                            return plc.Write(address, value as ushort[]);
+                        }
+                        else if (value is bool[])
+                        {
+                            return plc.Write(address, value as bool[]);
+                        }
+                        else if (value is float[])
+                        {
+                            return plc.Write(address, value as float[]);
+                        }
+                        else if (value is double[])
+                        {
+                            return plc.Write(address, value as double[]);
+                        }
+                        else if (value is byte[])
+                        {
+                            return plc.Write(address, value as byte[]);
+                        }
                         throw new CommunicationException(string.Format(CommunicationExceptionMessage.DataTypeErrorException, type.Name, address), CommunicationErrorType.TypeError);
                 }
             }
@@ -237,29 +276,54 @@
             }
         }
 
-        private object Read(string address, TypeCode typeCode)
+        private object Read(string address, TypeCode typeCode, ushort length = 1)
         {
             try
             {
                 switch (typeCode)
                 {
                     case TypeCode.Int32:
-                        return (int)GetContent(plc.ReadInt32(address), address);
+                        if (length > 1)
+                            return (int[])GetContent(plc.ReadInt32(address, length), address);
+                        else
+                            return (int)GetContent(plc.ReadInt32(address), address);
                     case TypeCode.UInt32:
-                        return (uint)GetContent(plc.ReadUInt32(address), address);
+                        if (length > 1)
+                            return (uint[])GetContent(plc.ReadUInt32(address, length), address);
+                        else
+                            return (uint)GetContent(plc.ReadUInt32(address), address);
                     case TypeCode.Int16:
-                        return (short)GetContent(plc.ReadInt16(address), address);
+                        if (length > 1)
+                            return (short[])GetContent(plc.ReadInt16(address, length), address);
+                        else
+                            return (short)GetContent(plc.ReadInt16(address), address);
                     case TypeCode.UInt16:
-                        return (ushort)GetContent(plc.ReadUInt16(address), address);
+                        if (length > 1)
+                            return (ushort[])GetContent(plc.ReadUInt16(address, length), address);
+                        else
+                            return (ushort)GetContent(plc.ReadUInt16(address), address);
                     case TypeCode.Single:
-                        return (float)GetContent(plc.ReadFloat(address), address);
+                        if (length > 1)
+                            return (float[])GetContent(plc.ReadFloat(address, length), address);
+                        else
+                            return (float)GetContent(plc.ReadFloat(address), address);
                     case TypeCode.Boolean:
-                        return (bool)GetContent(plc.ReadBool(address), address);
+                        if (length > 1)
+                            return (bool[])GetContent(plc.ReadBool(address, length), address);
+                        else
+                            return (bool)GetContent(plc.ReadBool(address), address);
                     case TypeCode.Byte:
+                        if (length > 1)
+                            return (byte)GetContent(plc.Read(address, length), address);
                         return (byte)GetContent(plc.ReadByte(address), address);
                     case TypeCode.String:
-                        return (string)GetContent(plc.ReadString(address), address);
+                        if (length > 1)
+                            return (string)GetContent(plc.ReadString(address, length), address);
+                        else
+                            return (string)GetContent(plc.ReadString(address), address);
                     case TypeCode.Char:
+                        if (length > 1)
+                            return (char)GetContent(plc.Read(address, length), address);
                         return (char)GetContent(plc.ReadByte(address), address);
                     default:
                         throw new CommunicationException(string.Format(CommunicationExceptionMessage.DataTypeErrorException, typeCode.ToString(), address), CommunicationErrorType.TypeError);
@@ -311,7 +375,6 @@
         {
             try
             {
-
                 //瀹炰緥鍖栦竴涓タ闂ㄥ瓙鐨凷7鍗忚鐨勯�氳瀵硅薄
                 plc = new SiemensS7Net(SiemensPLCS.S1500)
                 {
@@ -388,6 +451,12 @@
             return (T)Read(address, Type.GetTypeCode(type));
         }
 
+        public override T[] Read<T>(string address, ushort length)
+        {
+            Type type = typeof(T);
+            return (T[])Read(address, Type.GetTypeCode(type), length);
+        }
+
         /// <summary>
         /// 浠嶱LC璇诲彇鏁版嵁杩斿洖object銆�
         /// </summary>
@@ -427,7 +496,7 @@
             catch (Exception ex)
             {
                 //鍐欏叆寮傚父鏃舵姏鍑鸿嚜瀹氫箟閫氳寮傚父绫�
-                throw new CommunicationException($"鍐欏叆鏁版嵁寮傚父,鍦板潃:銆恵address}銆�,閿欒淇℃伅: {ex.Message}", CommunicationErrorType.ReadFailed, innerException: ex);
+                throw new CommunicationException($"鍐欏叆鏁版嵁寮傚父,鍦板潃:銆恵address}銆�,閿欒淇℃伅: {ex.Message}", CommunicationErrorType.WriteFailed, innerException: ex);
             }
         }
 
@@ -442,6 +511,12 @@
         public override bool Write<T>(string address, T value)
         {
             return GetResult(Write(address, value), address, value);
+        }
+
+
+        public override bool Write<T>(string address, T[] values)
+        {
+            return GetResult(Write(address, values), address, values);
         }
 
         /// <summary>
@@ -581,6 +656,12 @@
         #endregion
 
         #region ReadCustomer
+        /// <summary>
+        /// 璇诲彇鑷畾涔夌殑鏁版嵁绫诲瀷锛岄渶瑕佺户鎵胯嚜IDataTransfer鎺ュ彛锛岃繑鍥炰竴涓柊鐨勭被鍨嬬殑瀹炰緥瀵硅薄銆�
+        /// </summary>
+        /// <typeparam name="T">鑷畾涔夌殑鏁版嵁绫诲瀷娉涘瀷銆�</typeparam>
+        /// <param name="address">婧愬湴鍧�锛屽叿浣撴牸寮忓彇鍐充簬浣跨敤鐨勫伐涓氬崗璁��</param>
+        /// <returns>鎴愬姛杩斿洖鑷畾涔夌被鍨嬫暟鎹紝澶辫触鎶涘嚭寮傚父銆�</returns>
         public override T ReadCustomer<T>(string address)
         {
             try
@@ -592,11 +673,17 @@
                 LogNet.WriteException(Name, $"銆恵Name}銆慞LC璇诲彇寮傚父锛屽湴鍧�锛氥�恵address}銆戯紝閿欒淇℃伅锛氥�恵ex.Message}銆�", ex);
                 throw new CommunicationException(ex.Message, CommunicationErrorType.ReadException, innerException: ex);
             }
-
         }
         #endregion
 
         #region WriteCustomer
+        /// <summary>
+        /// 鍐欏叆鑷畾涔夌被鍨嬬殑鏁版嵁锛岃绫诲瀷蹇呴』缁ф壙鑷狪DataTransfer鎺ュ彛銆�
+        /// </summary>
+        /// <typeparam name="T">鑷畾涔夌殑鏁版嵁绫诲瀷娉涘瀷銆�</typeparam>
+        /// <param name="address">婧愬湴鍧�锛屽叿浣撴牸寮忓彇鍐充簬浣跨敤鐨勫伐涓氬崗璁��</param>
+        /// <param name="value">瑕佸啓鍏ユ暟鎹��</param>
+        /// <returns>濡傛灉鍐欏叆鎴愬姛鍒欒繑鍥瀟rue锛屽け璐ュ垯鎶涘嚭寮傚父銆�</returns>
         public override bool WriteCustomer<T>(string address, [NotNull] T value)
         {
             StringBuilder stringBuilder = new StringBuilder();
@@ -617,7 +704,7 @@
                         {
                             object? writeValueItem = propertyInfos[j].GetValue(value);
                             object? readValueItem = propertyInfos[j].GetValue(readValue);
-                            if (writeValueItem.Equals(readValueItem))
+                            if (writeValueItem?.Equals(readValueItem) ?? false)
                             {
                                 stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAndReadCheckSuccess, address, JsonConvert.SerializeObject(value), JsonConvert.SerializeObject(readValue)));
                             }
@@ -648,11 +735,12 @@
             {
                 LogNet.WriteInfo(Name, stringBuilder.ToString());
             }
-
         }
         #endregion
 
-        // 鏄惧紡瀹炵幇IDisposable鎺ュ彛浠ユ彁渚涘瀮鍦惧洖鏀舵椂鐨勬竻鐞�  
+        /// <summary>
+        /// 鏄惧紡瀹炵幇IDisposable鎺ュ彛浠ユ彁渚涘瀮鍦惧洖鏀舵椂鐨勬竻鐞�
+        /// </summary>
         public override void Dispose()
         {
             _isPing = false;
@@ -661,6 +749,15 @@
             GC.SuppressFinalize(this);
         }
 
+        /// <summary>
+        /// 绛夊緟鎸囧畾鍦板潃鐨勬硾鍨嬬被鍨嬪�间负鎸囧畾鐨勫��
+        /// </summary>
+        /// <typeparam name="T">鎸囧畾鐨勫�肩殑绫诲瀷娉涘瀷銆�</typeparam>
+        /// <param name="address">婧愬湴鍧�锛屽叿浣撴牸寮忓彇鍐充簬浣跨敤鐨勫伐涓氬崗璁��</param>
+        /// <param name="readInterval">璇诲彇鐨勯鐜囥��</param>
+        /// <param name="waitTimeout">绛夊緟鐨勮秴鏃舵椂闂达紝濡傛灉瓒呮椂鏃堕棿涓�-1鐨勮瘽锛屽垯鏄棤鏈熼檺绛夊緟銆�</param>
+        /// <param name="value">绛夊緟妫�娴嬬殑鍊�</param>
+        /// <returns>鏄惁绛夊緟鎴愬姛鐨勭粨鏋滃璞★紝涓�鏃﹂�氫俊澶辫触锛屾垨鏄瓑寰呰秴鏃跺氨杩斿洖澶辫触銆傚惁鍒欒繑鍥炴垚鍔燂紝骞跺憡鐭ヨ皟鐢ㄦ柟绛夊緟浜嗗涔呫��</returns>
         public override OperateResult<TimeSpan> Wait<T>(string address, int readInterval, int waitTimeout, T value)
         {
             TypeCode typeCode = Type.GetTypeCode(typeof(T));

--
Gitblit v1.9.3