IModbusMaster.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.Threading.Tasks;
  3. namespace NModbus
  4. {
  5. /// <summary>
  6. /// Modbus master device.
  7. /// </summary>
  8. public interface IModbusMaster : IDisposable
  9. {
  10. /// <summary>
  11. /// Transport used by this master.
  12. /// </summary>
  13. IModbusTransport Transport { get; }
  14. /// <summary>
  15. /// Reads from 1 to 2000 contiguous coils status.
  16. /// </summary>
  17. /// <param name="slaveAddress">Address of device to read values from.</param>
  18. /// <param name="startAddress">Address to begin reading.</param>
  19. /// <param name="numberOfPoints">Number of coils to read.</param>
  20. /// <returns>Coils status.</returns>
  21. bool[] ReadCoils(byte slaveAddress, ushort startAddress, ushort numberOfPoints);
  22. /// <summary>
  23. /// Asynchronously reads from 1 to 2000 contiguous coils status.
  24. /// </summary>
  25. /// <param name="slaveAddress">Address of device to read values from.</param>
  26. /// <param name="startAddress">Address to begin reading.</param>
  27. /// <param name="numberOfPoints">Number of coils to read.</param>
  28. /// <returns>A task that represents the asynchronous read operation.</returns>
  29. Task<bool[]> ReadCoilsAsync(byte slaveAddress, ushort startAddress, ushort numberOfPoints);
  30. /// <summary>
  31. /// Reads from 1 to 2000 contiguous discrete input status.
  32. /// </summary>
  33. /// <param name="slaveAddress">Address of device to read values from.</param>
  34. /// <param name="startAddress">Address to begin reading.</param>
  35. /// <param name="numberOfPoints">Number of discrete inputs to read.</param>
  36. /// <returns>Discrete inputs status.</returns>
  37. bool[] ReadInputs(byte slaveAddress, ushort startAddress, ushort numberOfPoints);
  38. /// <summary>
  39. /// Asynchronously reads from 1 to 2000 contiguous discrete input status.
  40. /// </summary>
  41. /// <param name="slaveAddress">Address of device to read values from.</param>
  42. /// <param name="startAddress">Address to begin reading.</param>
  43. /// <param name="numberOfPoints">Number of discrete inputs to read.</param>
  44. /// <returns>A task that represents the asynchronous read operation.</returns>
  45. Task<bool[]> ReadInputsAsync(byte slaveAddress, ushort startAddress, ushort numberOfPoints);
  46. /// <summary>
  47. /// Reads contiguous block of holding registers.
  48. /// </summary>
  49. /// <param name="slaveAddress">Address of device to read values from.</param>
  50. /// <param name="startAddress">Address to begin reading.</param>
  51. /// <param name="numberOfPoints">Number of holding registers to read.</param>
  52. /// <returns>Holding registers status.</returns>
  53. ushort[] ReadHoldingRegisters(byte slaveAddress, ushort startAddress, ushort numberOfPoints);
  54. /// <summary>
  55. /// Asynchronously reads contiguous block of holding registers.
  56. /// </summary>
  57. /// <param name="slaveAddress">Address of device to read values from.</param>
  58. /// <param name="startAddress">Address to begin reading.</param>
  59. /// <param name="numberOfPoints">Number of holding registers to read.</param>
  60. /// <returns>A task that represents the asynchronous read operation.</returns>
  61. Task<ushort[]> ReadHoldingRegistersAsync(byte slaveAddress, ushort startAddress, ushort numberOfPoints);
  62. /// <summary>
  63. /// Reads contiguous block of input registers.
  64. /// </summary>
  65. /// <param name="slaveAddress">Address of device to read values from.</param>
  66. /// <param name="startAddress">Address to begin reading.</param>
  67. /// <param name="numberOfPoints">Number of holding registers to read.</param>
  68. /// <returns>Input registers status.</returns>
  69. ushort[] ReadInputRegisters(byte slaveAddress, ushort startAddress, ushort numberOfPoints);
  70. /// <summary>
  71. /// Asynchronously reads contiguous block of input registers.
  72. /// </summary>
  73. /// <param name="slaveAddress">Address of device to read values from.</param>
  74. /// <param name="startAddress">Address to begin reading.</param>
  75. /// <param name="numberOfPoints">Number of holding registers to read.</param>
  76. /// <returns>A task that represents the asynchronous read operation.</returns>
  77. Task<ushort[]> ReadInputRegistersAsync(byte slaveAddress, ushort startAddress, ushort numberOfPoints);
  78. /// <summary>
  79. /// Writes a single coil value.
  80. /// </summary>
  81. /// <param name="slaveAddress">Address of the device to write to.</param>
  82. /// <param name="coilAddress">Address to write value to.</param>
  83. /// <param name="value">Value to write.</param>
  84. void WriteSingleCoil(byte slaveAddress, ushort coilAddress, bool value);
  85. /// <summary>
  86. /// Asynchronously writes a single coil value.
  87. /// </summary>
  88. /// <param name="slaveAddress">Address of the device to write to.</param>
  89. /// <param name="coilAddress">Address to write value to.</param>
  90. /// <param name="value">Value to write.</param>
  91. /// <returns>A task that represents the asynchronous write operation.</returns>
  92. Task WriteSingleCoilAsync(byte slaveAddress, ushort coilAddress, bool value);
  93. /// <summary>
  94. /// Writes a single holding register.
  95. /// </summary>
  96. /// <param name="slaveAddress">Address of the device to write to.</param>
  97. /// <param name="registerAddress">Address to write.</param>
  98. /// <param name="value">Value to write.</param>
  99. void WriteSingleRegister(byte slaveAddress, ushort registerAddress, ushort value);
  100. /// <summary>
  101. /// Asynchronously writes a single holding register.
  102. /// </summary>
  103. /// <param name="slaveAddress">Address of the device to write to.</param>
  104. /// <param name="registerAddress">Address to write.</param>
  105. /// <param name="value">Value to write.</param>
  106. /// <returns>A task that represents the asynchronous write operation.</returns>
  107. Task WriteSingleRegisterAsync(byte slaveAddress, ushort registerAddress, ushort value);
  108. /// <summary>
  109. /// Writes a block of 1 to 123 contiguous registers.
  110. /// </summary>
  111. /// <param name="slaveAddress">Address of the device to write to.</param>
  112. /// <param name="startAddress">Address to begin writing values.</param>
  113. /// <param name="data">Values to write.</param>
  114. void WriteMultipleRegisters(byte slaveAddress, ushort startAddress, ushort[] data);
  115. /// <summary>
  116. /// Asynchronously writes a block of 1 to 123 contiguous registers.
  117. /// </summary>
  118. /// <param name="slaveAddress">Address of the device to write to.</param>
  119. /// <param name="startAddress">Address to begin writing values.</param>
  120. /// <param name="data">Values to write.</param>
  121. /// <returns>A task that represents the asynchronous write operation.</returns>
  122. Task WriteMultipleRegistersAsync(byte slaveAddress, ushort startAddress, ushort[] data);
  123. /// <summary>
  124. /// Writes a sequence of coils.
  125. /// </summary>
  126. /// <param name="slaveAddress">Address of the device to write to.</param>
  127. /// <param name="startAddress">Address to begin writing values.</param>
  128. /// <param name="data">Values to write.</param>
  129. void WriteMultipleCoils(byte slaveAddress, ushort startAddress, bool[] data);
  130. /// <summary>
  131. /// Asynchronously writes a sequence of coils.
  132. /// </summary>
  133. /// <param name="slaveAddress">Address of the device to write to.</param>
  134. /// <param name="startAddress">Address to begin writing values.</param>
  135. /// <param name="data">Values to write.</param>
  136. /// <returns>A task that represents the asynchronous write operation.</returns>
  137. Task WriteMultipleCoilsAsync(byte slaveAddress, ushort startAddress, bool[] data);
  138. /// <summary>
  139. /// Performs a combination of one read operation and one write operation in a single Modbus transaction.
  140. /// The write operation is performed before the read.
  141. /// </summary>
  142. /// <param name="slaveAddress">Address of device to read values from.</param>
  143. /// <param name="startReadAddress">Address to begin reading (Holding registers are addressed starting at 0).</param>
  144. /// <param name="numberOfPointsToRead">Number of registers to read.</param>
  145. /// <param name="startWriteAddress">Address to begin writing (Holding registers are addressed starting at 0).</param>
  146. /// <param name="writeData">Register values to write.</param>
  147. ushort[] ReadWriteMultipleRegisters(
  148. byte slaveAddress,
  149. ushort startReadAddress,
  150. ushort numberOfPointsToRead,
  151. ushort startWriteAddress,
  152. ushort[] writeData);
  153. /// <summary>
  154. /// Asynchronously performs a combination of one read operation and one write operation in a single Modbus transaction.
  155. /// The write operation is performed before the read.
  156. /// </summary>
  157. /// <param name="slaveAddress">Address of device to read values from.</param>
  158. /// <param name="startReadAddress">Address to begin reading (Holding registers are addressed starting at 0).</param>
  159. /// <param name="numberOfPointsToRead">Number of registers to read.</param>
  160. /// <param name="startWriteAddress">Address to begin writing (Holding registers are addressed starting at 0).</param>
  161. /// <param name="writeData">Register values to write.</param>
  162. /// <returns>A task that represents the asynchronous operation</returns>
  163. Task<ushort[]> ReadWriteMultipleRegistersAsync(
  164. byte slaveAddress,
  165. ushort startReadAddress,
  166. ushort numberOfPointsToRead,
  167. ushort startWriteAddress,
  168. ushort[] writeData);
  169. /// <summary>
  170. /// Write a file record to the device.
  171. /// </summary>
  172. /// <param name="slaveAdress">Address of device to write values to</param>
  173. /// <param name="fileNumber">The Extended Memory file number</param>
  174. /// <param name="startingAddress">The starting register address within the file</param>
  175. /// <param name="data">The data to be written</param>
  176. void WriteFileRecord(byte slaveAdress, ushort fileNumber, ushort startingAddress, byte[] data);
  177. /// <summary>
  178. /// Executes the custom message.
  179. /// </summary>
  180. /// <typeparam name="TResponse">The type of the response.</typeparam>
  181. /// <param name="request">The request.</param>
  182. TResponse ExecuteCustomMessage<TResponse>(IModbusMessage request)
  183. where TResponse : IModbusMessage, new();
  184. }
  185. }