Skip to content

Instantly share code, notes, and snippets.

@groupsky
Created April 20, 2021 17:28
Show Gist options
  • Save groupsky/0b1ec16d6cda99eb9bb7080a933e0759 to your computer and use it in GitHub Desktop.
Save groupsky/0b1ec16d6cda99eb9bb7080a933e0759 to your computer and use it in GitHub Desktop.
ModBus read and setup for Dio Smart MBSL32DI
#!/usr/bin/env node
/*
* ModBus configuration for Dio Smart MBSL32DI 32-channel digital input module
* https://www.aliexpress.com/item/4001220947199.html
*/
////////////////
// CONFIGURATION
////////////////
const DEVICE = '/dev/ttyUSB0'
// possible values 1200, 2400, 4800, 9600, default 9600
const BAUD_RATE = 9600
// possible values 'even', 'odd', 'none', default 'none'
const PARITY = 'none'
// possible values 1-255, default 1
const ADDRESS = 1
////////////////////
// END CONFIGURATION
////////////////////
const ModbusRTU = require('modbus-serial')
// create an empty modbus client
const client = new ModbusRTU()
// open connection to a serial port
const promise = client
.connectRTUBuffered(DEVICE, { baudRate: BAUD_RATE, parity: PARITY })
// set timeout, if slave did not reply back
.then(() => client.setTimeout(250))
// run configure after connect
.then(async () => {
try {
// set ID of slave
await client.setID(ADDRESS);
// write the new configuration
await client.writeRegisters(0x0002, [
// address between 1 and 255
1,
// baud rate:
// 1 - 4800
// 2 - 9600
// 3 - 19200
// 4 - 38400
// 5 - 57600
// 6 - 115200
2,
// parity check:
// 1 - none
// 2 - odd
// 3 - even
1
])
console.log('Device configured')
} catch (e) {
// if error, handle them here (it should not)
console.log(e)
}
})
.then(async () => new Promise((resolve) => client.close(resolve)))
#!/usr/bin/env node
/*
* ModBus read for Dio Smart MBSL32DI 32-channel digital input module
* https://www.aliexpress.com/item/4001220947199.html
*/
////////////////
// CONFIGURATION
////////////////
const DEVICE = '/dev/ttyUSB0'
// possible values 1200, 2400, 4800, 9600, default 9600
const BAUD_RATE = 9600
// possible values 'even', 'odd', 'none', default 'none'
const PARITY = 'none'
// possible values 1-255, default 1
const ADDRESS = 1
////////////////////
// END CONFIGURATION
////////////////////
const ModbusRTU = require('modbus-serial')
// 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)
const getValue = async () => {
try {
// set ID of slave
await client.setID(ADDRESS);
// read the status of digital inputs
let val = await client.readHoldingRegisters(0x0000, 7)
// combine into single 32 bit integer
const di = val.data[1] << 16 || val.data[0]
const address = val.data[2]
const baudRate = (v => {
switch (v) {
case 1: return 4800
case 2: return 9600
case 3: return 19200
case 4: return 38400
case 5: return 57600
case 6: return 115200
default: return `Unknown value for baud rate ${v}`
}
})(val.data[3])
const parityCheck = (v => {
switch (v) {
case 1: return 'none'
case 2: return 'odd'
case 3: return 'even'
default: return `Unknown value for parity check ${v}`
}
})(val.data[4])
const productVersion = val.data[6]
console.log({
di,
address,
baudRate,
parityCheck,
productVersion
})
} catch(e){
// if error, handle them here (it should not)
console.log(e)
} finally {
// after get all data from salve repeate it again
setImmediate(() => {
getValue()
})
}
}
// start get value
getValue()
{
"name": "test-modbus",
"version": "1.0.0",
"description": "",
"author": "Geno Roupsky",
"license": "ISC",
"dependencies": {
"modbus-serial": "^8.0.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment