| | |
| | | /// </summary> |
| | | public override bool IsConnected => _connected; |
| | | |
| | | /// <summary> |
| | | /// 是否在写入数据后读取数据确认。 |
| | | /// </summary> |
| | | public override bool IsReadAfterWrite { get; set; } = true; |
| | | |
| | | #endregion Public Member |
| | | |
| | | #region Constructor Function |
| | |
| | | } |
| | | else |
| | | { |
| | | object? obj = null; |
| | | for (int i = 0; i < 5; i++) |
| | | if (IsReadAfterWrite) |
| | | { |
| | | T readValue = Read<T>(address); |
| | | stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAfterRead, readValue, 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, readValue, 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) |
| | |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// |
| | | /// </summary> |
| | | public override void Dispose() |
| | | { |
| | | _isPing = false; |
| | |
| | | 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 |
| | | { |