npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

modbusrtu

v0.2.1

Published

NodeJS module to communicate with devices, that support the Modbus RTU protocol.

Downloads

8

Readme

node-modbusrtu

The modbusrtu package is Node.js module to communicate with devices, that support the Modbus RTU protocol. Written in native JavaScript

It requires Node.js to run and npm to be installed.

This project now supported only GNU/Linux environment and following Modbus functions:

  • 0x01 - read coils
  • 0x02 - read discrete inputs
  • 0x03 - read holding registers
  • 0x04 - read input registers
  • 0x05 - write single coil
  • 0x06 - write single register
  • 0x0f - write multiple coils
  • 0x10 - write multiple registers

Installing

NPM NPM

  • Latest packaged version: npm install modbusrtu

  • Latest version on GitHub: npm install https://github.com/Serge78rus/node-modbusrtu/tarball/master

Usage

Simple example of usage

Create Modbus object:

var Modbus = require("modbus").Modbus;

var modbus = new Modbus(
		"/dev/ttyUSB1", //communication serial device 
		{},             //communication options (default: 9600, 8N2)
		function(err) {
	if (!err) {
		/* Code for using modbus object	*/
	} else
		console.error("Modbus constructor error: " + err);
});

Read coils:

modbus.readCoils(
		1, //slave address 
		0, //first coil address
		10, //number of coils to read 
		function(err, data) {
  if (!err) {
  	/* Code for using boolean array "data", contains coils status */
  } else {
    console.error("readCoils() error: " + err);
  }
});

Read discrete inputs:

modbus.readDiscrInps(
		1, //slave addres 
		0, //first discrete input address
		10, //number of inputs to read
		function(err, data) {
  if (!err) {
  	/* Code for using boolean array "data", contains inputs status */
  } else {
    console.error("readDiscrInps() error: " + err);
  }
});

Read holding registers:

modbus.readHoldRegs(
		1, //slave addres 
		0, //first holding register address
		10, //number of registers to read
		function(err, data) {
  if (!err) {
  	/* Code for using unsigned integer array "data", contains holding registers values */
  } else {
    console.error("readHoldRegs() error: " + err);
  }
});

Read input registers:

modbus.readInpRegs(
		1, //slave addres 
		0, //first input register address
		10, //number of registers to read
		function(err, data) {
  if (!err) {
  	/* Code for using unsigned integer array "data", contains input registers values */
  } else {
    console.error("readInpRegs() error: " + err);
  }
});

More complex example of usage in test/test.js

//var Modbus = require("../lib/modbus").Modbus; //relative path from test directory 
var Modbus = require("modbus").Modbus; //if module installed to default location

var tests = [
  {func: "readCoils",     pars: [1, 0, 10]},
  {func: "readDiscrInps", pars: [1, 0, 10]},
  {func: "readHoldRegs",  pars: [1, 0, 10]},
  {func: "readInpRegs",   pars: [1, 0, 10]},
  {func: "writeCoil",     pars: [1, 0, true]},
  {func: "readCoils",     pars: [1, 0, 1]},
  {func: "writeCoil",     pars: [1, 0, false]},
  {func: "readCoils",     pars: [1, 0, 1]},
  {func: "writeReg",      pars: [1, 0, 123]},
  {func: "readHoldRegs",  pars: [1, 0, 1]},
  {func: "writeReg",      pars: [1, 0, 321]},
  {func: "readHoldRegs",  pars: [1, 0, 1]},
  {func: "writeCoils",    pars: [1, 0, [true, false, true, false, true, 
                                        false, true, false, true, false]]},
  {func: "readCoils",     pars: [1, 0, 10]},
  {func: "writeCoils",    pars: [1, 0, [false, true, false, true, false, 
                                        true, false, true, false, true]]},
  {func: "readCoils",     pars: [1, 0, 10]},
  {func: "writeRegs",     pars: [1, 0, [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]]},
  {func: "readHoldRegs",  pars: [1, 0, 10]},
  {func: "writeRegs",     pars: [1, 0, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]},
  {func: "readHoldRegs",  pars: [1, 0, 10]}
];

var modbus = new Modbus("/dev/ttyUSB1", {}, function(err) {
	if (!err) {
		(function test(i) {
			if (tests[i]) {
				var f = "function: " + tests[i].func + 
						"() pars: " + tests[i].pars + 
						" \tresult: "; 
				modbus[tests[i].func].apply(modbus, 
						tests[i].pars.concat(function(err, data) {
					if (!err)
						console.log(f + "OK" + (data ? ", data: " + data : ""));
					else 
						console.log(f + err);
					test(++i);
				}));
			} else
				console.log("end tests");
		})(0);
	} else
		console.error("Modbus constructor error: " + err);
});

API

Modbus(dev, opt, done)

Constructor of modbus object

  • dev - communication device name (for example: /dev/ttyUSB0)
  • opt - communication options
  • done - callback function

Object opt may contain some of the following fields:

{
	baud: 9600, //communication speed
	fmt: "8n2", //data bits, parity and stop bits
	timeout: 1000, //response timeout
	pause: 200, //pause between response and next request
	retry: 3 //retry counts
}

If some fields are omitted, they take default values as described above

Function done take one argument:

done(err)

where err is null if success or Error object if an error occurred

readCoils(slave, addr, cnt, done)

Function for reading coils (Modbus function 0x01)

  • slave - slave device Id
  • addr - first coil address
  • cnt - number of coils to read
  • done - callback function

Function done take two arguments:

done(err, data)

where err is null if success or Error object if an error occurred, data - array of boolean values of results

readDiscrInps(slave, addr, cnt, done)

Function for reading discrete inputs (Modbus function 0x02)

  • slave - slave device Id
  • addr - first input address
  • cnt - number of inputs to read
  • done - callback function

Function done take two arguments:

done(err, data)

where err is null if success or Error object if an error occurred, data - array of boolean values of results

readHoldRegs(slave, addr, cnt, done)

Function for reading holding registers (Modbus function 0x03)

  • slave - slave device Id
  • addr - first register address
  • cnt - number of registers to read
  • done - callback function

Function done take two arguments:

done(err, data)

where err is null if success or Error object if an error occurred, data - array of unsigned integer values of results

readInpRegs(slave, addr, cnt, done)

Function for reading input registers (Modbus function 0x04)

  • slave - slave device Id
  • addr - first register address
  • cnt - number of registers to read
  • done - callback function

Function done take two arguments:

done(err, data)

where err is null if success or Error object if an error occurred, data - array of unsigned integer values of results

writeCoil(slave, addr, val, done)

Function for writing single coil (Modbus function 0x05)

  • slave - slave device Id
  • addr - coil address
  • val - value to write
  • done - callback function

Function done take one argument:

done(err)

where err is null if success or Error object if an error occurred

writeReg(slave, addr, val, done)

Function for writing single register (Modbus function 0x06)

  • slave - slave device Id
  • addr - register address
  • val - value to write
  • done - callback function

Function done take one argument:

done(err)

where err is null if success or Error object if an error occurred

writeCoils(slave, addr, vals, done)

Function for writing multiple coils (Modbus function 0x0f)

  • slave - slave device Id
  • addr - first coil address
  • vals - array of boolean values to write
  • done - callback function

Function done take one argument:

done(err)

where err is null if success or Error object if an error occurred

writeRegs(slave, addr, vals, done)

Function for writing multiple registers (Modbus function 0x10)

  • slave - slave device Id
  • addr - first coil address
  • vals - array of unsigned integer values to write
  • done - callback function

Function done take one argument:

done(err)

where err is null if success or Error object if an error occurred

License

MIT license (see the LICENSE file).