PLC.cs 1.3 KB

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