From 70233af5426b0d1c343ebe87183303a34a9aaa58 Mon Sep 17 00:00:00 2001 From: hutongqing <hutongqing@hnkhzn.com> Date: 星期三, 20 十一月 2024 09:05:38 +0800 Subject: [PATCH] WIDESEAWCS_QuartzJob更新版本 --- WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs | 495 +++++++++++++++++++++++++++--------------------------- 1 files changed, 247 insertions(+), 248 deletions(-) diff --git a/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs b/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs index 8c7eea6..7ffe969 100644 --- a/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs +++ b/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs @@ -23,8 +23,11 @@ using System; 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; @@ -35,7 +38,8 @@ /// <summary> /// 瑗块棬瀛怱7閫氳绫� /// </summary> - public class SiemensS7 : BaseCommunicator, IDisposable + [Description("瑗块棬瀛怱7")] + public class SiemensS7 : BaseCommunicator { #region Private Member /// <summary> @@ -58,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 @@ -69,6 +78,9 @@ /// </summary> public override bool IsConnected => _connected; + /// <summary> + /// PLC鍚嶇О + /// </summary> public override string Name => _name; public override ILogNet LogNet => _logNet; @@ -80,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) @@ -106,13 +119,21 @@ /// <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) { - if (!operateResult.IsSuccess) + try { - throw new CommunicationException(string.Format(CommunicationExceptionMessage.ReadFailedException, typeof(T).Name, address, operateResult.Message), CommunicationErrorType.ReadFailed); + if (!operateResult.IsSuccess) + { + throw new CommunicationException(string.Format(CommunicationExceptionMessage.ReadFailedException, typeof(T).Name, address, operateResult.Message), CommunicationErrorType.ReadFailed); + } + return operateResult.Content ?? throw new CommunicationException(string.Format(CommunicationExceptionMessage.ReadDataIsNull, address), CommunicationErrorType.ReadFailed); } - return operateResult.Content; + catch (Exception ex) + { + LogNet.WriteException(Name, ex.Message, ex); + throw new CommunicationException(ex.Message, CommunicationErrorType.ReadFailed, innerException: ex); + } } /// <summary> @@ -126,27 +147,44 @@ /// <exception cref="CommunicationException"></exception> private bool GetResult<T>(OperateResult operateResult, string address, T value) where T : notnull { - if (!operateResult.IsSuccess) + StringBuilder stringBuilder = new StringBuilder(); + try { - throw new CommunicationException(string.Format(CommunicationExceptionMessage.WriteFailedException, typeof(T).Name, address, value, operateResult.Message), CommunicationErrorType.WriteFailed); - } - else - { - object? obj = null; - for (int i = 0; i < 5; i++) + stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteData, address, value)); + if (!operateResult.IsSuccess) { - T readValue = Read<T>(address); - obj = readValue; - if (readValue.Equals(value)) - { - return true; - } - else - { - Write(address, value); - } + throw new CommunicationException(string.Format(CommunicationExceptionMessage.WriteFailedException, typeof(T).Name, address, value, operateResult.Message), CommunicationErrorType.WriteFailed); } - throw new CommunicationException(string.Format(CommunicationExceptionMessage.ReadWriteDifferentException, typeof(T).Name, address, value, obj), CommunicationErrorType.WriteFailed); + else + { + object? obj = null; + for (int i = 0; i < 5; i++) + { + 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); + } + } + stringBuilder.AppendLine(string.Format(CommunicationExceptionMessage.WriteAndReadCheckFaild, address, value, obj)); + throw new CommunicationException(stringBuilder.ToString(), CommunicationErrorType.WriteFailed); + } + } + catch (Exception ex) + { + LogNet.WriteException(Name, ex.Message, ex); + throw new CommunicationException(ex.Message, CommunicationErrorType.WriteFailed, innerException: ex); + } + finally + { + LogNet.WriteInfo(Name, stringBuilder.ToString()); } } @@ -189,12 +227,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); } } @@ -223,19 +261,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 @@ -259,15 +320,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); } @@ -308,20 +369,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> @@ -334,22 +382,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> @@ -361,20 +395,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 @@ -404,7 +425,7 @@ catch (Exception ex) { //鍐欏叆寮傚父鏃舵姏鍑鸿嚜瀹氫箟閫氳寮傚父绫� - throw new CommunicationException($"鍐欏叆鏁版嵁寮傚父,鍦板潃:銆恵address}銆�,閿欒淇℃伅: {ex.Message}", CommunicationErrorType.ReadFailed, innerException: ex); + throw new CommunicationException($"鍐欏叆鏁版嵁寮傚父,鍦板潃:銆恵address}銆�,閿欒淇℃伅: {ex.Message}", CommunicationErrorType.WriteFailed, innerException: ex); } } @@ -418,20 +439,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> @@ -442,219 +450,210 @@ /// <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 #region ReadCustomer public override T ReadCustomer<T>(string address) { - return plc.ReadCustomer<T>(address).Content; + try + { + return plc.ReadCustomer<T>(address).Content; + } + 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) { - OperateResult operateResult = plc.WriteCustomer(address, value); - if (operateResult.IsSuccess) + StringBuilder stringBuilder = new StringBuilder(); + try { - for (int i = 0; i < 5; i++) + OperateResult operateResult = plc.WriteCustomer(address, value); + stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteData, address, JsonConvert.SerializeObject(value))); + if (operateResult.IsSuccess) { - T readValue = ReadCustomer<T>(address); - PropertyInfo[] propertyInfos = typeof(T).GetProperties(); - foreach (var item in propertyInfos) + object? obj = null; + for (int i = 0; i < 5; i++) { - object writeValueItem = item.GetValue(value); - if (writeValueItem != null) + 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 readValueItem = item.GetValue(readValue); + object? writeValueItem = propertyInfos[j].GetValue(value); + object? readValueItem = propertyInfos[j].GetValue(readValue); if (writeValueItem.Equals(readValueItem)) { - break; + stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAndReadCheckSuccess, address, JsonConvert.SerializeObject(value), JsonConvert.SerializeObject(readValue))); } else { - plc.WriteCustomer(address, value); + break; } - throw new CommunicationException(string.Format(CommunicationExceptionMessage.ReadWriteDifferentException, typeof(T).Name, address, JsonConvert.SerializeObject(value), JsonConvert.SerializeObject(readValue)), CommunicationErrorType.WriteFailed); + 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); } } - else + catch (Exception ex) { - throw new CommunicationException(string.Format(CommunicationExceptionMessage.WriteFailedException, typeof(T).Name, address, JsonConvert.SerializeObject(value), operateResult.Message), CommunicationErrorType.WriteFailed); + LogNet.WriteException(Name, ex.Message, ex); + throw new CommunicationException(ex.Message, CommunicationErrorType.WriteFailed, innerException: ex); } - return operateResult.IsSuccess; + 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