From 17e4c7e3e7b3ef60d9da6de3b2a39a14a53c38a0 Mon Sep 17 00:00:00 2001 From: z8018 <1282578289@qq.com> Date: 星期三, 12 三月 2025 14:11:33 +0800 Subject: [PATCH] 1 --- WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs | 118 +++++++++++++++++++++++++++++++++++++++++------------------ 1 files changed, 82 insertions(+), 36 deletions(-) diff --git a/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs b/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs index 2f1436a..e23eb5e 100644 --- a/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs +++ b/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs @@ -84,7 +84,15 @@ /// </summary> public override string Name => _name; + /// <summary> + /// PLC璇诲啓鏃ュ織璁板綍 + /// </summary> public override ILogNet LogNet => _logNet; + + /// <summary> + /// 鏄惁鍦ㄥ啓鍏ユ暟鎹悗璇诲彇鏁版嵁纭銆� + /// </summary> + public override bool IsReadAfterWrite { get; set; } = true; #endregion Public Member #region Constructor Function @@ -162,24 +170,31 @@ } else { - object? obj = null; - for (int i = 0; i < 5; i++) + if (IsReadAfterWrite) { - T readValue = Read<T>(address); - stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAfterRead, address, value)); - obj = readValue; - if (readValue.Equals(value)) + object? obj = null; + for (int i = 0; i < 5; i++) { - stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAndReadCheckSuccess, address, value, readValue)); - return true; + T readValue = Read<T>(address); + stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAfterRead, address, value)); + obj = readValue; + if (readValue.Equals(value)) + { + stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAndReadCheckSuccess, address, value, readValue)); + return true; + } + else if (i < 4) + { + Write(address, value); + } } - else if (i < 4) - { - Write(address, value); - } + stringBuilder.AppendLine(string.Format(CommunicationExceptionMessage.WriteAndReadCheckFaild, address, value, obj)); + throw new CommunicationException(stringBuilder.ToString(), CommunicationErrorType.WriteFailed); } - stringBuilder.AppendLine(string.Format(CommunicationExceptionMessage.WriteAndReadCheckFaild, address, value, obj)); - throw new CommunicationException(stringBuilder.ToString(), CommunicationErrorType.WriteFailed); + else + { + return true; + } } } catch (Exception ex) @@ -653,6 +668,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 @@ -668,6 +689,13 @@ #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(); @@ -677,33 +705,40 @@ stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteData, address, JsonConvert.SerializeObject(value))); if (operateResult.IsSuccess) { - object? obj = null; - for (int i = 0; i < 5; i++) + if (IsReadAfterWrite) { - T readValue = ReadCustomer<T>(address); - stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAfterRead, address, JsonConvert.SerializeObject(readValue))); - obj = readValue; - PropertyInfo[] propertyInfos = typeof(T).GetProperties(); - for (int j = 0; j < propertyInfos.Length; j++) + object? obj = null; + for (int i = 0; i < 5; i++) { - object? writeValueItem = propertyInfos[j].GetValue(value); - object? readValueItem = propertyInfos[j].GetValue(readValue); - if (writeValueItem.Equals(readValueItem)) + T readValue = ReadCustomer<T>(address); + stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAfterRead, address, JsonConvert.SerializeObject(readValue))); + obj = readValue; + PropertyInfo[] propertyInfos = typeof(T).GetProperties(); + for (int j = 0; j < propertyInfos.Length; j++) { - stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAndReadCheckSuccess, address, JsonConvert.SerializeObject(value), JsonConvert.SerializeObject(readValue))); + object? writeValueItem = propertyInfos[j].GetValue(value); + object? readValueItem = propertyInfos[j].GetValue(readValue); + if (writeValueItem.Equals(readValueItem)) + { + stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAndReadCheckSuccess, address, JsonConvert.SerializeObject(value), JsonConvert.SerializeObject(readValue))); + } + else + { + break; + } + if (j == propertyInfos.Length - 1) + return true; } - else - { - break; - } - if (j == propertyInfos.Length - 1) - return true; - } - plc.WriteCustomer(address, value); + plc.WriteCustomer(address, value); + } + stringBuilder.AppendLine(string.Format(CommunicationExceptionMessage.WriteAndReadCheckFaild, address, JsonConvert.SerializeObject(value), JsonConvert.SerializeObject(obj))); + throw new CommunicationException(string.Format(CommunicationExceptionMessage.WriteAndReadCheckFaild, address, JsonConvert.SerializeObject(value), JsonConvert.SerializeObject(obj)), CommunicationErrorType.WriteFailed); } - stringBuilder.AppendLine(string.Format(CommunicationExceptionMessage.WriteAndReadCheckFaild, address, JsonConvert.SerializeObject(value), JsonConvert.SerializeObject(obj))); - throw new CommunicationException(string.Format(CommunicationExceptionMessage.WriteAndReadCheckFaild, address, JsonConvert.SerializeObject(value), JsonConvert.SerializeObject(obj)), CommunicationErrorType.WriteFailed); + else + { + return true; + } } else { @@ -722,7 +757,9 @@ } #endregion - // 鏄惧紡瀹炵幇IDisposable鎺ュ彛浠ユ彁渚涘瀮鍦惧洖鏀舵椂鐨勬竻鐞� + /// <summary> + /// 鏄惧紡瀹炵幇IDisposable鎺ュ彛浠ユ彁渚涘瀮鍦惧洖鏀舵椂鐨勬竻鐞� + /// </summary> public override void Dispose() { _isPing = false; @@ -731,6 +768,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