| | |
| | | using System.Collections; |
| | | using System.Collections.Generic; |
| | | using System.ComponentModel; |
| | | using System.Diagnostics.CodeAnalysis; |
| | | using System.Linq; |
| | | using System.Net; |
| | | using System.Reflection; |
| | |
| | | /// </summary> |
| | | private bool _connected; |
| | | |
| | | /// <summary> |
| | | /// PLC名称 |
| | | /// </summary> |
| | | private string _name; |
| | | |
| | | private ILogNet _logNet; |
| | |
| | | /// </summary> |
| | | public override bool IsConnected => _connected; |
| | | |
| | | /// <summary> |
| | | /// PLC名称 |
| | | /// </summary> |
| | | public override string Name => _name; |
| | | |
| | | public override ILogNet LogNet => _logNet; |
| | |
| | | /// <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) |
| | | { |
| | | try |
| | | { |
| | |
| | | { |
| | | throw new CommunicationException(string.Format(CommunicationExceptionMessage.ReadFailedException, typeof(T).Name, address, operateResult.Message), CommunicationErrorType.ReadFailed); |
| | | } |
| | | return operateResult.Content; |
| | | return operateResult.Content ?? throw new CommunicationException(string.Format(CommunicationExceptionMessage.ReadDataIsNull, address), CommunicationErrorType.ReadFailed); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogNet.WriteException(Name, $"【{Name}】PLC读取异常,地址:【{address}】,错误信息:【{ex.Message}】", ex); |
| | | throw new CommunicationException(ex.Message, CommunicationErrorType.ReadException, innerException: ex); |
| | | LogNet.WriteException(Name, ex.Message, ex); |
| | | throw new CommunicationException(ex.Message, CommunicationErrorType.ReadFailed, innerException: ex); |
| | | } |
| | | |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// <exception cref="CommunicationException"></exception> |
| | | private bool GetResult<T>(OperateResult operateResult, string address, T value) where T : notnull |
| | | { |
| | | string logMsg = ""; |
| | | StringBuilder stringBuilder = new StringBuilder(); |
| | | try |
| | | { |
| | | logMsg += $"【{Name}】PLC写入数据,地址:【{address}】,写入的数据:【{value}】{Environment.NewLine}"; |
| | | stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteData, address, value)); |
| | | if (!operateResult.IsSuccess) |
| | | { |
| | | throw new CommunicationException(string.Format(CommunicationExceptionMessage.WriteFailedException, typeof(T).Name, address, value, operateResult.Message), CommunicationErrorType.WriteFailed); |
| | |
| | | for (int i = 0; i < 5; i++) |
| | | { |
| | | T readValue = Read<T>(address); |
| | | logMsg += $"【{Name}】PLC写入后读取数据,地址:【{address}】,读取的数据:【{readValue}】{Environment.NewLine}"; |
| | | stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAfterRead, readValue, value)); |
| | | obj = readValue; |
| | | if (readValue.Equals(value)) |
| | | { |
| | | logMsg += $"【{Name}】PLC写入后读取数据校验成功,地址:【{address}】,读取的数据:【{readValue}】,写入的数据:【{value}】{Environment.NewLine}"; |
| | | stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAndReadCheckSuccess, address, value, readValue)); |
| | | return true; |
| | | } |
| | | else if(i < 4) |
| | | else if (i < 4) |
| | | { |
| | | Write(address, value); |
| | | } |
| | | } |
| | | |
| | | logMsg += $"【{Name}】PLC写入后读取数据校验失败,地址:【{address}】,读取的数据:【{obj}】,写入的数据:【{value}】{Environment.NewLine}"; |
| | | |
| | | throw new CommunicationException(string.Format(CommunicationExceptionMessage.ReadWriteDifferentException, typeof(T).Name, address, value, obj), CommunicationErrorType.WriteFailed); |
| | | stringBuilder.AppendLine(string.Format(CommunicationExceptionMessage.WriteAndReadCheckFaild, address, value, obj)); |
| | | throw new CommunicationException(stringBuilder.ToString(), CommunicationErrorType.WriteFailed); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogNet.WriteException(Name, $"【{Name}】PLC写入异常,地址:【{address}】,写入的数据:【{value}】,错误信息:【{ex.Message}】", ex); |
| | | LogNet.WriteException(Name, ex.Message, ex); |
| | | throw new CommunicationException(ex.Message, CommunicationErrorType.WriteFailed, innerException: ex); |
| | | } |
| | | finally |
| | | { |
| | | LogNet.WriteInfo(Name, logMsg); |
| | | LogNet.WriteInfo(Name, stringBuilder.ToString()); |
| | | } |
| | | |
| | | } |
| | |
| | | default: |
| | | 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); |
| | | } |
| | | finally |
| | | { |
| | | |
| | | throw new CommunicationException(string.Format(CommunicationExceptionMessage.DataTypeErrorException, address, value), CommunicationErrorType.TypeError, innerException: ex); |
| | | } |
| | | } |
| | | |
| | |
| | | case TypeCode.Char: |
| | | 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) |
| | | { |
| | |
| | | _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)); |
| | | |
| | | 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); |
| | | } |
| | |
| | | /// <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> |
| | |
| | | /// <exception cref="CommunicationException">自定义通讯异常类</exception> |
| | | public override T Read<T>(string address) |
| | | { |
| | | try |
| | | { |
| | | 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); |
| | | } |
| | | Type type = typeof(T); |
| | | return (T)Read(address, Type.GetTypeCode(type)); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// <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 |
| | | |
| | |
| | | /// <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); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// <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 |
| | | |
| | |
| | | #endregion |
| | | |
| | | #region WriteCustomer |
| | | public override bool WriteCustomer<T>(string address, T value) |
| | | public override bool WriteCustomer<T>(string address, [NotNull] T value) |
| | | { |
| | | string logMsg = string.Empty; |
| | | StringBuilder stringBuilder = new StringBuilder(); |
| | | try |
| | | { |
| | | OperateResult operateResult = plc.WriteCustomer(address, value); |
| | | logMsg += $"【{Name}】PLC写入数据,地址:【{address}】,写入的数据:【{JsonConvert.SerializeObject(value)}】{Environment.NewLine}"; |
| | | stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteData, address, JsonConvert.SerializeObject(value))); |
| | | if (operateResult.IsSuccess) |
| | | { |
| | | object? obj = null; |
| | | for (int i = 0; i < 5; i++) |
| | | { |
| | | T readValue = ReadCustomer<T>(address); |
| | | logMsg += $"【{Name}】PLC写入后读取数据,地址:【{address}】,读取的数据:【{JsonConvert.SerializeObject(readValue)}】{Environment.NewLine}"; |
| | | stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAfterRead, address, JsonConvert.SerializeObject(readValue))); |
| | | obj = readValue; |
| | | PropertyInfo[] propertyInfos = typeof(T).GetProperties(); |
| | | foreach (var item in propertyInfos) |
| | | for (int j = 0; j < propertyInfos.Length; j++) |
| | | { |
| | | object writeValueItem = item.GetValue(value); |
| | | if (writeValueItem != null) |
| | | object? writeValueItem = propertyInfos[j].GetValue(value); |
| | | object? readValueItem = propertyInfos[j].GetValue(readValue); |
| | | if (writeValueItem.Equals(readValueItem)) |
| | | { |
| | | object readValueItem = item.GetValue(readValue); |
| | | if (writeValueItem.Equals(readValueItem)) |
| | | { |
| | | logMsg += $"【{Name}】PLC写入后读取数据校验成功,地址:【{address}】,读取的数据:【{JsonConvert.SerializeObject(readValue)}】,写入的数据:【{JsonConvert.SerializeObject(value)}】{Environment.NewLine}"; |
| | | return true; |
| | | } |
| | | else |
| | | { |
| | | plc.WriteCustomer(address, value); |
| | | } |
| | | |
| | | logMsg += $"【{Name}】PLC写入后读取数据校验失败,地址:【{address}】,读取的数据:【{JsonConvert.SerializeObject(readValue)}】,写入的数据:【{JsonConvert.SerializeObject(value)}】{Environment.NewLine}"; |
| | | throw new CommunicationException(string.Format(CommunicationExceptionMessage.ReadWriteDifferentException, typeof(T).Name, address, JsonConvert.SerializeObject(value), JsonConvert.SerializeObject(readValue)), CommunicationErrorType.WriteFailed); |
| | | stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAndReadCheckSuccess, address, JsonConvert.SerializeObject(value), JsonConvert.SerializeObject(readValue))); |
| | | } |
| | | else |
| | | { |
| | | break; |
| | | } |
| | | 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); |
| | | } |
| | | return operateResult.IsSuccess; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogNet.WriteException(Name, $"【{Name}】PLC写入异常,地址:【{address}】,写入的数据:【{JsonConvert.SerializeObject(value)}】,错误信息:【{ex.Message}】", ex); |
| | | LogNet.WriteException(Name, ex.Message, ex); |
| | | throw new CommunicationException(ex.Message, CommunicationErrorType.WriteFailed, innerException: ex); |
| | | } |
| | | finally |
| | | { |
| | | LogNet.WriteInfo(Name, logMsg); |
| | | LogNet.WriteInfo(Name, stringBuilder.ToString()); |
| | | } |
| | | |
| | | } |