1
hutongqing
2024-08-20 d8d11d4d8a9178e02e39a8cee435290dec7a3b0b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#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; }
 
        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
    {
        IpAddressError,
        ConnectionFailed,
        Unknown,
        ReadFailed,
        WriteFailed,
        TypeError,
        ReadException
    }
 
    public class CommunicationExceptionMessage
    {
        public const string IpAddressErrorException = "Ip地址错误,【{0}】";
 
        public const string ReadFailedException = "数据读取失败,数据类型:【{0}】,地址:【{1}】,错误信息:【{2}】";
 
        public const string WriteFailedException = "数据写入失败,数据类型:【{0}】,地址:【{1}】,写入的数据:【{2}】,错误信息:【{3}】";
 
        public const string ReadWriteDifferentException = "数据写入失败,写入后再读取与写入值不一致,数据类型:【{0}】,地址:【{1}】,写入的数据:【{2}】,写入后读取的数据:【{3}】";
 
        public const string DataTypeErrorException = "数据类型错误,未定义该PLC数据类型:【{0}】,地址:【{1}】";
    }
 
    public class CommunicationInfoMessage
    {
 
    }
}