hutongqing
2024-08-21 b5bc0d6eb2d2e55ea830a1b286252b9754cbf8e9
WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs
@@ -23,6 +23,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Reflection;
@@ -35,6 +36,7 @@
    /// <summary>
    /// 西门子S7通讯类
    /// </summary>
    [Description("西门子S7")]
    public class SiemensS7 : BaseCommunicator, IDisposable
    {
        #region Private Member
@@ -108,11 +110,20 @@
        /// <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>
@@ -125,6 +136,8 @@
        /// <returns></returns>
        /// <exception cref="CommunicationException"></exception>
        private bool GetResult<T>(OperateResult operateResult, string address, T value) where T : notnull
        {
            try
        {
            if (!operateResult.IsSuccess)
            {
@@ -148,6 +161,13 @@
                }
                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>
@@ -194,7 +214,7 @@
            catch (Exception ex)
            {
                //读取异常时抛出自定义通讯异常类
                throw new CommunicationException($"读取数据异常,错误信息:{ex.Message}", CommunicationErrorType.TypeError, innerException: ex);
                throw new CommunicationException($"写入数据异常,错误信息:{ex.Message}", CommunicationErrorType.TypeError, innerException: ex);
            }
        }
@@ -610,12 +630,23 @@
        #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)
@@ -649,6 +680,13 @@
            }
            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接口以提供垃圾回收时的清理