1
z8018
2025-03-12 17e4c7e3e7b3ef60d9da6de3b2a39a14a53c38a0
WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs
@@ -84,7 +84,15 @@
        /// </summary>
        public override string Name => _name;
        /// <summary>
        /// PLC读写日志记录
        /// </summary>
        public override ILogNet LogNet => _logNet;
        /// <summary>
        /// 是否在写入数据后读取数据确认。
        /// </summary>
        public override bool IsReadAfterWrite { get; set; } = true;
        #endregion Public Member
        #region Constructor Function
@@ -162,24 +170,31 @@
                }
                else
                {
                    object? obj = null;
                    for (int i = 0; i < 5; i++)
                    if (IsReadAfterWrite)
                    {
                        T readValue = Read<T>(address);
                        stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAfterRead, address, value));
                        obj = readValue;
                        if (readValue.Equals(value))
                        object? obj = null;
                        for (int i = 0; i < 5; i++)
                        {
                            stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAndReadCheckSuccess, address, value, readValue));
                            return true;
                            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);
                            }
                        }
                        else if (i < 4)
                        {
                            Write(address, value);
                        }
                        stringBuilder.AppendLine(string.Format(CommunicationExceptionMessage.WriteAndReadCheckFaild, address, value, obj));
                        throw new CommunicationException(stringBuilder.ToString(), CommunicationErrorType.WriteFailed);
                    }
                    stringBuilder.AppendLine(string.Format(CommunicationExceptionMessage.WriteAndReadCheckFaild, address, value, obj));
                    throw new CommunicationException(stringBuilder.ToString(), CommunicationErrorType.WriteFailed);
                    else
                    {
                        return true;
                    }
                }
            }
            catch (Exception ex)
@@ -653,6 +668,12 @@
        #endregion
        #region ReadCustomer
        /// <summary>
        /// 读取自定义的数据类型,需要继承自IDataTransfer接口,返回一个新的类型的实例对象。
        /// </summary>
        /// <typeparam name="T">自定义的数据类型泛型。</typeparam>
        /// <param name="address">源地址,具体格式取决于使用的工业协议。</param>
        /// <returns>成功返回自定义类型数据,失败抛出异常。</returns>
        public override T ReadCustomer<T>(string address)
        {
            try
@@ -668,6 +689,13 @@
        #endregion
        #region WriteCustomer
        /// <summary>
        /// 写入自定义类型的数据,该类型必须继承自IDataTransfer接口。
        /// </summary>
        /// <typeparam name="T">自定义的数据类型泛型。</typeparam>
        /// <param name="address">源地址,具体格式取决于使用的工业协议。</param>
        /// <param name="value">要写入数据。</param>
        /// <returns>如果写入成功则返回true,失败则抛出异常。</returns>
        public override bool WriteCustomer<T>(string address, [NotNull] T value)
        {
            StringBuilder stringBuilder = new StringBuilder();
@@ -677,33 +705,40 @@
                stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteData, address, JsonConvert.SerializeObject(value)));
                if (operateResult.IsSuccess)
                {
                    object? obj = null;
                    for (int i = 0; i < 5; i++)
                    if (IsReadAfterWrite)
                    {
                        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? obj = null;
                        for (int i = 0; i < 5; i++)
                        {
                            object? writeValueItem = propertyInfos[j].GetValue(value);
                            object? readValueItem = propertyInfos[j].GetValue(readValue);
                            if (writeValueItem.Equals(readValueItem))
                            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++)
                            {
                                stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAndReadCheckSuccess, address, JsonConvert.SerializeObject(value), JsonConvert.SerializeObject(readValue)));
                                object? writeValueItem = propertyInfos[j].GetValue(value);
                                object? readValueItem = propertyInfos[j].GetValue(readValue);
                                if (writeValueItem.Equals(readValueItem))
                                {
                                    stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAndReadCheckSuccess, address, JsonConvert.SerializeObject(value), JsonConvert.SerializeObject(readValue)));
                                }
                                else
                                {
                                    break;
                                }
                                if (j == propertyInfos.Length - 1)
                                    return true;
                            }
                            else
                            {
                                break;
                            }
                            if (j == propertyInfos.Length - 1)
                                return true;
                        }
                        plc.WriteCustomer(address, value);
                            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);
                    }
                    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
                    {
                        return true;
                    }
                }
                else
                {
@@ -722,7 +757,9 @@
        }
        #endregion
        // 显式实现IDisposable接口以提供垃圾回收时的清理
        /// <summary>
        /// 显式实现IDisposable接口以提供垃圾回收时的清理
        /// </summary>
        public override void Dispose()
        {
            _isPing = false;
@@ -731,6 +768,15 @@
            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));