A pure JavaScript implemetation of MODBUS-RTU (and TCP) for NodeJS
A pure JavaScript implemetation of MODBUS-RTU (and TCP) for NodeJS
A pure JavaScript implementation 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
serialport dependencyserialport is an optional dependency: npm tries to install it, but if the install fails (or you skip optionals), modbus-serial still installs and TCP/UDP usage works.
The version range pulled in by this package targets serialport 13.x, which requires Node.js 20 or newer. If you need serial on an older Node release, install a compatible serialport major (for example 12.x) explicitly alongside modbus-serial, or use --no-optional and TCP-only flows.
Skip installing optional dependencies (no native serial build, smaller tree):
npm install modbus-serial --no-optional
Or in .npmrc: optional=false
If you skipped or dropped serialport and later need serial RTU/ASCII, install it explicitly:
npm install serialport
Without serialport, serial-specific APIs fail at runtime when used, for example:
ModbusRTU.getPorts(), connectRTU, connectRTUBuffered, connectAsciiSerialModbusRTU.RTUBufferedPort, ModbusRTU.ServerSerial (not exported if serialport cannot be loaded)TypeScript: ServerSerial.d.ts imports serialport types. If you use --no-optional and have no serialport in the tree, use skipLibCheck or add serialport as a devDependency for type-checking.
This repo uses npm and a committed package-lock.json. After cloning:
npm ci
npm run lint
npm test
To refresh dependencies after editing package.json, run npm install and commit the updated lockfile.
Optional maintainer tasks (generated output is gitignored):
npm run clean — remove modbus-serial/ and docs/gen/npm run build — copy apis, ports, servers, and utils into modbus-serial/npm run doc — generate API HTML under docs/gen/ (JSDoc 4 + clean-jsdoc-theme)npm run doc:examples — copy examples/ into docs/gen/examples/npm run docs — run doc then doc:examplesnpm run publish:prepare — runs build then docs (mirrored package tree plus generated HTML and copied examples)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:
This module has not been tested on every single version of NodeJS. For best results you should stick to LTS versions, which are denoted by even major version numbers e.g. 4.x, 6.x, 8.x.
| Class | Function |
|---|---|
| FC1 "Read Coil Status" | readCoils(coil, len) |
| FC2 "Read Input Status" | readDiscreteInputs(addr, arg) |
| FC3 "Read Holding Registers" | readHoldingRegisters(addr, len) |
| FC4 "Read Input Registers" | readInputRegisters(addr, len) |
| FC5 "Force Single Coil" | writeCoil(coil, binary) //NOT setCoil |
| FC6 "Preset Single Register" | writeRegister(addr, value) |
| FC15 "Force Multiple Coil" | writeCoils(addr, valueAry) |
| FC16 "Preset Multiple Registers" | writeRegisters(addr, valueAry) |
| FC22 "Mask Write Register" | maskWriteRegister(addr, andMask, orMask) |
| FC43/14 "Read Device Identification" (supported ports: TCP, RTU) | readDeviceIdentification(id, obj) |
| CustomFC "Custom Function" | customFunction(functionCode, data) |
…
…
// create an empty modbus client
const ModbusRTU = require("modbus-serial");
const 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
const ModbusRTU = require("modbus-serial");
const 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
const ModbusRTU = require("modbus-serial");
const client = new ModbusRTU();
// open connection to a udp line
client.connectUDP("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);
…
…
Here is an example of using modbusdb wrapper over modbus-serial:
…
Enums:
ScopeEnum: (Modbus Table)
1 = PhysicalState = Discrete Input
2 = InternalState = Coil Status
3 = PhysicalRegister = Input Register
4 = InternalRegister = Holding Register
TypeEnum: (Available Data Types)
1 = Bit,
4 = Int16,
5 = UInt16,
6 = Int32,
7 = UInt32,
8 = Float
to get more see Examples
node-modbus-serial use node-serialport for serial communication, for serial port options settings it passes to serial port the openOptions object, default serial port settings are 9600,8,n,1.
client.connectRTUBuffered("/dev/ttyUSB0", { baudRate: 9600, parity: 'even' });
No open issues yet, or sync has not completed.