S7.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using IPLCConnect;
  2. using S7.Net;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Runtime.CompilerServices;
  5. namespace S7Connect
  6. {
  7. [PLCProtocol( PLCProtocol.S7)]
  8. public sealed class S7 : IPLCConnect.IPLCConnect
  9. {
  10. public PLCProtocol Protocol => PLCProtocol.S7;
  11. [AllowNull]
  12. private Plc plc;
  13. private bool isConnected = false;
  14. private bool isfirst = true;
  15. public CpuType CpuType { get; set; } = CpuType.S71200;
  16. public Int16 Rack { get; set; } = 0;
  17. public Int16 Slot { get; set; } = 1;
  18. public bool IsConnected
  19. {
  20. get => isConnected;
  21. set
  22. {
  23. if (isConnected != value || isfirst)
  24. {
  25. isConnected = value;
  26. StatusChanged?.Invoke(this, value);
  27. }
  28. isfirst = false;
  29. }
  30. }
  31. public string IP { get; private set; } = "127.0.0.1";
  32. public int Port { get; private set; } = 502;
  33. public event EventHandler<bool> StatusChanged;
  34. public void Connect()
  35. {
  36. if (plc == null) return;
  37. try
  38. {
  39. plc.Open();
  40. IsConnected = plc.IsConnected;
  41. }
  42. catch
  43. {
  44. IsConnected = false;
  45. }
  46. }
  47. public void DisConnect()
  48. {
  49. if (plc == null) return;
  50. try
  51. {
  52. plc.Close();
  53. }
  54. catch
  55. {
  56. }
  57. IsConnected = false;
  58. }
  59. public void Dispose()
  60. {
  61. try
  62. {
  63. plc?.Close();
  64. }
  65. catch
  66. {
  67. }
  68. plc = null;
  69. }
  70. public void Init(string ip, int port)
  71. {
  72. try
  73. {
  74. IP = ip;
  75. plc = new Plc(this.CpuType, IP, Rack, Slot);
  76. }
  77. catch
  78. {
  79. }
  80. IsConnected = false;
  81. }
  82. public T Read<T>(string addr) where T : unmanaged
  83. {
  84. try
  85. {
  86. if (plc == null || !plc.IsConnected)
  87. {
  88. IsConnected = false;
  89. return default;
  90. }
  91. if (string.IsNullOrEmpty(addr)) return default;
  92. object? value = plc.Read(addr);
  93. if (value == null) return default;
  94. if (typeof(T) == value.GetType()) return (T)value;
  95. switch (value)
  96. {
  97. case byte b:
  98. return Unsafe.As<byte, T>(ref b);
  99. case ushort us:
  100. return Unsafe.As<ushort, T>(ref us);
  101. case uint ui:
  102. return Unsafe.As<uint, T>(ref ui);
  103. }
  104. return (T)plc.Read(addr)!;
  105. }
  106. catch
  107. {
  108. DisConnect();
  109. }
  110. return default;
  111. }
  112. public void Write<T>(string addr, T value) where T : unmanaged
  113. {
  114. if(plc ==null || !plc.IsConnected)
  115. {
  116. IsConnected = false;
  117. return;
  118. }
  119. if (string.IsNullOrEmpty(addr)) return;
  120. try
  121. {
  122. plc.Write(addr, value);
  123. }
  124. catch
  125. {
  126. DisConnect();
  127. }
  128. }
  129. public bool ReadBit(string addr, byte bitindex)
  130. {
  131. if (string.IsNullOrEmpty(addr)) return false;
  132. return Read<bool>(addr);
  133. }
  134. public void Writebit(string addr, byte bitindex, bool value)
  135. {
  136. if (string.IsNullOrEmpty(addr)) return;
  137. Write(addr, value);
  138. }
  139. }
  140. }