PLC.cs 1.1 KB

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