1
0

modbus-access.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. const {
  2. buildReadFrame,
  3. buildWriteMultipleRegistersFrame,
  4. buildWriteSingleCoilFrame,
  5. buildWriteSingleRegisterFrame,
  6. getMaxReadQuantity,
  7. getMaxWriteMultipleRegisterQuantity
  8. } = require('./modbus-rtu')
  9. const settingsService = require('./settings-service')
  10. const transport = require('./ble-transport')
  11. const {
  12. addCoilReadValues,
  13. addWordReadValues
  14. } = require('./register-value-utils')
  15. function getSharedSlaveAddress(title = '从机地址错误') {
  16. try {
  17. return settingsService.getModbusSlaveAddress()
  18. } catch (error) {
  19. transport.showCommandAlert(title, error.message)
  20. return null
  21. }
  22. }
  23. function formatAddress(value) {
  24. return Number(value || 0).toString(16).toUpperCase()
  25. }
  26. function getChunkLabel(label, chunks, chunk) {
  27. if (!label || chunks.length <= 1) return label
  28. return `${label} ${formatAddress(chunk.address)}-${formatAddress(chunk.address + chunk.quantity - 1)}`
  29. }
  30. function splitQuantity(startAddress, quantity, maxQuantity) {
  31. const chunks = []
  32. let address = Number(startAddress) || 0
  33. let remaining = Math.max(0, Math.floor(Number(quantity) || 0))
  34. const chunkLimit = Math.max(1, Math.floor(Number(maxQuantity) || remaining || 1))
  35. while (remaining > 0) {
  36. const chunkQuantity = Math.min(remaining, chunkLimit)
  37. chunks.push({
  38. address,
  39. quantity: chunkQuantity
  40. })
  41. address += chunkQuantity
  42. remaining -= chunkQuantity
  43. }
  44. return chunks
  45. }
  46. function getReadChunks(functionCode, startAddress, quantity, options = {}) {
  47. const maxQuantity = getMaxReadQuantity(functionCode, options.maxFrameBytes)
  48. return splitQuantity(startAddress, quantity, maxQuantity || quantity)
  49. }
  50. async function sendReadChunk(slaveAddress, functionCode, chunk, label, kind, options = {}) {
  51. return transport.sendManagedFrame(
  52. buildReadFrame(slaveAddress, functionCode, chunk.address, chunk.quantity, {
  53. maxFrameBytes: options.maxFrameBytes
  54. }),
  55. label,
  56. {
  57. address: chunk.address,
  58. functionCode,
  59. kind,
  60. quantity: chunk.quantity,
  61. slaveAddress
  62. },
  63. {
  64. maxFrameBytes: options.maxFrameBytes,
  65. showModal: options.showModal
  66. }
  67. )
  68. }
  69. async function readSpans(slaveAddress, functionCode, spans, label, kind, options = {}) {
  70. const readValues = {
  71. coils: {},
  72. words: {}
  73. }
  74. const normalizedSpans = (spans || []).filter((span) => span && span.quantity > 0)
  75. for (const span of normalizedSpans) {
  76. const chunks = getReadChunks(functionCode, span.address, span.quantity, options)
  77. for (const chunk of chunks) {
  78. const response = await sendReadChunk(
  79. slaveAddress,
  80. functionCode,
  81. chunk,
  82. getChunkLabel(label, chunks, chunk),
  83. kind,
  84. options
  85. )
  86. if (!response) return null
  87. if (functionCode === 0x01 || functionCode === 0x02) {
  88. addCoilReadValues(readValues, chunk.address, chunk.quantity, response)
  89. } else {
  90. addWordReadValues(readValues, chunk.address, response)
  91. }
  92. if (typeof options.onChunk === 'function') {
  93. options.onChunk(response, chunk)
  94. }
  95. }
  96. }
  97. return readValues
  98. }
  99. async function readRegisterWords(slaveAddress, functionCode, startAddress, quantity, label, kind, options = {}) {
  100. const words = []
  101. const chunks = getReadChunks(functionCode, startAddress, quantity, options)
  102. for (const chunk of chunks) {
  103. const response = await sendReadChunk(
  104. slaveAddress,
  105. functionCode,
  106. chunk,
  107. getChunkLabel(label, chunks, chunk),
  108. kind,
  109. options
  110. )
  111. if (!response) return null
  112. const chunkWords = response.words || []
  113. chunkWords.forEach((word, index) => {
  114. words[chunk.address - startAddress + index] = Number(word) & 0xFFFF
  115. })
  116. if (typeof options.onChunk === 'function') {
  117. options.onChunk(response, chunk)
  118. }
  119. }
  120. return words
  121. }
  122. async function readBitValues(slaveAddress, functionCode, startAddress, quantity, label, kind, options = {}) {
  123. const result = await readSpans(
  124. slaveAddress,
  125. functionCode,
  126. [{ address: startAddress, quantity }],
  127. label,
  128. kind,
  129. options
  130. )
  131. return result ? result.coils : null
  132. }
  133. async function readSingleHoldingWord(slaveAddress, address, label = '读取配对寄存器', kind = 'holding-word-read') {
  134. const words = await readRegisterWords(slaveAddress, 0x03, address, 1, label, kind)
  135. return words && Number.isInteger(words[0]) ? words[0] & 0xFFFF : null
  136. }
  137. function writeSingleCoil(slaveAddress, address, checked, label, kind = 'coil-write', options = {}) {
  138. const coilValue = checked ? 0xFF00 : 0x0000
  139. return transport.sendManagedFrame(
  140. buildWriteSingleCoilFrame(slaveAddress, address, checked),
  141. label,
  142. {
  143. address,
  144. functionCode: 0x05,
  145. kind,
  146. quantity: 1,
  147. slaveAddress,
  148. value: coilValue
  149. },
  150. {
  151. maxFrameBytes: options.maxFrameBytes,
  152. showModal: options.showModal
  153. }
  154. )
  155. }
  156. function writeSingleRegister(slaveAddress, address, value, label, kind = 'register-write', options = {}) {
  157. return transport.sendManagedFrame(
  158. buildWriteSingleRegisterFrame(slaveAddress, address, value),
  159. label,
  160. {
  161. address,
  162. functionCode: 0x06,
  163. kind,
  164. quantity: 1,
  165. slaveAddress,
  166. value
  167. },
  168. {
  169. maxFrameBytes: options.maxFrameBytes,
  170. showModal: options.showModal
  171. }
  172. )
  173. }
  174. function writeMultipleRegisters(slaveAddress, address, values, label, kind = 'registers-write', options = {}) {
  175. return transport.sendManagedFrame(
  176. buildWriteMultipleRegistersFrame(slaveAddress, address, values, {
  177. maxFrameBytes: options.maxFrameBytes
  178. }),
  179. label,
  180. {
  181. address,
  182. functionCode: 0x10,
  183. kind,
  184. quantity: values.length,
  185. slaveAddress
  186. },
  187. {
  188. maxFrameBytes: options.maxFrameBytes,
  189. showModal: options.showModal
  190. }
  191. )
  192. }
  193. module.exports = {
  194. getReadChunks,
  195. getSharedSlaveAddress,
  196. getMaxReadQuantity,
  197. readBitValues,
  198. readRegisterWords,
  199. readSingleHoldingWord,
  200. readSpans,
  201. splitQuantity,
  202. getMaxWriteMultipleRegisterQuantity,
  203. writeMultipleRegisters,
  204. writeSingleCoil,
  205. writeSingleRegister
  206. }