Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
modbus-serial
Advanced tools
A pure JavaScript implemetation of MODBUS-RTU (and TCP) for NodeJS
This class makes ModbusRTU (and TCP) calls fun and easy.
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.
npm install modbus-serial
For use over serial port (ModbusRTU), also install node-serialport:
npm install serialport
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 tcp line
client.connectTCP("192.168.1.42", run);
// read the values of 10 registers starting at address 0
// on device number 1. and log the values to the console.
function run() {
client.setID(1);
client.readInputRegisters(0, 10)
.then(console.log)
.then(run);
}
// 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);
}
// 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});
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("192.168.1.42");
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 serial port
client.connectRTU("/dev/ttyUSB0", {baudrate: 9600}, run);
function run() {
client.setID(1);
// read 2 16bit-registers to get one 32bit number
client.readInputRegisters(5, 2, function(err, data) {
var int32 = data.buffer.readUInt32BE();
console.log(int32);
});
}
This communication functions use a pre-set unit-id and can return a promise, Using callbacks is optional.
// set the client's unit id
client.setID(1);
// read 8 discrete inputs starting at input 10
// (function use the unit id 1, we set earlier)
client.readDiscreteInputs(10, 8)
.then(function(data) {
console.log(data);
});
Sets the unit id
The new client id
Writes "Read Coils" (FC=1) request to serial port.
The Data Address of the first register.
The total number of registers requested.
Writes "Read Discrete Inputs" (FC=2) request to serial port.
The Data Address of the first register.
The total number of registers requested.
Writes "Read Holding Registers" (FC=3) request to serial port.
The Data Address of the first register.
The total number of registers requested.
Writes "Read Input Registers" (FC=4) request to serial port.
The Data Address of the first register.
The total number of registers requested.
Writes "Force Coil Status" (FC=5) request to serial port.
The Data Address of the coil.
The state to force into coil.
Writes "Preset Multiple Registers" (FC=16) request to serial port.
The Data Address of the first register.
The array of values to set into the registers.
This communication functions use callbacks.
// read 8 holding registers starting at register 10
// (function use the unit id 1)
client.writeFC3(1, 10, 8, function(err, data) {
if (err) {
console.log(err);
} else {
console.log(data);
});
Opens a modbus connection using the given serial port.
Called when a connection has been opened.
Writes "Read coil status" (FC=01) request to serial port.
The slave unit address.
The Data Address of the first coil.
The total number of coils requested.
Called once the unit returns an answer. The callback should be a function that looks like: function (error, data) { ... }
error - null on success, error string o/w
data - an object with two fildes:
data.data: array of boolean coils (in multiples of 8 = one byte).
data.buffer: raw baffer of bytes returned by slave.
Writes "Read input status" (FC=02) request to serial port.
The slave unit address.
The Data Address of the first digital input.
The total number of digital inputs requested.
Called once the unit returns an answer. The callback should be a function that looks like: function (error, data) { ... }
error - null on success, error string o/w
data - an object with two fildes:
data.data: array of boolean digital inputs (in multiples of 8 = one byte).
data.buffer: raw baffer of bytes returned by slave.
Writes "Read Holding Registers" (FC=03) request to serial port.
The slave unit address.
The Data Address of the first register.
The total number of registers requested.
Called once the unit returns an answer. The callback should be a function that looks like: function (error, data) { ... }
error - null on success, error string o/w
data - an object with two fildes:
data.data: array of unsinged 16 bit registers.
data.buffer: raw baffer of bytes returned by slave.
Writes "Read Input Registers" (FC=04) request to serial port.
The slave unit address.
The Data Address of the first register.
The total number of registers requested.
Called once the unit returns an answer. The callback should be a function that looks like: function (error, data) { ... }
error - null on success, error string o/w
data - an object with two fildes:
data.data: array of unsinged 16 bit registers.
data.buffer: raw baffer of bytes returned by slave.
Writes "Force Single Coil" (FC=05) request to serial port.
The slave unit address.
The Data Address of the coil.
The state to set into the coil (true / false).
Called once the unit returns an answer. The callback should be a function that looks like: function (error, data) { ... }
Writes "Preset Multiple Registers" (FC=16) request to serial port.
The slave unit address.
The Data Address of the first register.
The array of values to set into the registers.
Called once the unit returns an answer. The callback should be a function that looks like: function (error, data) { ... }
The shorthand connection functions creates a port and open it.
Long way, without shorthand:
// open a serial port
var SerialPort = require("serialport").SerialPort;
var serialPort = new SerialPort("/dev/ttyUSB0", {baudrate: 9600});
// create a modbus client using the serial port
var ModbusRTU = require("modbus-serial");
var client = new ModbusRTU(serialPort);
// open connection to a serial port
client.open();
// tell your coffee machine to do something ...
Using shorthand:
// 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});
// tell your robot to do something ...
Using shorthand (TCP):
// create an empty modbus client
var ModbusRTU = require("modbus-serial");
var client = new ModbusRTU();
// open connection to a tcp line
client.connectTCP("192.168.1.42");
// tell your robot to do something ...
Connect using serial port.
The port path (e.g. "/dev/ttyS0")
The options for this connection.
Called once the client is connected.
Connect using tcp/ip.
The port ip (e.g. "24.230.1.42")
The options for this connection.
Called once the client is connected.
Connect using a telnet server
The port ip (e.g. "24.230.1.42")
The options for this connection.
Called once the client is connected.
FAQs
A pure JavaScript implemetation of MODBUS-RTU (Serial and TCP) for NodeJS.
The npm package modbus-serial receives a total of 2,645 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
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.