Skip to content

Commit

Permalink
Add a tryWriteHoldingRegister wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Jalle19 committed Aug 15, 2024
1 parent 4afa0d5 commit 0701b9c
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions app/modbus.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ export const getSettings = async (modbusClient) => {
}

export const setSetting = async (modbusClient, setting, value) => {
if (AVAILABLE_SETTINGS[setting] === undefined) {
const dataAddress = AVAILABLE_SETTINGS[setting]
if (dataAddress === undefined) {
throw new Error('Unknown setting')
}

Expand Down Expand Up @@ -275,9 +276,9 @@ export const setSetting = async (modbusClient, setting, value) => {

// This isn't very nice, but it's good enough for now
if (coil) {
await mutex.runExclusive(async () => tryWriteCoil(modbusClient, AVAILABLE_SETTINGS[setting], value))
await mutex.runExclusive(async () => tryWriteCoil(modbusClient, dataAddress, value))
} else {
await mutex.runExclusive(async () => modbusClient.writeRegister(AVAILABLE_SETTINGS[setting], intValue))
await mutex.runExclusive(async () => tryWriteHoldingRegister(modbusClient, dataAddress, intValue))
}
}

Expand Down Expand Up @@ -430,3 +431,13 @@ const tryReadHoldingRegisters = async (modbusClient, dataAddress, length) => {
throw e
}
}

const tryWriteHoldingRegister = async (modbusClient, dataAddress, value) => {
try {
logger.debug(`Writing ${value} to holding register address ${dataAddress}`)
return await modbusClient.writeRegister(dataAddress, value)
} catch (e) {
logger.error(`Failed to write holding register address ${dataAddress}, value ${value}`)
throw e
}
}

0 comments on commit 0701b9c

Please sign in to comment.