using System.Net.NetworkInformation; using System.Threading; namespace WCS.Core; public class PLC : EntityEx { public PLC(PLCInfo ent, World world) : base(ent) { if (Configs.PLCAccessorCreater != null) Accessor = Configs.PLCAccessorCreater.Create(ent); else throw new Exception("Configs.PLCAccessorCreater未赋值"); Task.Run(() => { while (true) { _Ping = Ping(); Task.Delay(1000).Wait(); } // ReSharper disable once FunctionNeverReturns }); } 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) { var p = new Ping(); if (Entity.IP == "1") return false; var res = p.Send(Entity.IP, timeout); return res.Status == IPStatus.Success; } } public interface IPLCAccessorCreater { IPLCAccessor Create(PLCInfo data); } public interface IPLCAccessor { void WriteBytes(ushort db, ushort address, byte[] data); byte[] ReadBytes(ushort db, ushort address, ushort length); }