From 2cc4dfef234b47bef364edf798b5051a25f33217 Mon Sep 17 00:00:00 2001 From: hutongqing <hutongqing@hnkhzn.com> Date: 星期五, 30 八月 2024 10:58:56 +0800 Subject: [PATCH] 1 --- WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs | 35 ++++++++++++++++++++++++++++++++--- 1 files changed, 32 insertions(+), 3 deletions(-) diff --git a/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs b/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs index e504bdc..3c1846f 100644 --- a/WIDESEAWCS_Server/WIDESEAWCS_Communicator/Siemens/SiemensS7Communicator.cs +++ b/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 @@ /// 瑗块棬瀛怱7閫氳绫� /// </summary> [Description("瑗块棬瀛怱7")] - 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 @@ -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 @@ -284,6 +310,7 @@ { try { + //瀹炰緥鍖栦竴涓タ闂ㄥ瓙鐨凷7鍗忚鐨勯�氳瀵硅薄 plc = new SiemensS7Net(SiemensPLCS.S1500) { @@ -297,7 +324,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) @@ -625,9 +652,11 @@ #endregion // 鏄惧紡瀹炵幇IDisposable鎺ュ彛浠ユ彁渚涘瀮鍦惧洖鏀舵椂鐨勬竻鐞� - public void Dispose() + public override void Dispose() { + _isPing = false; Disconnect(); + plc.Dispose(); GC.SuppressFinalize(this); } -- Gitblit v1.9.3