Security News
Combatting Alert Fatigue by Prioritizing Malicious Intent
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
modbus-serial
Advanced tools
A pure JavaScript implemetation of MODBUS-RTU (Serial and TCP) for NodeJS.
A pure JavaScript implemetation of MODBUS-RTU (Serial and TCP) for NodeJS.
Modbus is a serial communications protocol, first used in 1979. Modbus is simple and robust, openly published, royalty-free and easy to deploy and maintain.
This package makes Modbus calls and serve fun and easy.
npm install modbus-serial
try these options on npm install to build, if you have problems to install
--unsafe-perm --build-from-source
This class makes it fun and easy to communicate with electronic devices such as irrigation controllers, protocol droids and robots. It talks with devices that use a serial line (e.g. RS485, RS232). Many industrial electronic devices implement modbus. Arduino can also talk modbus and you can control your projects and robots using modbus.
Arduino libraries for modbus slave:
Arduino sketch for irrigation timer with modbus support:
Node Modbus-WebSocket bridge:
// create an empty modbus client
var ModbusRTU = require("modbus-serial");
var client = new ModbusRTU();
// open connection to a serial port
client.connectRTU("/dev/ttyUSB0", { baudRate: 9600 }, write);
function write() {
client.setID(1);
// write the values 0, 0xffff to registers starting at address 5
// on device number 1.
client.writeRegisters(5, [0 , 0xffff])
.then(read);
}
function read() {
// read the 2 registers starting at address 5
// on device number 1.
client.readHoldingRegisters(5, 2)
.then(console.log);
}
const ModbusRTU = require("modbus-serial");
// create an empty modbus client
const client = new ModbusRTU();
// open connection to a serial port
client.connectRTUBuffered("/dev/ttyS0", { baudRate: 9600 });
// set timeout, if slave did not reply back
client.setTimeout(500);
// list of meter's id
const metersIdList = [10, 11, 12, 13, 14];
const getMetersValue = async (meters) => {
try{
// get value of all meters
for(let meter of meters) {
// output value to console
console.log(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);
// read the 1 registers starting at address 0 (first register)
let val = await client.readInputRegisters(0, 1);
// return the value
return val.data[0];
} catch(e){
// if error return -1
return -1
}
}
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
// start get value
getMetersValue(metersIdList);
// create an empty modbus client
var ModbusRTU = require("modbus-serial");
var client = new ModbusRTU();
// open connection to a serial port
client.connectRTUBuffered("/dev/ttyUSB0", { baudRate: 9600 });
client.setID(1);
// read the values of 10 registers starting at address 0
// on device number 1. and log the values to the console.
setInterval(function() {
client.readHoldingRegisters(0, 10, function(err, data) {
console.log(data.data);
});
}, 1000);
// create an empty modbus client
var ModbusRTU = require("modbus-serial");
var client = new ModbusRTU();
// open connection to a tcp line
client.connectTCP("127.0.0.1", { port: 8502 });
client.setID(1);
// read the values of 10 registers starting at address 0
// on device number 1. and log the values to the console.
setInterval(function() {
client.readHoldingRegisters(0, 10, function(err, data) {
console.log(data.data);
});
}, 1000);
// create an empty modbus client
var ModbusRTU = require("modbus-serial");
var vector = {
getInputRegister: function(addr, unitID) { return addr; },
getHoldingRegister: function(addr, unitID) { return addr + 8000; },
getCoil: function(addr, unitID) { return (addr % 2) === 0; },
setRegister: function(addr, value, unitID) { console.log("set register", addr, value, unitID); return; },
setCoil: function(addr, value, unitID) { console.log("set coil", addr, value, unitID); return; }
};
// set the server to answer for modbus requests
console.log("ModbusTCP listening on modbus://0.0.0.0:8502");
var serverTCP = new ModbusRTU.ServerTCP(vector, { host: "0.0.0.0", port: 8502, debug: true, unitID: 1 });
serverTCP.on("socketError", function(err){
// Handle socket error if needed, can be ignored
console.error(err);
});
to get more see Examples
FAQs
A pure JavaScript implemetation of MODBUS-RTU (Serial and TCP) for NodeJS.
The npm package modbus-serial receives a total of 4,419 weekly downloads. As such, modbus-serial popularity was classified as popular.
We found that modbus-serial demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.