| | |
| | | /// <exception cref="CommunicationException"></exception> |
| | | private bool GetResult<T>(OperateResult operateResult, string address, T value) where T : notnull |
| | | { |
| | | string logMsg = ""; |
| | | try |
| | | { |
| | | logMsg += $"【{Name}】PLC写入数据,地址:【{address}】,写入的数据:【{value}】{Environment.NewLine}"; |
| | | 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}】,读取的数据:【{value}】{Environment.NewLine}"; |
| | | obj = readValue; |
| | | if (readValue.Equals(value)) |
| | | { |
| | | logMsg += $"【{Name}】PLC写入后读取数据校验成功,地址:【{address}】,读取的数据:【{value}】,写入的数据:【{value}】{Environment.NewLine}"; |
| | | return true; |
| | | } |
| | | else |
| | | else if(i < 4) |
| | | { |
| | | Write(address, value); |
| | | } |
| | | } |
| | | |
| | | logMsg += $"【{Name}】PLC写入后读取数据校验失败,地址:【{address}】,读取的数据:【{value}】,写入的数据:【{value}】{Environment.NewLine}"; |
| | | |
| | | throw new CommunicationException(string.Format(CommunicationExceptionMessage.ReadWriteDifferentException, typeof(T).Name, address, value, obj), CommunicationErrorType.WriteFailed); |
| | | } |
| | | } |
| | |
| | | default: |
| | | throw new CommunicationException(string.Format(CommunicationExceptionMessage.DataTypeErrorException, type.Name, address), CommunicationErrorType.TypeError); |
| | | } |
| | | |
| | | |
| | | } |
| | | catch (CommunicationException ex) |
| | | { |
| | |
| | | //读取异常时抛出自定义通讯异常类 |
| | | throw new CommunicationException($"写入数据异常,错误信息:{ex.Message}", CommunicationErrorType.TypeError, innerException: ex); |
| | | } |
| | | finally |
| | | { |
| | | |
| | | } |
| | | } |
| | | |
| | | private object Read(string address, TypeCode typeCode) |