ModbusMasterEnhanced.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. namespace NModbus.Extensions
  2. {
  3. using System;
  4. /// <summary>
  5. /// Utility Class to support Modbus 32/64bit devices.
  6. /// </summary>
  7. public class ModbusMasterEnhanced
  8. {
  9. private readonly IModbusMaster master;
  10. private readonly uint wordSize;
  11. private readonly Func<byte[], byte[]> endian;
  12. private readonly bool wordSwapped;
  13. /// <summary>
  14. /// Constructor with values to be used by all methods.
  15. /// Default is 32bit, LittleEndian, with wordswapping.
  16. /// </summary>
  17. /// <param name="master">The Modbus master</param>
  18. /// <param name="wordSize">Wordsize used by device. 16/32/64 are valid.</param>
  19. /// <param name="endian">The endian encoding of the device.</param>
  20. /// <param name="wordSwapped">Should the ushort words mirrored then flattened to bytes.</param>
  21. public ModbusMasterEnhanced(IModbusMaster master, uint wordSize=32, Func<byte[], byte[]> endian = null, bool wordSwapped = false)
  22. {
  23. this.master = master;
  24. this.wordSize = wordSize;
  25. this.endian = endian ?? Functions.Endian.LittleEndian;
  26. this.wordSwapped = wordSwapped;
  27. }
  28. /// <summary>
  29. /// Reads registers and converts the result into a char array.
  30. /// </summary>
  31. /// <param name="slaveAddress">Address of device to read values from.</param>
  32. /// <param name="startAddress">Address to begin reading.</param>
  33. /// <param name="numberOfPoints">Number of chars to read.</param>
  34. /// <returns></returns>
  35. public char[] ReadCharHoldingRegisters(byte slaveAddress, ushort startAddress, ushort numberOfPoints)
  36. => Functions.RegisterFunctions.ByteValueArraysToChars(
  37. Functions.RegisterFunctions.ReadRegisters(slaveAddress, startAddress, numberOfPoints, this.master, this.wordSize, this.endian, this.wordSwapped));
  38. /// <summary>
  39. /// Reads registers and converts the result into a ushort array.
  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 ushorts to read.</param>
  44. /// <returns></returns>
  45. public ushort[] ReadUshortHoldingRegisters(byte slaveAddress, ushort startAddress, ushort numberOfPoints)
  46. => Functions.RegisterFunctions.ByteValueArraysToUShorts(
  47. Functions.RegisterFunctions.ReadRegisters(slaveAddress, startAddress, numberOfPoints, this.master, this.wordSize, this.endian, this.wordSwapped));
  48. /// <summary>
  49. /// Reads registers and converts the result into a short array.
  50. /// </summary>
  51. /// <param name="slaveAddress">Address of device to read values from.</param>
  52. /// <param name="startAddress">Address to begin reading.</param>
  53. /// <param name="numberOfPoints">Number of shots to read.</param>
  54. /// <returns></returns>
  55. public short[] ReadShortHoldingRegisters(byte slaveAddress, ushort startAddress, ushort numberOfPoints)
  56. => Functions.RegisterFunctions.ByteValueArraysToShorts(
  57. Functions.RegisterFunctions.ReadRegisters(slaveAddress, startAddress, numberOfPoints, this.master, this.wordSize, this.endian, this.wordSwapped));
  58. /// <summary>
  59. /// Reads registers and converts the result into a uint array.
  60. /// </summary>
  61. /// <param name="slaveAddress">Address of device to read values from.</param>
  62. /// <param name="startAddress">Address to begin reading.</param>
  63. /// <param name="numberOfPoints">Number of uints to read.</param>
  64. /// <returns></returns>
  65. public uint[] ReadUintHoldingRegisters(byte slaveAddress, ushort startAddress, ushort numberOfPoints)
  66. => Functions.RegisterFunctions.ByteValueArraysToUInts(
  67. Functions.RegisterFunctions.ReadRegisters(slaveAddress, startAddress, numberOfPoints, this.master, this.wordSize, this.endian, this.wordSwapped));
  68. /// <summary>
  69. /// Reads registers and converts the result into a int array.
  70. /// </summary>
  71. /// <param name="slaveAddress">Address of device to read values from.</param>
  72. /// <param name="startAddress">Address to begin reading.</param>
  73. /// <param name="numberOfPoints">Number of ints to read.</param>
  74. /// <returns></returns>
  75. public int[] ReadIntHoldingRegisters(byte slaveAddress, ushort startAddress, ushort numberOfPoints)
  76. => Functions.RegisterFunctions.ByteValueArraysToInts(
  77. Functions.RegisterFunctions.ReadRegisters(slaveAddress, startAddress, numberOfPoints, this.master, this.wordSize, this.endian, this.wordSwapped));
  78. /// <summary>
  79. /// Reads registers and converts the result into a float array.
  80. /// </summary>
  81. /// <param name="slaveAddress">Address of device to read values from.</param>
  82. /// <param name="startAddress">Address to begin reading.</param>
  83. /// <param name="numberOfPoints">Number of floats to read.</param>
  84. /// <returns></returns>
  85. public float[] ReadFloatHoldingRegisters(byte slaveAddress, ushort startAddress, ushort numberOfPoints)
  86. => Functions.RegisterFunctions.ByteValueArraysToFloats(
  87. Functions.RegisterFunctions.ReadRegisters(slaveAddress, startAddress, numberOfPoints, this.master, this.wordSize, this.endian, this.wordSwapped));
  88. /// <summary>
  89. /// Write a char array to registers.
  90. /// </summary>
  91. /// <param name="slaveAddress">Address of the device to write to.</param>
  92. /// <param name="startAddress">Address to start writting values at.</param>
  93. /// <param name="data">Chars to write to device.</param>
  94. public void WriteCharHoldingRegisters(byte slaveAddress, ushort startAddress, char[] data)
  95. => Functions.RegisterFunctions.WriteRegistersFunc(
  96. slaveAddress,
  97. startAddress,
  98. Functions.RegisterFunctions.CharsToByteValueArrays(data, this.wordSize),
  99. this.master,
  100. this.wordSize,
  101. this.endian, this.wordSwapped);
  102. /// <summary>
  103. /// Write a ushort array to registers.
  104. /// </summary>
  105. /// <param name="slaveAddress">Address of the device to write to.</param>
  106. /// <param name="startAddress">Address to start writting values at.</param>
  107. /// <param name="data">Ushorts to write to device.</param>
  108. public void WriteUshortHoldingRegisters(byte slaveAddress, ushort startAddress, ushort[] data)
  109. => Functions.RegisterFunctions.WriteRegistersFunc(
  110. slaveAddress,
  111. startAddress,
  112. Functions.RegisterFunctions.UShortsToByteValueArrays(data, this.wordSize),
  113. this.master,
  114. this.wordSize,
  115. this.endian, this.wordSwapped);
  116. /// <summary>
  117. /// Write a short array to registers.
  118. /// </summary>
  119. /// <param name="slaveAddress">Address of the device to write to.</param>
  120. /// <param name="startAddress">Address to start writting values at.</param>
  121. /// <param name="data">Shorts to write to device.</param>
  122. public void WriteShortHoldingRegisters(byte slaveAddress, ushort startAddress, short[] data)
  123. => Functions.RegisterFunctions.WriteRegistersFunc(
  124. slaveAddress,
  125. startAddress,
  126. Functions.RegisterFunctions.ShortsToByteValueArrays(data, this.wordSize),
  127. this.master,
  128. this.wordSize,
  129. this.endian, this.wordSwapped);
  130. /// <summary>
  131. /// Write a int array to registers.
  132. /// </summary>
  133. /// <param name="slaveAddress">Address of the device to write to.</param>
  134. /// <param name="startAddress">Address to start writting values at.</param>
  135. /// <param name="data">Ints to write to device.</param>
  136. public void WriteIntHoldingRegisters(byte slaveAddress, ushort startAddress, int[] data)
  137. => Functions.RegisterFunctions.WriteRegistersFunc(
  138. slaveAddress,
  139. startAddress,
  140. Functions.RegisterFunctions.IntToByteValueArrays(data, this.wordSize),
  141. this.master,
  142. this.wordSize,
  143. this.endian, this.wordSwapped);
  144. /// <summary>
  145. /// Write a uint array to registers.
  146. /// </summary>
  147. /// <param name="slaveAddress">Address of the device to write to.</param>
  148. /// <param name="startAddress">Address to start writting values at.</param>
  149. /// <param name="data">Uints to write to device.</param>
  150. public void WriteUIntHoldingRegisters(byte slaveAddress, ushort startAddress, uint[] data)
  151. => Functions.RegisterFunctions.WriteRegistersFunc(
  152. slaveAddress,
  153. startAddress,
  154. Functions.RegisterFunctions.UIntToByteValueArrays(data, this.wordSize),
  155. this.master,
  156. this.wordSize,
  157. this.endian, this.wordSwapped);
  158. /// <summary>
  159. /// Write a float array to registers.
  160. /// </summary>
  161. /// <param name="slaveAddress">Address of the device to write to.</param>
  162. /// <param name="startAddress">Address to start writting values at.</param>
  163. /// <param name="data">Floats to write to device.</param>
  164. public void WriteFloatHoldingRegisters(byte slaveAddress, ushort startAddress, float[] data)
  165. => Functions.RegisterFunctions.WriteRegistersFunc(
  166. slaveAddress,
  167. startAddress,
  168. Functions.RegisterFunctions.FloatToByteValueArrays(data, this.wordSize),
  169. this.master,
  170. this.wordSize,
  171. this.endian, this.wordSwapped);
  172. }
  173. }