PLC.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Net.NetworkInformation;
  2. using System.Threading;
  3. namespace WCS.Core;
  4. public class PLC : EntityEx<PLCInfo>
  5. {
  6. public PLC(PLCInfo ent, World world) : base(ent)
  7. {
  8. if (Configs.PLCAccessorCreater != null)
  9. Accessor = Configs.PLCAccessorCreater.Create(ent);
  10. else
  11. throw new Exception("Configs.PLCAccessorCreater未赋值");
  12. Task.Run(() =>
  13. {
  14. while (true)
  15. {
  16. _Ping = Ping();
  17. Task.Delay(1000).Wait();
  18. }
  19. // ReSharper disable once FunctionNeverReturns
  20. });
  21. }
  22. public bool _Ping { get; private set; }
  23. public bool Ping()
  24. {
  25. if (_Ping) return _Ping;
  26. var p = new Ping();
  27. if (Entity.IP == "1") return false;
  28. var res = p.Send(Entity.IP, 300);
  29. return res.Status == IPStatus.Success;
  30. }
  31. public IPLCAccessor Accessor { get; private set; }
  32. private bool Ping(int timeout = 300)
  33. {
  34. var p = new Ping();
  35. if (Entity.IP == "1") return false;
  36. var res = p.Send(Entity.IP, timeout);
  37. return res.Status == IPStatus.Success;
  38. }
  39. }
  40. public interface IPLCAccessorCreater
  41. {
  42. IPLCAccessor Create(PLCInfo data);
  43. }
  44. public interface IPLCAccessor
  45. {
  46. void WriteBytes(ushort db, ushort address, byte[] data);
  47. byte[] ReadBytes(ushort db, ushort address, ushort length);
  48. }