| | |
| | | using System; |
| | | using System.Collections; |
| | | using System.Collections.Generic; |
| | | using System.ComponentModel; |
| | | using System.Linq; |
| | | using System.Net; |
| | | using System.Reflection; |
| | |
| | | /// <summary> |
| | | /// 西门子S7通讯类 |
| | | /// </summary> |
| | | [Description("西门子S7")] |
| | | public class SiemensS7 : BaseCommunicator, IDisposable |
| | | { |
| | | #region Private Member |
| | |
| | | /// <exception cref="CommunicationException">自定义通讯异常类</exception> |
| | | 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; |
| | | } |
| | | return operateResult.Content; |
| | | catch (Exception ex) |
| | | { |
| | | LogNet.WriteException(Name, $"【{Name}】PLC读取异常,地址:【{address}】,错误信息:【{ex.Message}】", ex); |
| | | throw new CommunicationException(ex.Message, CommunicationErrorType.ReadException, innerException: ex); |
| | | } |
| | | |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// <exception cref="CommunicationException"></exception> |
| | | private bool GetResult<T>(OperateResult operateResult, string address, T value) where T : notnull |
| | | { |
| | | if (!operateResult.IsSuccess) |
| | | string logMsg = ""; |
| | | try |
| | | { |
| | | throw new CommunicationException(string.Format(CommunicationExceptionMessage.WriteFailedException, typeof(T).Name, address, value, operateResult.Message), CommunicationErrorType.WriteFailed); |
| | | } |
| | | else |
| | | { |
| | | object? obj = null; |
| | | for (int i = 0; i < 5; i++) |
| | | logMsg += $"【{Name}】PLC写入数据,地址:【{address}】,写入的数据:【{value}】{Environment.NewLine}"; |
| | | 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); |
| | | logMsg += $"【{Name}】PLC写入后读取数据,地址:【{address}】,读取的数据:【{readValue}】{Environment.NewLine}"; |
| | | obj = readValue; |
| | | if (readValue.Equals(value)) |
| | | { |
| | | logMsg += $"【{Name}】PLC写入后读取数据校验成功,地址:【{address}】,读取的数据:【{readValue}】,写入的数据:【{value}】{Environment.NewLine}"; |
| | | return true; |
| | | } |
| | | 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); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogNet.WriteException(Name, $"【{Name}】PLC写入异常,地址:【{address}】,写入的数据:【{value}】,错误信息:【{ex.Message}】", ex); |
| | | throw new CommunicationException(ex.Message, CommunicationErrorType.WriteFailed, innerException: ex); |
| | | } |
| | | finally |
| | | { |
| | | LogNet.WriteInfo(Name, logMsg); |
| | | } |
| | | |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | default: |
| | | throw new CommunicationException(string.Format(CommunicationExceptionMessage.DataTypeErrorException, type.Name, address), CommunicationErrorType.TypeError); |
| | | } |
| | | |
| | | |
| | | } |
| | | catch (CommunicationException ex) |
| | | { |
| | |
| | | catch (Exception ex) |
| | | { |
| | | //读取异常时抛出自定义通讯异常类 |
| | | throw new CommunicationException($"读取数据异常,错误信息:{ex.Message}", CommunicationErrorType.TypeError, innerException: ex); |
| | | throw new CommunicationException($"写入数据异常,错误信息:{ex.Message}", CommunicationErrorType.TypeError, innerException: ex); |
| | | } |
| | | finally |
| | | { |
| | | |
| | | } |
| | | } |
| | | |
| | |
| | | #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) |
| | | { |
| | | OperateResult operateResult = plc.WriteCustomer(address, value); |
| | | if (operateResult.IsSuccess) |
| | | string logMsg = string.Empty; |
| | | try |
| | | { |
| | | for (int i = 0; i < 5; i++) |
| | | OperateResult operateResult = plc.WriteCustomer(address, value); |
| | | logMsg += $"【{Name}】PLC写入数据,地址:【{address}】,写入的数据:【{JsonConvert.SerializeObject(value)}】{Environment.NewLine}"; |
| | | if (operateResult.IsSuccess) |
| | | { |
| | | T readValue = ReadCustomer<T>(address); |
| | | PropertyInfo[] propertyInfos = typeof(T).GetProperties(); |
| | | foreach (var item in propertyInfos) |
| | | for (int i = 0; i < 5; i++) |
| | | { |
| | | object writeValueItem = item.GetValue(value); |
| | | if (writeValueItem != null) |
| | | T readValue = ReadCustomer<T>(address); |
| | | logMsg += $"【{Name}】PLC写入后读取数据,地址:【{address}】,读取的数据:【{JsonConvert.SerializeObject(readValue)}】{Environment.NewLine}"; |
| | | PropertyInfo[] propertyInfos = typeof(T).GetProperties(); |
| | | foreach (var item in propertyInfos) |
| | | { |
| | | object readValueItem = item.GetValue(readValue); |
| | | if (writeValueItem.Equals(readValueItem)) |
| | | object writeValueItem = item.GetValue(value); |
| | | if (writeValueItem != null) |
| | | { |
| | | break; |
| | | 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); |
| | | } |
| | | else |
| | | { |
| | | plc.WriteCustomer(address, value); |
| | | } |
| | | throw new CommunicationException(string.Format(CommunicationExceptionMessage.ReadWriteDifferentException, typeof(T).Name, address, JsonConvert.SerializeObject(value), JsonConvert.SerializeObject(readValue)), CommunicationErrorType.WriteFailed); |
| | | } |
| | | } |
| | | } |
| | | else |
| | | { |
| | | throw new CommunicationException(string.Format(CommunicationExceptionMessage.WriteFailedException, typeof(T).Name, address, JsonConvert.SerializeObject(value), operateResult.Message), CommunicationErrorType.WriteFailed); |
| | | } |
| | | return operateResult.IsSuccess; |
| | | } |
| | | 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, $"【{Name}】PLC写入异常,地址:【{address}】,写入的数据:【{JsonConvert.SerializeObject(value)}】,错误信息:【{ex.Message}】", ex); |
| | | throw new CommunicationException(ex.Message, CommunicationErrorType.WriteFailed, innerException: ex); |
| | | } |
| | | return operateResult.IsSuccess; |
| | | finally |
| | | { |
| | | LogNet.WriteInfo(Name, logMsg); |
| | | } |
| | | |
| | | } |
| | | #endregion |
| | | |