Procházet zdrojové kódy

修复 Ping 调用问题并重构 PLC 类

修复了 `DataBlock` 类中 `plc.Ping` 调用时缺少括号的问题。
在 `PLC.cs` 文件中添加了 `System.Threading` 的引用。
重命名 `Ping` 属性为 `_Ping` 并设为私有,添加新的 `Ping` 方法用于实际的 Ping 操作。
将原来的 `ping` 方法重命名为 `Ping` 并保持其私有性。
在 `Task.Run` 的循环中,将 `Ping` 属性的调用改为 `Ping()` 方法。
林豪 左 před 11 měsíci
rodič
revize
35e01d0fec
2 změnil soubory, kde provedl 14 přidání a 4 odebrání
  1. 1 1
      WCS.Core/DataBlock.cs
  2. 13 3
      WCS.Core/PLC.cs

+ 1 - 1
WCS.Core/DataBlock.cs

@@ -39,7 +39,7 @@ public class DataBlock : EntityEx<DBInfo>
             var plc = Entity.PLCInfo.Ex(World);
             if (Entity.PLCInfo.Type != PLCType.VitrualRedisPLC)
                 if (Failed)
-                    if (!plc.Ping)
+                    if (!plc.Ping())
                     {
                         if (Entity.PLCInfo.IP == "1") throw new Exception($"{Entity.PLCInfo.IP}无法访问");
                         Console.ForegroundColor = ConsoleColor.Red;

+ 13 - 3
WCS.Core/PLC.cs

@@ -1,4 +1,5 @@
 using System.Net.NetworkInformation;
+using System.Threading;
 
 namespace WCS.Core;
 
@@ -15,18 +16,27 @@ public class PLC : EntityEx<PLCInfo>
         {
             while (true)
             {
-                Ping = ping();
+                _Ping = Ping();
                 Task.Delay(1000).Wait();
             }
             // ReSharper disable once FunctionNeverReturns
         });
     }
 
-    public bool Ping { get; private set; }
+    public bool _Ping { get; private set; }
+
+    public bool Ping()
+    {
+        if (_Ping) return _Ping;
+        var p = new Ping();
+        if (Entity.IP == "1") return false;
+        var res = p.Send(Entity.IP, 300);
+        return res.Status == IPStatus.Success;
+    }
 
     public IPLCAccessor Accessor { get; private set; }
 
-    private bool ping(int timeout = 300)
+    private bool Ping(int timeout = 300)
     {
         var p = new Ping();
         if (Entity.IP == "1") return false;