hutongqing
2024-11-20 70233af5426b0d1c343ebe87183303a34a9aaa58
WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs
@@ -27,6 +27,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
@@ -38,7 +39,7 @@
    /// 西门子S7通讯类
    /// </summary>
    [Description("西门子S7")]
    public class SiemensS7 : BaseCommunicator, IDisposable
    public class SiemensS7 : BaseCommunicator
    {
        #region Private Member
        /// <summary>
@@ -67,6 +68,8 @@
        private string _name;
        private ILogNet _logNet;
        private bool _isPing = true;
        #endregion Private Member
        #region Public Member
@@ -89,10 +92,11 @@
        /// </summary>
        /// <param name="ipAddress">设备的IP地址</param>
        /// <param name="port">连接使用的端口号</param>
        /// <param name="name">设备名称</param>
        public SiemensS7(string ipAddress, int port, string name)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + $"Log_PLCReadWrite\\{name}";
            _logNet = new LogNetFileSize(path, 3 * 1024 * 1024, 100);
            _logNet = new LogNetFileSize(path, 10 * 1024 * 1024, 100);
            bool ipCheck = IPAddress.TryParse(ipAddress, out IPAddress? address);
            if (!ipCheck)
@@ -157,7 +161,7 @@
                    for (int i = 0; i < 5; i++)
                    {
                        T readValue = Read<T>(address);
                        stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAfterRead, readValue, value));
                        stringBuilder.AppendLine(string.Format(CommunicationInfoMessage.WriteAfterRead, address, value));
                        obj = readValue;
                        if (readValue.Equals(value))
                        {
@@ -182,7 +186,6 @@
            {
                LogNet.WriteInfo(Name, stringBuilder.ToString());
            }
        }
        /// <summary>
@@ -272,6 +275,29 @@
                throw new CommunicationException($"读取数据异常,错误信息:{ex.Message}", CommunicationErrorType.ReadException, innerException: ex);
            }
        }
        private void Ping()
        {
            Task.Run(() =>
            {
                while (_isPing)
                {
                    try
                    {
                        IPStatus status = plc.IpAddressPing();
                        if (status == IPStatus.Success)
                            _connected = true;
                        else
                            _connected = false;
                    }
                    finally
                    {
                        Thread.Sleep(100);
                    }
                }
            });
        }
        #endregion
        #region Public Method
@@ -297,7 +323,7 @@
                    LogNet.WriteInfo(Name, string.Format(CommunicationInfoMessage.ConnectSuccess, _ipAddress, _port));
                else
                    LogNet.WriteError(Name, string.Format(CommunicationExceptionMessage.ConnectFaild, _ipAddress, _port, operateResult.Message));
                Ping();
                return operateResult.IsSuccess;
            }
            catch (Exception ex)
@@ -399,7 +425,7 @@
            catch (Exception ex)
            {
                //写入异常时抛出自定义通讯异常类
                throw new CommunicationException($"写入数据异常,地址:【{address}】,错误信息: {ex.Message}", CommunicationErrorType.ReadFailed, innerException: ex);
                throw new CommunicationException($"写入数据异常,地址:【{address}】,错误信息: {ex.Message}", CommunicationErrorType.WriteFailed, innerException: ex);
            }
        }
@@ -564,7 +590,6 @@
                LogNet.WriteException(Name, $"【{Name}】PLC读取异常,地址:【{address}】,错误信息:【{ex.Message}】", ex);
                throw new CommunicationException(ex.Message, CommunicationErrorType.ReadException, innerException: ex);
            }
        }
        #endregion
@@ -620,14 +645,15 @@
            {
                LogNet.WriteInfo(Name, stringBuilder.ToString());
            }
        }
        #endregion
        // 显式实现IDisposable接口以提供垃圾回收时的清理  
        public void Dispose()
        public override void Dispose()
        {
            _isPing = false;
            Disconnect();
            plc.Dispose();
            GC.SuppressFinalize(this);
        }