PLC.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. // ReSharper disable once FunctionNeverReturns
  28. });
  29. }
  30. public IPLCAccessor Accessor { get; private set; }
  31. private bool ping(int timeout = 300)
  32. {
  33. var p = new Ping();
  34. if (Entity.IP == "1") return false;
  35. var res = p.Send(Entity.IP, timeout);
  36. return res.Status == IPStatus.Success;
  37. }
  38. }
  39. public interface IPLCAccessorCreater
  40. {
  41. IPLCAccessor Create(PLCInfo data);
  42. }
  43. public interface IPLCAccessor
  44. {
  45. void WriteBytes(ushort db, ushort address, byte[] data);
  46. byte[] ReadBytes(ushort db, ushort address, ushort length);
  47. }
  48. }