1
hutongqing
2025-01-02 8c6fd742db249ad4cc819cf041eb98d880a3ef73
WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs
@@ -20,11 +20,15 @@
using HslCommunication.LogNet;
using HslCommunication.Profinet.Siemens;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
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 +39,8 @@
    /// <summary>
    /// 西门子S7通讯类
    /// </summary>
    public class SiemensS7 : BaseCommunicator, IDisposable
    [Description("西门子S7")]
    public class SiemensS7 : BaseCommunicator
    {
        #region Private Member
        /// <summary>
@@ -58,9 +63,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,8 +79,14 @@
        /// </summary>
        public override bool IsConnected => _connected;
        /// <summary>
        /// PLC名称
        /// </summary>
        public override string Name => _name;
        /// <summary>
        /// PLC读写日志记录
        /// </summary>
        public override ILogNet LogNet => _logNet;
        #endregion Public Member
@@ -80,10 +96,11 @@
        /// </summary>
        /// <param name="ipAddress">设备的IP地址</param>
        /// <param name="port">连接使用的端口号</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 +123,21 @@
        /// <param name="operateResult">HSLCommunication读取的OperateResult<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 +151,48 @@
        /// <exception cref="CommunicationException"></exception>
        private bool GetResult<T>(OperateResult operateResult, string address, T value) where T : notnull
        {
            if (!operateResult.IsSuccess)
            if (value is Array)
            {
                throw new CommunicationException(string.Format(CommunicationExceptionMessage.WriteFailedException, typeof(T).Name, address, value, operateResult.Message), CommunicationErrorType.WriteFailed);
                return operateResult.IsSuccess;
            }
            else
            StringBuilder stringBuilder = new StringBuilder();
            try
            {
                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());
            }
        }
@@ -184,58 +230,138 @@
                    case TypeCode.Char:
                        return plc.Write(address, Convert.ToChar(value));
                    default:
                        if (value is int[])
                        {
                            return plc.Write(address, value as int[]);
                        }
                        else if (value is uint[])
                        {
                            return plc.Write(address, value as uint[]);
                        }
                        else if (value is short[])
                        {
                            return plc.Write(address, value as short[]);
                        }
                        else if (value is ushort[])
                        {
                            return plc.Write(address, value as ushort[]);
                        }
                        else if (value is bool[])
                        {
                            return plc.Write(address, value as bool[]);
                        }
                        else if (value is float[])
                        {
                            return plc.Write(address, value as float[]);
                        }
                        else if (value is double[])
                        {
                            return plc.Write(address, value as double[]);
                        }
                        else if (value is byte[])
                        {
                            return plc.Write(address, value as byte[]);
                        }
                        throw new CommunicationException(string.Format(CommunicationExceptionMessage.DataTypeErrorException, type.Name, address), CommunicationErrorType.TypeError);
                }
            }
            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);
            }
        }
        private object Read(string address, TypeCode typeCode)
        private object Read(string address, TypeCode typeCode, ushort length = 1)
        {
            try
            {
                switch (typeCode)
                {
                    case TypeCode.Int32:
                        return (int)GetContent(plc.ReadInt32(address), address);
                        if (length > 1)
                            return (int[])GetContent(plc.ReadInt32(address, length), address);
                        else
                            return (int)GetContent(plc.ReadInt32(address), address);
                    case TypeCode.UInt32:
                        return (uint)GetContent(plc.ReadUInt32(address), address);
                        if (length > 1)
                            return (uint[])GetContent(plc.ReadUInt32(address, length), address);
                        else
                            return (uint)GetContent(plc.ReadUInt32(address), address);
                    case TypeCode.Int16:
                        return (short)GetContent(plc.ReadInt16(address), address);
                        if (length > 1)
                            return (short[])GetContent(plc.ReadInt16(address, length), address);
                        else
                            return (short)GetContent(plc.ReadInt16(address), address);
                    case TypeCode.UInt16:
                        return (ushort)GetContent(plc.ReadUInt16(address), address);
                        if (length > 1)
                            return (ushort[])GetContent(plc.ReadUInt16(address, length), address);
                        else
                            return (ushort)GetContent(plc.ReadUInt16(address), address);
                    case TypeCode.Single:
                        return (float)GetContent(plc.ReadFloat(address), address);
                        if (length > 1)
                            return (float[])GetContent(plc.ReadFloat(address, length), address);
                        else
                            return (float)GetContent(plc.ReadFloat(address), address);
                    case TypeCode.Boolean:
                        return (bool)GetContent(plc.ReadBool(address), address);
                        if (length > 1)
                            return (bool[])GetContent(plc.ReadBool(address, length), address);
                        else
                            return (bool)GetContent(plc.ReadBool(address), address);
                    case TypeCode.Byte:
                        if (length > 1)
                            return (byte)GetContent(plc.Read(address, length), address);
                        return (byte)GetContent(plc.ReadByte(address), address);
                    case TypeCode.String:
                        return (string)GetContent(plc.ReadString(address), address);
                        if (length > 1)
                            return (string)GetContent(plc.ReadString(address, length), address);
                        else
                            return (string)GetContent(plc.ReadString(address), address);
                    case TypeCode.Char:
                        if (length > 1)
                            return (char)GetContent(plc.Read(address, length), address);
                        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 +385,15 @@
                _connected = operateResult.IsSuccess;//将连接是否成功赋值给当前通讯器是否已连接到PLC
                if (_connected)
                    LogNet.WriteInfo(Name, $"【{Name}】PLC连接成功,IP【{_ipAddress}】,Port【{_port}】");
                    LogNet.WriteInfo(Name, string.Format(CommunicationInfoMessage.ConnectSuccess, _ipAddress, _port));
                else
                    LogNet.WriteError(Name, $"【{Name}】PLC连接失败,IP【{_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}】PLC连接异常,IP【{_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 +434,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 +447,14 @@
        /// <exception cref="CommunicationException">自定义通讯异常类</exception>
        public override T Read<T>(string address)
        {
            try
            {
                Type type = typeof(T);
                return (T)Read(address, Type.GetTypeCode(type));
            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);
            }
        public override T[] Read<T>(string address, ushort length)
        {
            Type type = typeof(T);
            return (T[])Read(address, Type.GetTypeCode(type), length);
        }
        /// <summary>
@@ -361,20 +466,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 +496,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 +510,13 @@
        /// <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);
        }
        public override bool Write<T>(string address, T[] values)
        {
            return GetResult(Write(address, values), address, values);
        }
        /// <summary>
@@ -442,222 +527,224 @@
        /// <param name="value">要写入的数据。</param>
        /// <returns>如果写入成功则返回true,失败则抛出自定义通讯异常。</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}】PLC读取异常,地址:【{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()
        /// <summary>
        /// 显式实现IDisposable接口以提供垃圾回收时的清理
        /// </summary>
        public override void Dispose()
        {
            _isPing = false;
            Disconnect();
            plc.Dispose();
            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));