PLC.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. }
  31. }
  32. public interface IPLCAccessorCreater
  33. {
  34. IPLCAccessor Create(PLCInfo data);
  35. }
  36. public interface IPLCAccessor
  37. {
  38. void WriteBytes(ushort db, ushort address, byte[] data);
  39. byte[] ReadBytes(ushort db, ushort address, ushort length);
  40. }
  41. }