| #region << 版 本 注 释 >> | 
| /*---------------------------------------------------------------- | 
|  * 命名空间:WIDESEAWCS_Communicator | 
|  * 创建者:胡童庆 | 
|  * 创建时间:2024/8/2 16:13:36 | 
|  * 版本:V1.0.0 | 
|  * 描述:自定义通讯异常类,定义了错误类型、错误代码(暂时没定义错误代码表) | 
|  * | 
|  * ---------------------------------------------------------------- | 
|  * 修改人: | 
|  * 修改时间: | 
|  * 版本:V1.0.1 | 
|  * 修改说明: | 
|  *  | 
|  *----------------------------------------------------------------*/ | 
| #endregion << 版 本 注 释 >> | 
|   | 
| using HslCommunication.LogNet; | 
| using System; | 
| using System.Collections.Generic; | 
| using System.Linq; | 
| using System.Text; | 
| using System.Threading.Tasks; | 
|   | 
| namespace WIDESEAWCS_Communicator | 
| { | 
|     /// <summary>   | 
|     /// 工业通信错误类,封装了与PLC通信时可能发生的错误。   | 
|     /// </summary>   | 
|     [Serializable] // 确保类支持序列化  | 
|     public class CommunicationException : Exception | 
|     { | 
|         //public static ILogNet logNet = new LogNetFileSize() | 
|   | 
|         /// <summary>   | 
|         /// 错误代码。   | 
|         /// </summary>   | 
|         public int? ErrorCode { get; } | 
|   | 
|         /// <summary>   | 
|         /// 通信错误的类型。   | 
|         /// </summary>   | 
|         public CommunicationErrorType ErrorType { get; } | 
|   | 
|         /// <summary> | 
|         /// 错误信息 | 
|         /// </summary> | 
|         public override string Message => _message; | 
|   | 
|         private string _message; | 
|   | 
|         /// <summary>   | 
|         /// 初始化一个新的 CommunicationException 实例。   | 
|         /// </summary>   | 
|         /// <param name="message">错误的描述。</param>   | 
|         /// <param name="plcErrorCode">错误代码(可选)。</param>   | 
|         /// <param name="innerException">导致当前异常的异常(可选)。</param>   | 
|         public CommunicationException(string message, CommunicationErrorType errorType, int? plcErrorCode = null, Exception innerException = null) | 
|             : base(message, innerException) | 
|         { | 
|             ErrorCode = plcErrorCode; | 
|             ErrorType = errorType; | 
|             _message = message; | 
|         } | 
|   | 
|         /// <summary>   | 
|         /// 提供一个更友好的字符串表示形式,包含PLC错误代码(如果可用)和任何内部异常信息。   | 
|         /// </summary>   | 
|         /// <returns>异常的字符串表示形式。</returns>   | 
|         public override string ToString() | 
|         { | 
|             //string plcCodeInfo = ErrorCode.HasValue ? $"PLC Error Code: {ErrorCode.Value}" : "No PLC Error Code"; | 
|             string innerExceptionInfo = InnerException != null ? $"\nInner Exception: {InnerException.Message}" : ""; | 
|             return $"{GetType().Name}: {Message}\n{innerExceptionInfo}"; | 
|         } | 
|     } | 
|   | 
|     /// <summary>   | 
|     /// 工业通信错误类型枚举。   | 
|     /// </summary>   | 
|     public enum CommunicationErrorType | 
|     { | 
|         /// <summary> | 
|         /// IP地址错误 | 
|         /// </summary> | 
|         IpAddressError, | 
|         /// <summary> | 
|         /// 连接PLC错误 | 
|         /// </summary> | 
|         ConnectionFailed, | 
|         /// <summary> | 
|         /// 未知类型错误 | 
|         /// </summary> | 
|         Unknown, | 
|         /// <summary> | 
|         /// 读取失败 | 
|         /// </summary> | 
|         ReadFailed, | 
|         /// <summary> | 
|         /// 写入失败 | 
|         /// </summary> | 
|         WriteFailed, | 
|         /// <summary> | 
|         /// 类型错误 | 
|         /// </summary> | 
|         TypeError, | 
|         /// <summary> | 
|         /// 读取异常 | 
|         /// </summary> | 
|         ReadException | 
|     } | 
|   | 
|     public class CommunicationExceptionMessage | 
|     { | 
|         /// <summary> | 
|         /// Ip地址错误,参数【IP】 | 
|         /// </summary> | 
|         public const string IpAddressErrorException = "Ip地址错误,【{0}】"; | 
|   | 
|         /// <summary> | 
|         /// 数据读取失败,参数【数据类型,协议地址,错误信息】 | 
|         /// </summary> | 
|         public const string ReadFailedException = "数据读取失败,数据类型:【{0}】,地址:【{1}】,错误信息:【{2}】"; | 
|   | 
|         /// <summary> | 
|         /// 数据写入失败,参数【数据类型,协议地址,写入的数据,错误信息】 | 
|         /// </summary> | 
|         public const string WriteFailedException = "数据写入失败,数据类型:【{0}】,地址:【{1}】,写入的数据:【{2}】,错误信息:【{3}】"; | 
|   | 
|         /// <summary> | 
|         /// 数据类型错误,参数【数据类型,协议地址】 | 
|         /// </summary> | 
|         public const string DataTypeErrorException = "数据类型错误,未定义该PLC数据类型:【{0}】,地址:【{1}】"; | 
|   | 
|         /// <summary> | 
|         /// 数据读取异常,参数【数据类型,协议地址,错误信息】 | 
|         /// </summary> | 
|         public const string ReadException = "数据读取失败,数据类型:【{0}】,地址:【{1}】,错误信息:【{2}】"; | 
|   | 
|         /// <summary> | 
|         /// 数据读取失败,参数【数据类型,协议地址,错误信息】 | 
|         /// </summary> | 
|         public const string WriteAndReadCheckFaild = "数据写入后读取校验失败,地址:【{0}】,写入的数据:【{1}】,读取的数据:【{2}】"; | 
|   | 
|         /// <summary> | 
|         /// 数据读取错误,结果为null,参数【协议地址】 | 
|         /// </summary> | 
|         public const string ReadDataIsNull = "数据读取错误,未取到数据结果,地址:【{0}】"; | 
|   | 
|         /// <summary> | 
|         /// 连接失败,参数【IP,Port,错误信息】 | 
|         /// </summary> | 
|         public const string ConnectFaild = "PLC连接失败,IP:【{0}】,Port:【{1}】,错误信息:【{2}】"; | 
|   | 
|         /// <summary> | 
|         /// 连接异常,参数【IP,Port,错误信息】 | 
|         /// </summary> | 
|         public const string ConnectException = "PLC连接异常,IP:【{0}】,Port:【{1}】,错误信息:【{2}】"; | 
|   | 
|         /// <summary> | 
|         /// 数据类型转化错误,参数【数据类型,协议地址,写入的数据,错误信息】 | 
|         /// </summary> | 
|         public const string TypeConvertError = "数据类型转换错误,数据类型:【{0}】,地址:【{1}】,写入的数据:【{2}】,错误信息:【{3}】"; | 
|     } | 
|   | 
|     public class CommunicationInfoMessage | 
|     { | 
|         /// <summary> | 
|         /// 数据写入,参数【协议地址,写入的数据】 | 
|         /// </summary> | 
|         public const string WriteData = "数据写入,地址:【{0}】,写入的数据:【{1}】"; | 
|   | 
|         /// <summary> | 
|         /// 数据写入后读取,参数【协议地址,读取的数据】 | 
|         /// </summary> | 
|         public const string WriteAfterRead = "数据写入后读取,地址:【{0}】,读取的数据:【{1}】"; | 
|   | 
|         /// <summary> | 
|         /// 数据写入后读取校验成功,参数【协议地址,写入的数据,读取的数据】 | 
|         /// </summary> | 
|         public const string WriteAndReadCheckSuccess = "数据写入后读取校验成功,地址:【{0}】,写入的数据:【{1}】,读取的数据:【{2}】"; | 
|   | 
|         /// <summary> | 
|         /// 连接成功,参数【IP,Port】 | 
|         /// </summary> | 
|         public const string ConnectSuccess = "PLC连接成功,IP:【{0}】,Port:【{1}】"; | 
|     } | 
| } |