| | |
| | | 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) |
| | | { |
| | | try |
| | | { |
| | | if (!operateResult.IsSuccess) |
| | | { |
| | | throw new CommunicationException(string.Format(CommunicationExceptionMessage.ReadFailedException, typeof(T).Name, address, operateResult.Message), CommunicationErrorType.ReadFailed); |
| | | } |
| | | 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> |
| | |
| | | /// <returns></returns> |
| | | /// <exception cref="CommunicationException"></exception> |
| | | private bool GetResult<T>(OperateResult operateResult, string address, T value) where T : notnull |
| | | { |
| | | try |
| | | { |
| | | if (!operateResult.IsSuccess) |
| | | { |
| | |
| | | } |
| | | 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); |
| | | } |
| | | |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | catch (Exception ex) |
| | | { |
| | | //读取异常时抛出自定义通讯异常类 |
| | | throw new CommunicationException($"读取数据异常,错误信息:{ex.Message}", CommunicationErrorType.TypeError, innerException: ex); |
| | | throw new CommunicationException($"写入数据异常,错误信息:{ex.Message}", CommunicationErrorType.TypeError, innerException: ex); |
| | | } |
| | | } |
| | | |
| | |
| | | #region ReadCustomer |
| | | public override T ReadCustomer<T>(string address) |
| | | { |
| | | 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) |
| | | { |
| | | try |
| | | { |
| | | OperateResult operateResult = plc.WriteCustomer(address, value); |
| | | if (operateResult.IsSuccess) |
| | |
| | | } |
| | | return operateResult.IsSuccess; |
| | | } |
| | | catch(Exception ex) |
| | | { |
| | | LogNet.WriteException(Name, $"【{Name}】PLC写入异常,地址:【{address}】,写入的数据:【{JsonConvert.SerializeObject(value)}】,错误信息:【{ex.Message}】", ex); |
| | | throw new CommunicationException(ex.Message, CommunicationErrorType.WriteFailed, innerException: ex); |
| | | } |
| | | |
| | | } |
| | | #endregion |
| | | |
| | | // 显式实现IDisposable接口以提供垃圾回收时的清理 |