From 0d07b90fd906e52ce486484aa53a6850983b1325 Mon Sep 17 00:00:00 2001 From: hutongqing <hutongqing@hnkhzn.com> Date: 星期二, 15 十月 2024 14:21:57 +0800 Subject: [PATCH] 更新 --- WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs | 441 +++++++++++++++++++++++++----------------------------- 1 files changed, 203 insertions(+), 238 deletions(-) diff --git a/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs b/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs index 4fe7441..ce35fed 100644 --- a/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs +++ b/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs @@ -24,8 +24,10 @@ using System.Collections; using System.Collections.Generic; using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Net; +using System.Net.NetworkInformation; using System.Reflection; using System.Text; using System.Threading.Tasks; @@ -37,7 +39,7 @@ /// 瑗块棬瀛怱7閫氳绫� /// </summary> [Description("瑗块棬瀛怱7")] - public class SiemensS7 : BaseCommunicator, IDisposable + public class SiemensS7 : BaseCommunicator { #region Private Member /// <summary> @@ -60,9 +62,14 @@ /// </summary> private bool _connected; + /// <summary> + /// PLC鍚嶇О + /// </summary> private string _name; private ILogNet _logNet; + + private bool _isPing = true; #endregion Private Member #region Public Member @@ -71,6 +78,9 @@ /// </summary> public override bool IsConnected => _connected; + /// <summary> + /// PLC鍚嶇О + /// </summary> public override string Name => _name; public override ILogNet LogNet => _logNet; @@ -82,10 +92,11 @@ /// </summary> /// <param name="ipAddress">璁惧鐨処P鍦板潃</param> /// <param name="port">杩炴帴浣跨敤鐨勭鍙e彿</param> + /// <param name="name">璁惧鍚嶇О</param> 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) @@ -108,7 +119,7 @@ /// <param name="operateResult">HSLCommunication璇诲彇鐨凮perateResult<T>瀵硅薄</param> /// <returns>濡傛灉璇诲彇鎴愬姛锛岃繑鍥炶鍙栫粨鏋滐紝璇诲彇澶辫触锛屾姏鍑鸿嚜瀹氫箟閫氳寮傚父</returns> /// <exception cref="CommunicationException">鑷畾涔夐�氳寮傚父绫�</exception> - private object? GetContent<T>(OperateResult<T> operateResult, string address) + private object GetContent<T>(OperateResult<T> operateResult, string address) { try { @@ -116,14 +127,13 @@ { throw new CommunicationException(string.Format(CommunicationExceptionMessage.ReadFailedException, typeof(T).Name, address, operateResult.Message), CommunicationErrorType.ReadFailed); } - return operateResult.Content; + return operateResult.Content ?? throw new CommunicationException(string.Format(CommunicationExceptionMessage.ReadDataIsNull, address), CommunicationErrorType.ReadFailed); } catch (Exception ex) { - LogNet.WriteException(Name, $"銆恵Name}銆慞LC璇诲彇寮傚父锛屽湴鍧�锛氥�恵address}銆戯紝閿欒淇℃伅锛氥�恵ex.Message}銆�", ex); - throw new CommunicationException(ex.Message, CommunicationErrorType.ReadException, innerException: ex); + LogNet.WriteException(Name, ex.Message, ex); + throw new CommunicationException(ex.Message, CommunicationErrorType.ReadFailed, innerException: ex); } - } /// <summary> @@ -137,8 +147,10 @@ /// <exception cref="CommunicationException"></exception> private bool GetResult<T>(OperateResult operateResult, string address, T value) where T : notnull { + StringBuilder stringBuilder = new StringBuilder(); try { + stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteData, address, value)); if (!operateResult.IsSuccess) { throw new CommunicationException(string.Format(CommunicationExceptionMessage.WriteFailedException, typeof(T).Name, address, value, operateResult.Message), CommunicationErrorType.WriteFailed); @@ -149,25 +161,32 @@ for (int i = 0; i < 5; i++) { T readValue = Read<T>(address); + stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAfterRead, readValue, value)); obj = readValue; if (readValue.Equals(value)) { + stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAndReadCheckSuccess, address, value, readValue)); return true; } - else + else if (i < 4) { Write(address, value); } } - throw new CommunicationException(string.Format(CommunicationExceptionMessage.ReadWriteDifferentException, typeof(T).Name, address, value, obj), CommunicationErrorType.WriteFailed); + stringBuilder.AppendLine(string.Format(CommunicationExceptionMessage.WriteAndReadCheckFaild, address, value, obj)); + throw new CommunicationException(stringBuilder.ToString(), CommunicationErrorType.WriteFailed); } } - catch(Exception ex) + catch (Exception ex) { - LogNet.WriteException(Name, $"銆恵Name}銆慞LC鍐欏叆寮傚父锛屽湴鍧�锛氥�恵address}銆戯紝鍐欏叆鐨勬暟鎹細銆恵value}銆戯紝閿欒淇℃伅锛氥�恵ex.Message}銆�", ex); + LogNet.WriteException(Name, ex.Message, ex); throw new CommunicationException(ex.Message, CommunicationErrorType.WriteFailed, innerException: ex); } - + finally + { + LogNet.WriteInfo(Name, stringBuilder.ToString()); + } + } /// <summary> @@ -209,12 +228,12 @@ } catch (CommunicationException ex) { - throw new CommunicationException($"鍐欏叆閿欒锛�" + ex.Message, ex.ErrorType, ex.ErrorCode, ex.InnerException); + throw new CommunicationException(ex.Message, ex.ErrorType); } catch (Exception ex) { //璇诲彇寮傚父鏃舵姏鍑鸿嚜瀹氫箟閫氳寮傚父绫� - throw new CommunicationException($"鍐欏叆鏁版嵁寮傚父,閿欒淇℃伅:{ex.Message}", CommunicationErrorType.TypeError, innerException: ex); + throw new CommunicationException(string.Format(CommunicationExceptionMessage.DataTypeErrorException, address, value), CommunicationErrorType.TypeError, innerException: ex); } } @@ -243,19 +262,42 @@ case TypeCode.Char: return (char)GetContent(plc.ReadByte(address), address); default: - throw new CommunicationException($"璇诲彇鏁版嵁澶辫触,鏈畾涔夋暟鎹被鍨�:銆恵typeCode}銆�,鍦板潃:銆恵address}銆�", CommunicationErrorType.TypeError); + throw new CommunicationException(string.Format(CommunicationExceptionMessage.DataTypeErrorException, typeCode.ToString(), address), CommunicationErrorType.TypeError); } } catch (CommunicationException ex) { //璇诲彇寮傚父鏃舵姏鍑鸿嚜瀹氫箟閫氳寮傚父绫� - throw ex; + throw new CommunicationException(ex.Message, ex.ErrorType); } catch (Exception ex) { //璇诲彇寮傚父鏃舵姏鍑鸿嚜瀹氫箟閫氳寮傚父绫� throw new CommunicationException($"璇诲彇鏁版嵁寮傚父,閿欒淇℃伅:{ex.Message}", CommunicationErrorType.ReadException, innerException: ex); } + } + + private void Ping() + { + Task.Run(() => + { + while (_isPing) + { + try + { + IPStatus status = plc.IpAddressPing(); + if (status == IPStatus.Success) + _connected = true; + else + _connected = false; + } + finally + { + Thread.Sleep(100); + } + } + }); + } #endregion @@ -269,6 +311,7 @@ { try { + //瀹炰緥鍖栦竴涓タ闂ㄥ瓙鐨凷7鍗忚鐨勯�氳瀵硅薄 plc = new SiemensS7Net(SiemensPLCS.S1500) { @@ -279,15 +322,15 @@ _connected = operateResult.IsSuccess;//灏嗚繛鎺ユ槸鍚︽垚鍔熻祴鍊肩粰褰撳墠閫氳鍣ㄦ槸鍚﹀凡杩炴帴鍒癙LC if (_connected) - LogNet.WriteInfo(Name, $"銆恵Name}銆慞LC杩炴帴鎴愬姛锛孖P銆恵_ipAddress}銆戯紝Port銆恵_port}銆�"); + LogNet.WriteInfo(Name, string.Format(CommunicationInfoMessage.ConnectSuccess, _ipAddress, _port)); else - LogNet.WriteError(Name, $"銆恵Name}銆慞LC杩炴帴澶辫触锛孖P銆恵_ipAddress}銆戯紝Port銆恵_port}銆戯紝閿欒淇℃伅锛歿operateResult.Message}"); - + LogNet.WriteError(Name, string.Format(CommunicationExceptionMessage.ConnectFaild, _ipAddress, _port, operateResult.Message)); + Ping(); return operateResult.IsSuccess; } catch (Exception ex) { - LogNet.WriteException(Name, $"銆恵Name}銆慞LC杩炴帴寮傚父锛孖P銆恵_ipAddress}銆戯紝Port銆恵_port}銆�", ex); + LogNet.WriteException(Name, string.Format(CommunicationExceptionMessage.ConnectFaild, _ipAddress, _port, ex.Message), ex); //杩炴帴寮傚父鏃舵姏鍑鸿嚜瀹氫箟寮傚父绫� throw new CommunicationException(ex.Message, CommunicationErrorType.ConnectionFailed, innerException: ex); } @@ -328,20 +371,7 @@ /// <exception cref="CommunicationException">鑷畾涔夐�氳寮傚父绫�</exception> public override byte[] Read(string address, int length) { - try - { - return (byte[])GetContent(plc.Read(address, (ushort)length), address); - } - catch (CommunicationException ex) - { - //璇诲彇寮傚父鏃舵姏鍑鸿嚜瀹氫箟閫氳寮傚父绫� - throw ex; - } - catch (Exception ex) - { - //璇诲彇寮傚父鏃舵姏鍑鸿嚜瀹氫箟閫氳寮傚父绫� - throw new CommunicationException($"璇诲彇鏁版嵁寮傚父:{ex.Message}", CommunicationErrorType.ReadFailed, innerException: ex); - } + return (byte[])GetContent(plc.Read(address, (ushort)length), address); } /// <summary> @@ -354,22 +384,8 @@ /// <exception cref="CommunicationException">鑷畾涔夐�氳寮傚父绫�</exception> public override T Read<T>(string address) { - try - { - Type type = typeof(T); - return (T)Read(address, Type.GetTypeCode(type)); - - } - catch (CommunicationException ex) - { - //璇诲彇寮傚父鏃舵姏鍑鸿嚜瀹氫箟閫氳寮傚父绫� - throw ex; - } - catch (Exception ex) - { - //璇诲彇寮傚父鏃舵姏鍑鸿嚜瀹氫箟閫氳寮傚父绫� - throw new CommunicationException($"璇诲彇鏁版嵁寮傚父,閿欒淇℃伅:{ex.Message}", CommunicationErrorType.ReadException, innerException: ex); - } + Type type = typeof(T); + return (T)Read(address, Type.GetTypeCode(type)); } /// <summary> @@ -381,20 +397,7 @@ /// <exception cref="CommunicationException">鑷畾涔夐�氳寮傚父绫�</exception> public override object ReadAsObj(string address, string dataType) { - try - { - return Read(address, SiemensDBDataType.GetTypeCode(dataType)); - } - catch (CommunicationException ex) - { - //璇诲彇寮傚父鏃舵姏鍑鸿嚜瀹氫箟閫氳寮傚父绫� - throw ex; - } - catch (Exception ex) - { - //璇诲彇寮傚父鏃舵姏鍑鸿嚜瀹氫箟閫氳寮傚父绫� - throw new CommunicationException($"璇诲彇鏁版嵁寮傚父,鏁版嵁绫诲瀷:銆恵dataType}銆�,鍦板潃:銆恵address}銆�,閿欒淇℃伅:{ex.Message}", CommunicationErrorType.ReadException, innerException: ex); - } + return Read(address, SiemensDBDataType.GetTypeCode(dataType)); } #endregion @@ -438,20 +441,7 @@ /// <exception cref="NotImplementedException"></exception> public override bool Write<T>(string address, T value) { - try - { - return GetResult(Write(address, value), address, value); - } - catch (CommunicationException ex) - { - //璇诲彇寮傚父鏃舵姏鍑鸿嚜瀹氫箟閫氳寮傚父绫� - throw ex; - } - catch (Exception ex) - { - //璇诲彇寮傚父鏃舵姏鍑鸿嚜瀹氫箟閫氳寮傚父绫� - throw new CommunicationException($"璇诲彇鏁版嵁寮傚父,鍦板潃:銆恵address}銆�,閿欒淇℃伅:{ex.Message}", CommunicationErrorType.TypeError, innerException: ex); - } + return GetResult(Write(address, value), address, value); } /// <summary> @@ -462,168 +452,131 @@ /// <param name="value">瑕佸啓鍏ョ殑鏁版嵁銆�</param> /// <returns>濡傛灉鍐欏叆鎴愬姛鍒欒繑鍥瀟rue锛屽け璐ュ垯鎶涘嚭鑷畾涔夐�氳寮傚父銆�</returns> /// <exception cref="CommunicationException"></exception> - public override bool WriteObj(string address, string dataType, object value) + public override bool WriteObj(string address, string dataType, [NotNull] object value) { - try + bool obj = false; + switch (dataType.ToLower()) { - bool obj = false; - switch (dataType.ToLower()) - { - case SiemensDBDataType.DataType_DInt: + case SiemensDBDataType.DataType_DInt: + { + int writeVal; + try { - int writeVal; - try - { - writeVal = Convert.ToInt32(value); - } - catch (Exception ex) - { - throw new CommunicationException($"鍐欏叆鏁版嵁寮傚父,鏁版嵁绫诲瀷:銆恵dataType}銆�,鍦板潃:銆恵address}銆�,鏁版嵁:銆恵value}銆�,鏁版嵁绫诲瀷杞崲閿欒,閿欒淇℃伅:{ex.Message}", CommunicationErrorType.TypeError, innerException: ex); - } - obj = GetResult(Write(address, writeVal), address, writeVal); + writeVal = Convert.ToInt32(value); } - break; - case SiemensDBDataType.DataType_DW: + catch (Exception ex) { - uint writeVal; - try - { - writeVal = Convert.ToUInt32(value); - } - catch (Exception ex) - { - throw new CommunicationException($"鍐欏叆鏁版嵁寮傚父,鏁版嵁绫诲瀷:銆恵dataType}銆�,鍦板潃:銆恵address}銆�,鏁版嵁:銆恵value}銆�,鏁版嵁绫诲瀷杞崲閿欒,閿欒淇℃伅:{ex.Message}", CommunicationErrorType.TypeError, innerException: ex); - } - obj = GetResult(Write(address, writeVal), address, writeVal); + throw new CommunicationException(string.Format(CommunicationExceptionMessage.TypeConvertError, dataType, address, value, ex.Message), CommunicationErrorType.TypeError, innerException: ex); } - break; - case SiemensDBDataType.DataType_Int: + obj = GetResult(Write(address, writeVal), address, writeVal); + } + break; + case SiemensDBDataType.DataType_DW: + { + uint writeVal; + try { - short writeVal; - try - { - writeVal = Convert.ToInt16(value); - } - catch (Exception ex) - { - throw new CommunicationException($"鍐欏叆鏁版嵁寮傚父,鏁版嵁绫诲瀷:銆恵dataType}銆�,鍦板潃:銆恵address}銆�,鏁版嵁:銆恵value}銆�,鏁版嵁绫诲瀷杞崲閿欒,閿欒淇℃伅:{ex.Message}", CommunicationErrorType.TypeError, innerException: ex); - } - obj = GetResult(Write(address, writeVal), address, writeVal); + writeVal = Convert.ToUInt32(value); } - - break; - case SiemensDBDataType.DataType_W: + catch (Exception ex) { - ushort writeVal; - try - { - writeVal = Convert.ToUInt16(value); - } - catch (Exception ex) - { - throw new CommunicationException($"鍐欏叆鏁版嵁寮傚父,鏁版嵁绫诲瀷:銆恵dataType}銆�,鍦板潃:銆恵address}銆�,鏁版嵁:銆恵value}銆�,鏁版嵁绫诲瀷杞崲閿欒,閿欒淇℃伅:{ex.Message}", CommunicationErrorType.TypeError, innerException: ex); - } - obj = GetResult(Write(address, writeVal), address, writeVal); + throw new CommunicationException(string.Format(CommunicationExceptionMessage.TypeConvertError, dataType, address, value, ex.Message), CommunicationErrorType.TypeError, innerException: ex); } - - break; - case SiemensDBDataType.DataType_Float: + obj = GetResult(Write(address, writeVal), address, writeVal); + } + break; + case SiemensDBDataType.DataType_Int: + { + short writeVal; + try { - float writeVal; - try - { - writeVal = Convert.ToSingle(value); - } - catch (Exception ex) - { - throw new CommunicationException($"鍐欏叆鏁版嵁寮傚父,鏁版嵁绫诲瀷:銆恵dataType}銆�,鍦板潃:銆恵address}銆�,鏁版嵁:銆恵value}銆�,鏁版嵁绫诲瀷杞崲閿欒,閿欒淇℃伅:{ex.Message}", CommunicationErrorType.TypeError, innerException: ex); - } - obj = GetResult(Write(address, writeVal), address, writeVal); + writeVal = Convert.ToInt16(value); } - - break; - case SiemensDBDataType.DataType_Bool: + catch (Exception ex) { - bool writeVal; - try - { - writeVal = Convert.ToBoolean(value); - } - catch (Exception ex) - { - throw new CommunicationException($"鍐欏叆鏁版嵁寮傚父,鏁版嵁绫诲瀷:銆恵dataType}銆�,鍦板潃:銆恵address}銆�,鏁版嵁:銆恵value}銆�,鏁版嵁绫诲瀷杞崲閿欒,閿欒淇℃伅:{ex.Message}", CommunicationErrorType.TypeError, innerException: ex); - } - obj = GetResult(Write(address, writeVal), address, writeVal); + throw new CommunicationException(string.Format(CommunicationExceptionMessage.TypeConvertError, dataType, address, value, ex.Message), CommunicationErrorType.TypeError, innerException: ex); } - - break; - case SiemensDBDataType.DataType_Byte: + obj = GetResult(Write(address, writeVal), address, writeVal); + } + break; + case SiemensDBDataType.DataType_W: + { + ushort writeVal; + try { - byte writeVal; - try - { - writeVal = Convert.ToByte(value); - } - catch (Exception ex) - { - throw new CommunicationException($"鍐欏叆鏁版嵁寮傚父,鏁版嵁绫诲瀷:銆恵dataType}銆�,鍦板潃:銆恵address}銆�,鏁版嵁:銆恵value}銆�,鏁版嵁绫诲瀷杞崲閿欒,閿欒淇℃伅:{ex.Message}", CommunicationErrorType.TypeError, innerException: ex); - } - obj = GetResult(Write(address, writeVal), address, writeVal); + writeVal = Convert.ToUInt16(value); } - - break; - case SiemensDBDataType.DataType_ByteArray: + catch (Exception ex) { - byte[] writeVal; - try - { - writeVal = (byte[])value; - } - catch (Exception ex) - { - throw new CommunicationException($"鍐欏叆鏁版嵁寮傚父,鏁版嵁绫诲瀷:銆恵dataType}銆�,鍦板潃:銆恵address}銆�,鏁版嵁:銆恵value}銆�,鏁版嵁绫诲瀷杞崲閿欒,閿欒淇℃伅:{ex.Message}", CommunicationErrorType.TypeError, innerException: ex); - } - obj = GetResult(Write(address, writeVal), address, writeVal); + throw new CommunicationException(string.Format(CommunicationExceptionMessage.TypeConvertError, dataType, address, value, ex.Message), CommunicationErrorType.TypeError, innerException: ex); } - - break; - case SiemensDBDataType.DataType_String: + obj = GetResult(Write(address, writeVal), address, writeVal); + } + break; + case SiemensDBDataType.DataType_Float: + { + float writeVal; + try { - string writeVal; - try - { - writeVal = value.ToString(); - } - catch (Exception ex) - { - throw new CommunicationException($"鍐欏叆鏁版嵁寮傚父,鏁版嵁绫诲瀷:銆恵dataType}銆�,鍦板潃:銆恵address}銆�,鏁版嵁:銆恵value}銆�,鏁版嵁绫诲瀷杞崲閿欒,閿欒淇℃伅:{ex.Message}", CommunicationErrorType.TypeError, innerException: ex); - } - obj = GetResult(Write(address, writeVal), address, writeVal); + writeVal = Convert.ToSingle(value); } + catch (Exception ex) + { + throw new CommunicationException(string.Format(CommunicationExceptionMessage.TypeConvertError, dataType, address, value, ex.Message), CommunicationErrorType.TypeError, innerException: ex); + } + obj = GetResult(Write(address, writeVal), address, writeVal); + } + break; + case SiemensDBDataType.DataType_Bool: + { + bool writeVal; + try + { + writeVal = Convert.ToBoolean(value); + } + catch (Exception ex) + { + throw new CommunicationException(string.Format(CommunicationExceptionMessage.TypeConvertError, dataType, address, value, ex.Message), CommunicationErrorType.TypeError, innerException: ex); + } + obj = GetResult(Write(address, writeVal), address, writeVal); + } + break; + case SiemensDBDataType.DataType_Byte: + { + byte writeVal; + try + { + writeVal = Convert.ToByte(value); + } + catch (Exception ex) + { + throw new CommunicationException(string.Format(CommunicationExceptionMessage.TypeConvertError, dataType, address, value, ex.Message), CommunicationErrorType.TypeError, innerException: ex); + } + obj = GetResult(Write(address, writeVal), address, writeVal); + } + break; + case SiemensDBDataType.DataType_String: + { + string writeVal; + try + { + writeVal = value.ToString(); + } + catch (Exception ex) + { + throw new CommunicationException(string.Format(CommunicationExceptionMessage.TypeConvertError, dataType, address, value, ex.Message), CommunicationErrorType.TypeError, innerException: ex); + } + obj = GetResult(Write(address, writeVal), address, writeVal); + } - break; - case SiemensDBDataType.DataType_Char: + break; + case SiemensDBDataType.DataType_Char: - //obj = GetResult(plc.Write(address, (int)value), address, (int)value); - break; - case SiemensDBDataType.DataType_CharArray: - - break; - default: - throw new CommunicationException($"鍐欏叆鏁版嵁澶辫触,鏈畾涔夋暟鎹被鍨�:銆恵dataType}銆�,鍦板潃:銆恵address}銆�,鏁版嵁:銆恵value}銆�", CommunicationErrorType.TypeError); - } - return obj; + break; + default: + throw new CommunicationException(string.Format(CommunicationExceptionMessage.DataTypeErrorException, dataType, address), CommunicationErrorType.TypeError); } - catch (CommunicationException ex) - { - //鍐欏叆寮傚父鏃舵姏鍑鸿嚜瀹氫箟閫氳寮傚父绫� - throw ex; - } - catch (Exception ex) - { - //鍐欏叆寮傚父鏃舵姏鍑鸿嚜瀹氫箟閫氳寮傚父绫� - throw new CommunicationException($"鍐欏叆鏁版嵁寮傚父,鏁版嵁绫诲瀷:銆恵dataType}銆�,鍦板潃:銆恵address}銆�,鏁版嵁:銆恵value}銆�,閿欒淇℃伅:{ex.Message}", CommunicationErrorType.TypeError, innerException: ex); - } + return obj; } #endregion @@ -634,65 +587,77 @@ { return plc.ReadCustomer<T>(address).Content; } - catch(Exception ex) + catch (Exception ex) { LogNet.WriteException(Name, $"銆恵Name}銆慞LC璇诲彇寮傚父锛屽湴鍧�锛氥�恵address}銆戯紝閿欒淇℃伅锛氥�恵ex.Message}銆�", ex); throw new CommunicationException(ex.Message, CommunicationErrorType.ReadException, innerException: ex); } - + } #endregion #region WriteCustomer - public override bool WriteCustomer<T>(string address, T value) + public override bool WriteCustomer<T>(string address, [NotNull] T value) { + StringBuilder stringBuilder = new StringBuilder(); try { OperateResult operateResult = plc.WriteCustomer(address, value); + stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteData, address, JsonConvert.SerializeObject(value))); if (operateResult.IsSuccess) { + object? obj = null; for (int i = 0; i < 5; i++) { T readValue = ReadCustomer<T>(address); + stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAfterRead, address, JsonConvert.SerializeObject(readValue))); + obj = readValue; PropertyInfo[] propertyInfos = typeof(T).GetProperties(); - foreach (var item in propertyInfos) + for (int j = 0; j < propertyInfos.Length; j++) { - object writeValueItem = item.GetValue(value); - if (writeValueItem != null) + object? writeValueItem = propertyInfos[j].GetValue(value); + object? readValueItem = propertyInfos[j].GetValue(readValue); + if (writeValueItem.Equals(readValueItem)) { - object readValueItem = item.GetValue(readValue); - if (writeValueItem.Equals(readValueItem)) - { - break; - } - else - { - plc.WriteCustomer(address, value); - } - throw new CommunicationException(string.Format(CommunicationExceptionMessage.ReadWriteDifferentException, typeof(T).Name, address, JsonConvert.SerializeObject(value), JsonConvert.SerializeObject(readValue)), CommunicationErrorType.WriteFailed); + stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAndReadCheckSuccess, address, JsonConvert.SerializeObject(value), JsonConvert.SerializeObject(readValue))); } + else + { + break; + } + if (j == propertyInfos.Length - 1) + return true; } + + 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); } else { throw new CommunicationException(string.Format(CommunicationExceptionMessage.WriteFailedException, typeof(T).Name, address, JsonConvert.SerializeObject(value), operateResult.Message), CommunicationErrorType.WriteFailed); } - return operateResult.IsSuccess; } - catch(Exception ex) + catch (Exception ex) { - LogNet.WriteException(Name, $"銆恵Name}銆慞LC鍐欏叆寮傚父锛屽湴鍧�锛氥�恵address}銆戯紝鍐欏叆鐨勬暟鎹細銆恵JsonConvert.SerializeObject(value)}銆戯紝閿欒淇℃伅锛氥�恵ex.Message}銆�", ex); + LogNet.WriteException(Name, ex.Message, ex); throw new CommunicationException(ex.Message, CommunicationErrorType.WriteFailed, innerException: ex); } - + finally + { + LogNet.WriteInfo(Name, stringBuilder.ToString()); + } + } #endregion // 鏄惧紡瀹炵幇IDisposable鎺ュ彛浠ユ彁渚涘瀮鍦惧洖鏀舵椂鐨勬竻鐞� - public void Dispose() + public override void Dispose() { + _isPing = false; Disconnect(); + plc.Dispose(); GC.SuppressFinalize(this); } -- Gitblit v1.9.3