PLC.cs 1.2 KB

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