| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- const {
- paramsState: paramsPageState
- } = require('./motor-control-data')
- const {
- expandItems,
- getAreaKey,
- getGroupItems,
- makeReadSpans,
- mergeReadValues,
- parseRegisterAddress
- } = require('./motor-control-register-groups')
- const transport = require('./ble-transport')
- const modbusAccess = require('./modbus-access')
- const {
- floatToWords,
- toRegisterWord
- } = require('./register-value-utils')
- function hasWriteValue(value) {
- return value !== '' && value !== undefined && value !== null && value !== '--'
- }
- function toWriteNumber(value) {
- if (!hasWriteValue(value)) return null
- const numberValue = Number(value)
- if (!Number.isFinite(numberValue)) return null
- return numberValue
- }
- async function readGroup(data, groupKey) {
- const slaveAddress = modbusAccess.getSharedSlaveAddress()
- if (slaveAddress === null) return false
- const items = expandItems(getGroupItems(data, groupKey))
- const coilItems = items.filter((item) => getAreaKey(item) === 'coil')
- const holdingItems = items.filter((item) => getAreaKey(item) === 'holding')
- const inputItems = items.filter((item) => getAreaKey(item) === 'input')
- const coilSpans = makeReadSpans(coilItems)
- const holdingSpans = makeReadSpans(holdingItems)
- const inputSpans = makeReadSpans(inputItems)
- const readValues = {
- coils: {},
- words: {}
- }
- let sent = false
- if (coilSpans.length) {
- sent = true
- const values = await modbusAccess.readSpans(
- slaveAddress,
- 0x01,
- coilSpans,
- '参数读取',
- 'params-read'
- )
- if (!values) return false
- mergeReadValues(readValues, values)
- }
- if (holdingSpans.length) {
- sent = true
- const values = await modbusAccess.readSpans(
- slaveAddress,
- 0x03,
- holdingSpans,
- '参数读取',
- 'params-read'
- )
- if (!values) return false
- mergeReadValues(readValues, values)
- }
- if (inputSpans.length) {
- sent = true
- const values = await modbusAccess.readSpans(
- slaveAddress,
- 0x04,
- inputSpans,
- '参数读取',
- 'params-read'
- )
- if (!values) return false
- mergeReadValues(readValues, values)
- }
- if (!sent) {
- transport.showCommandAlert('参数读取', '当前分组没有可读取的寄存器')
- return false
- }
- if (!Object.keys(readValues.coils).length && !Object.keys(readValues.words).length) {
- return false
- }
- return paramsPageState.applyReadValues(data, readValues)
- }
- async function buildHoldingWriteEntries(slaveAddress, items) {
- const normalEntries = []
- const byteGroups = {}
- items.forEach((item) => {
- if (getAreaKey(item) !== 'holding') return
- if (item.type === 'uint8_t' && item.bytePosition) {
- const address = parseRegisterAddress(item.address)
- const group = byteGroups[address] || {
- address,
- high: null,
- low: null
- }
- group[item.bytePosition] = item
- byteGroups[address] = group
- return
- }
- const writeNumber = toWriteNumber(item.writeValue)
- if (writeNumber === null) return
- const words = item.type === 'float'
- ? floatToWords(writeNumber)
- : [toRegisterWord(writeNumber)]
- if (!words || words.some((word) => word === null)) return
- normalEntries.push({
- address: parseRegisterAddress(item.address),
- label: item.name,
- values: words
- })
- })
- for (const addressText of Object.keys(byteGroups)) {
- const group = byteGroups[addressText]
- const highValue = group.high ? toRegisterWord(group.high.writeValue) : null
- const lowValue = group.low ? toRegisterWord(group.low.writeValue) : null
- if (highValue === null && lowValue === null) continue
- let baseWord = 0
- if (highValue === null || lowValue === null) {
- const readWord = await modbusAccess.readSingleHoldingWord(
- slaveAddress,
- group.address,
- '读取配对寄存器',
- 'params-pair-read'
- )
- if (!Number.isInteger(readWord)) continue
- baseWord = readWord
- }
- const nextHigh = highValue === null ? ((baseWord >> 8) & 0xFF) : highValue
- const nextLow = lowValue === null ? (baseWord & 0xFF) : lowValue
- if (nextHigh > 0xFF || nextLow > 0xFF) continue
- normalEntries.push({
- address: group.address,
- label: '8位参数',
- values: [(nextHigh << 8) | nextLow]
- })
- }
- return normalEntries.sort((left, right) => left.address - right.address)
- }
- async function writeGroup(data, groupKey) {
- const slaveAddress = modbusAccess.getSharedSlaveAddress()
- if (slaveAddress === null) return false
- const items = expandItems(getGroupItems(data, groupKey))
- const coilItems = items.filter((item) => getAreaKey(item) === 'coil' && item.isDirty)
- const holdingItems = items.filter((item) => getAreaKey(item) === 'holding')
- let sent = false
- for (const item of coilItems) {
- const checked = Number(item.writeValue) !== 0
- const address = parseRegisterAddress(item.address)
- sent = true
- const response = await modbusAccess.writeSingleCoil(
- slaveAddress,
- address,
- checked,
- item.name,
- 'params-coil-write'
- )
- if (!response) return false
- }
- const holdingEntries = await buildHoldingWriteEntries(slaveAddress, holdingItems)
- for (const entry of holdingEntries) {
- sent = true
- const response = await modbusAccess.writeMultipleRegisters(
- slaveAddress,
- entry.address,
- entry.values,
- entry.label,
- 'params-holding-write'
- )
- if (!response) return false
- }
- if (!sent) {
- transport.showCommandAlert('参数写入', '当前分组没有可写入的参数')
- }
- return sent
- }
- async function writeSwitchRegister(item) {
- const slaveAddress = modbusAccess.getSharedSlaveAddress()
- if (slaveAddress === null || !item) return false
- const address = parseRegisterAddress(item.address)
- const checked = Number(item.writeValue) !== 0
- return modbusAccess.writeSingleCoil(
- slaveAddress,
- address,
- checked,
- item.name,
- 'params-switch-write'
- )
- }
- module.exports = {
- readGroup,
- writeGroup,
- writeSwitchRegister
- }
|