Modbus_Tcp.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using FluentModbus;
  2. using ProtocalGeneral;
  3. using System.Net;
  4. using System.Runtime.InteropServices;
  5. namespace ModBusTcp;
  6. public class Modbus_Tcp : IModBus<byte[]?>
  7. {
  8. private ModbusTcpClient? _master;
  9. private IPEndPoint? EndPoint;
  10. private string? _name;
  11. private string _ip = string.Empty;
  12. private ushort _port;
  13. private ITcpConnectNority? _connect;
  14. public bool Initialize(string name, ITcpConnectNority tcpConnect)
  15. {
  16. if (!string.IsNullOrEmpty(this._name))
  17. return false;
  18. this._name = name;
  19. this._name ??= "Default";
  20. this._connect = tcpConnect;
  21. return true;
  22. }
  23. public bool Open(string ip, ushort port, bool notify = true)
  24. {
  25. if (this._master is not null)
  26. return false;
  27. if (!IPAddress.TryParse(ip, out IPAddress? address) || address is null)
  28. return false;
  29. try
  30. {
  31. IPEndPoint endPoint = new(address, port);
  32. ModbusTcpClient client = new()
  33. {
  34. ReadTimeout = 500,
  35. ConnectTimeout = 500
  36. };
  37. if (notify)
  38. client.Connect(endPoint);
  39. this._ip = ip;
  40. this._port = port;
  41. this.EndPoint = endPoint;
  42. this._master = client;
  43. }
  44. catch
  45. {
  46. return false;
  47. }
  48. return true;
  49. }
  50. public byte[]? GetBuffer(ushort index, ushort count, byte slaveAddress = 1)
  51. {
  52. if (this._master is null)
  53. return null;
  54. try
  55. {
  56. lock (this)
  57. {
  58. Span<byte> bytes = this._master.ReadHoldingRegisters(0x00, index, count);
  59. if (bytes == null)
  60. return null;
  61. return bytes.ToArray();
  62. }
  63. }
  64. catch
  65. {
  66. TryReconnect();
  67. return null;
  68. }
  69. }
  70. public unsafe bool SetFloat(ushort index, float value, byte slaveAddress = 1)
  71. {
  72. if (this._master is null)
  73. return false;
  74. byte[] bytes = new byte[4];
  75. float* pSource = &value;
  76. byte* pByte = (byte*)pSource;
  77. bytes[0] = pByte[3];
  78. bytes[1] = pByte[2];
  79. bytes[2] = pByte[1];
  80. bytes[3] = pByte[0];
  81. try
  82. {
  83. lock (this)
  84. this._master.WriteMultipleRegisters(0x00, (int)index, bytes);
  85. }
  86. catch
  87. {
  88. return false;
  89. }
  90. return true;
  91. }
  92. public unsafe bool SetValue<T>(ushort index, T value, byte slaveAddress = 1) where T : struct
  93. {
  94. if (this._master is null)
  95. return false;
  96. int size = Marshal.SizeOf<T>();
  97. if (size % 4 != 0)
  98. return false;
  99. byte[] bytes = new byte[size];
  100. #pragma warning disable CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type
  101. T* pSource = &value;
  102. #pragma warning restore CS8500
  103. byte* pByte = (byte*)pSource;
  104. for (int i = 0; i < size; i += 4)
  105. {
  106. bytes[i] = pByte[i + 3];
  107. bytes[i + 1] = pByte[i + 2];
  108. bytes[i + 2] = pByte[i + 1];
  109. bytes[i + 3] = pByte[i];
  110. }
  111. try
  112. {
  113. lock (this)
  114. this._master.WriteMultipleRegisters(0x00, (int)index, bytes);
  115. }
  116. catch
  117. {
  118. return false;
  119. }
  120. return true;
  121. }
  122. public unsafe bool SetUshort(ushort index, ushort value, byte slaveAddress = 1)
  123. {
  124. if (this._master is null)
  125. return false;
  126. ushort* pUshort = &value;
  127. byte* pByte = (byte*)pUshort;
  128. byte[] result = [pByte[1], pByte[0]];
  129. try
  130. {
  131. lock (this)
  132. this._master.WriteSingleRegister(0x00, index, result);
  133. }
  134. catch
  135. {
  136. return false;
  137. }
  138. return true;
  139. }
  140. public bool Close()
  141. {
  142. this._master?.Dispose();
  143. this._master = null;
  144. this.IsCanceled = true;
  145. return true;
  146. }
  147. private volatile bool IsCanceled = false;
  148. private readonly Semaphore semaphore = new(1, 1);
  149. private void TryReconnect()
  150. {
  151. if (!semaphore.WaitOne(0))
  152. return;
  153. this._master?.Disconnect();
  154. this._connect?.DisConnect(this._ip, this._port);
  155. Task.Factory.StartNew(() =>
  156. {
  157. IsCanceled = false;
  158. this._master ??= new()
  159. {
  160. ReadTimeout = 500,
  161. ConnectTimeout = 500
  162. };
  163. try
  164. {
  165. while (true)
  166. {
  167. if (this.IsCanceled)
  168. return;
  169. try
  170. {
  171. this._master.Connect(EndPoint!);
  172. break;
  173. }
  174. catch
  175. {
  176. Thread.Sleep(1000);
  177. }
  178. }
  179. this._connect?.Connect(this._ip, this._port);
  180. }
  181. finally
  182. {
  183. semaphore.Release();
  184. }
  185. });
  186. }
  187. }