IModbusTcpMaster.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using System;
  4. namespace Team.Communicate.Modbus
  5. {
  6. public interface IModbusTcpMaster
  7. {
  8. public string Ip { get; }
  9. public int Port { get; }
  10. int ConnectTimeOut { get; set; }
  11. byte SlaveId { get; set; }
  12. bool IsConnect { get; }
  13. void Connect();
  14. Task ConnectAsync();
  15. /// <summary>
  16. /// 重连
  17. /// </summary>
  18. /// <param name="ip"></param>
  19. /// <param name="port"></param>
  20. /// <returns></returns>
  21. Task<bool> ReConnect();
  22. ushort[] ReadBytes(ushort start, ushort length);
  23. /// <summary>
  24. /// 获取单个位状态
  25. /// </summary>
  26. /// <param name="start">从PLC里读出第几个字节</param>
  27. /// <param name="index">第几位</param>
  28. /// <returns></returns>
  29. bool GetBool(ushort start, ushort index);
  30. List<KeyValuePair<string, bool>> GetBoolList(IEnumerable<string> addresses);
  31. /// <summary>
  32. /// 获取单个位状态
  33. /// </summary>
  34. /// <param name="start">从PLC里读出第几个字节</param>
  35. /// <param name="index" >第几位</param>
  36. /// <returns></returns>
  37. Task<bool> GetBoolAsync(ushort start, ushort index);
  38. /// <summary>
  39. /// 写modbus位
  40. /// </summary>
  41. /// <param name="start">字节位置</param>
  42. /// <param name="index">位索引</param>
  43. /// <param name="flag">位value</param>
  44. void WriteBool(ushort start, int index, bool flag);
  45. /// <summary>
  46. /// 异步写modbus位
  47. /// </summary>
  48. /// <param name="start">字节位置</param>
  49. /// <param name="index">位索引</param>
  50. /// <param name="flag">位value</param>
  51. Task WriteBoolAsync(ushort start, int index, bool flag);
  52. void WriteInt32(ushort start, int value);
  53. Task WriteInt32Async(ushort start, int value);
  54. /// <summary>
  55. /// 写modbus位
  56. /// </summary>
  57. /// <param name="start"></param>
  58. /// <param name="index"></param>
  59. /// <param name="value"></param>
  60. void WriteByte(ushort start, int index, byte value);
  61. /// <summary>
  62. /// 写modbus位
  63. /// </summary>
  64. /// <param name="start"></param>
  65. /// <param name="index"></param>
  66. /// <param name="value"></param>
  67. Task WriteByteAsync(ushort start, int index, byte value);
  68. /// <summary>
  69. /// 写入双精度浮点
  70. /// </summary>
  71. /// <param name="start"></param>
  72. /// <param name="value"></param>
  73. void WriteDouble(ushort start, double value);
  74. /// <summary>
  75. /// 异步写入双精度浮点
  76. /// </summary>
  77. /// <param name="start"></param>
  78. /// <param name="value"></param>
  79. /// <returns></returns>
  80. Task WriteDoubleAsync(ushort start, double value);
  81. /// <summary>
  82. /// 写入单精度浮点
  83. /// </summary>
  84. /// <param name="start"></param>
  85. /// <param name="value"></param>
  86. void WriteFloat(ushort start, float value);
  87. /// <summary>
  88. /// 异步写入单精度浮点
  89. /// </summary>
  90. /// <param name="start"></param>
  91. /// <param name="value"></param>
  92. /// <returns></returns>
  93. Task WriteFloatAsync(ushort start, float value);
  94. /// <summary>
  95. /// 获取浮点数值
  96. /// </summary>
  97. /// <param name="index">数据索引</param>
  98. /// <returns></returns>
  99. float GetFloat(ushort index);
  100. /// <summary>
  101. /// 获取浮点数值
  102. /// </summary>
  103. /// <param name="index">数据索引</param>
  104. /// <returns></returns>
  105. Task<float> GetFloatAsync(ushort index);
  106. /// <summary>
  107. /// 获取字
  108. /// </summary>
  109. /// <param name="index">数据索引</param>
  110. /// <returns></returns>
  111. ushort GetShort(ushort index);
  112. /// <summary>
  113. /// 获取字
  114. /// </summary>
  115. /// <param name="index">数据索引</param>
  116. /// <returns></returns>
  117. Task<ushort> GetShortAsync(ushort index);
  118. /// <summary>
  119. /// 获取整型数值
  120. /// </summary>
  121. /// 4字节整数
  122. /// <param name="index">数据索引</param>
  123. /// <returns></returns>
  124. int GetInteger(ushort index);
  125. /// <summary>
  126. /// 异步获取整型数值
  127. /// </summary>
  128. /// 4字节整数
  129. /// <param name="index">数据索引</param>
  130. /// <returns></returns>
  131. Task<int> GetIntegerAsync(ushort index);
  132. /// <summary>
  133. /// 读字节
  134. /// </summary>
  135. /// <param name="start">开始字索引</param>
  136. /// <param name="index">第几个字节</param>
  137. /// <returns></returns>
  138. Task<byte> GetByteAsync(ushort start, int index);
  139. /// <summary>
  140. /// 读字节
  141. /// </summary>
  142. /// <param name="start">开始字索引</param>
  143. /// <param name="index">第几个字节</param>
  144. /// <returns></returns>
  145. byte GetByte(ushort start, int index);
  146. ushort[] GetBytes(ushort start, ushort length);
  147. Task<ushort[]> GetBytesAsync(ushort start, ushort length);
  148. event EventHandler<bool> ConnectChanged;
  149. }
  150. }