Skip to content

Instantly share code, notes, and snippets.

@groupsky
Last active October 8, 2022 16:33
Show Gist options
  • Save groupsky/87f656a61822888ca5567460a4758dba to your computer and use it in GitHub Desktop.
Save groupsky/87f656a61822888ca5567460a4758dba to your computer and use it in GitHub Desktop.
ModBus read for Sinotype DDS519MR Electric Meter (https://s.click.aliexpress.com/e/_eNvkb3)
#!/usr/bin/env node
/*
* ModBus read for Sinotype DDS519MR Electric Meter
* https://s.click.aliexpress.com/e/_eNvkb3
*/
////////////////
// CONFIGURATION
////////////////
const DEVICE = '/dev/ttyUSB0'
// possible values 1200, 2400, 4800, 9600, default 9600
const BAUD_RATE = 9600
// possible values 'even', 'odd', 'none', default 'even'
const PARITY = 'even'
// possible values 1-247, default 20???
const ADDRESS = 20
////////////////////
// END CONFIGURATION
////////////////////
const ModbusRTU = require('modbus-serial')
const ieee754 = require('ieee754')
// create an empty modbus client
const client = new ModbusRTU()
// open connection to a serial port
client.connectRTUBuffered(DEVICE, { baudRate: BAUD_RATE, parity: PARITY })
// set timeout, if slave did not reply back
client.setTimeout(250)
// list of meter's id
const metersIdList = [ADDRESS]
const getMetersValue = async (meters) => {
try{
// get value of all meters
for(let meter of meters) {
// output value to console
console.log(`${meter}:`, await getMeterValue(meter))
// wait 100ms before get another device
await sleep(100)
}
} catch(e){
// if error, handle them here (it should not)
console.log(e)
} finally {
// after get all data from salve repeate it again
setImmediate(() => {
getMetersValue(metersIdList)
})
}
}
const getMeterValue = async (id) => {
try {
// set ID of slave
await client.setID(id)
// Baud rate
let val = await client.readHoldingRegisters(0x00, 2);
const baud = ieee754.read(val.buffer, 0, false, 23, 4)
// Parity
val = await client.readHoldingRegisters(0x02, 2);
const parity = ieee754.read(val.buffer, 0, false, 23, 4)
// Address
val = await client.readHoldingRegisters(0x08, 2);
const address = ieee754.read(val.buffer, 0, false, 23, 4)
// Relay status - according to docs 1 is on, 2 is off, but I read only 0
val = await client.readHoldingRegisters(0x10, 2);
const relay = ieee754.read(val.buffer, 0, false, 23, 4)
// Voltage in Volts
val = await client.readInputRegisters(0x00, 2)
const v = ieee754.read(val.buffer, 0, false, 23, 4)
// Current in Amps
val = await client.readInputRegisters(0x08, 2)
const c = ieee754.read(val.buffer, 0, false, 23, 4)
// Active power in kWh
val = await client.readInputRegisters(0x12, 2)
const p = ieee754.read(val.buffer, 0, false, 23, 4)
// Power factor in cosφ
val = await client.readInputRegisters(0x2A, 2)
const pf = ieee754.read(val.buffer, 0, false, 23, 4)
// Frequency in Hz
val = await client.readInputRegisters(0x36, 2)
const freq = ieee754.read(val.buffer, 0, false, 23, 4)
// Total active power in kWh
val = await client.readInputRegisters(0x100, 2)
const tot = ieee754.read(val.buffer, 0, false, 23, 4)
// return the value
return {baud, parity, address, relay, v, c, p, pf, freq, tot }
} catch(e){
// if error return -1
return -1
}
}
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms))
// start get value
getMetersValue(metersIdList)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment