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

modbusemu

v0.1.4

Published

Node.js module for Modbus RTU devices emulation

Downloads

7

Readme

node-modbusemu

The modbusemu package is Node.js module for Modbus RTU devices emulation.

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 modbusemu

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

Usage

Example of usage in test.js

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

//emulation data
var data = {
		coils: {
			minaddr: 0,
			maxaddr: 10,
			vals: []
		},
		discrInps: {
			minaddr: 0,
			maxaddr: 10,
			vals: []
		},
		holdRegs: {
			minaddr: 0,
			maxaddr: 10,
			vals: []
		},
		inpRegs: {
			minaddr: 0,
			maxaddr: 10,
			vals: []
		}
}; 

//data init 
for (var i = data.coils.minaddr; i <= data.coils.maxaddr; ++i) {
	data.coils.vals[i] = i & 0x1 ? true : false;
}
for (var i = data.discrInps.minaddr; i <= data.discrInps.maxaddr; ++i) {
	data.discrInps.vals[i] = i & 0x1 ? true : false;
}
for (var i = data.holdRegs.minaddr; i <= data.holdRegs.maxaddr; ++i) {
	data.holdRegs.vals[i] = i;
}
for (var i = data.inpRegs.minaddr; i <= data.inpRegs.maxaddr; ++i) {
	data.inpRegs.vals[i] = i;
}

//start emulation
var emulator = new Emulator(
		"/dev/ttyUSB0", //communication device 
		1, //slave address
		{}, //communication options (default)
		{ //user functions
			readCoils: function(addr, cnt) { //user function for read coils
				if (addr >= data.coils.minaddr && addr + cnt <= data.coils.maxaddr) {
					return data.coils.vals.slice(addr, addr + cnt);
				}
			},
			readDiscrInps: function(addr, cnt) { //user function for read discrete inputs
				if (addr >= data.discrInps.minaddr && addr + cnt <= data.discrInps.maxaddr) {
					return data.discrInps.vals.slice(addr, addr + cnt);
				}
			},
			readHoldRegs: function(addr, cnt) { //user function for read holding registers
				if (addr >= data.holdRegs.minaddr && addr + cnt <= data.holdRegs.maxaddr) {
					return data.holdRegs.vals.slice(addr, addr + cnt);
				}
			},
			readInpRegs: function(addr, cnt) { //user function for read input registers
				if (addr >= data.inpRegs.minaddr && addr + cnt <= data.inpRegs.maxaddr) {
					return data.inpRegs.vals.slice(addr, addr + cnt);
				}
			},
			writeCoils: function(addr, vals) { //user function for write coils
				var cnt = vals.length;
				if (addr >= data.coils.minaddr && addr + cnt <= data.coils.maxaddr) {
					for (var i = 0; i < cnt; ++i) {
						data.coils.vals[addr + i] = vals[i];
					}
					return true;
				}
			},
			writeRegs: function(addr, vals) { //user function for write holding registers
				var cnt = vals.length;
				if (addr >= data.holdRegs.minaddr && addr + cnt <= data.holdRegs.maxaddr) {
					for (var i = 0; i < cnt; ++i) {
						data.holdRegs.vals[addr + i] = vals[i];
					}
					return true;
				}
			}
		}, 
		function(err) { //done callback function
			if (err) {
				console.error("Emulator constructor error: " + err);
			}
		}
);

API

Modbus(dev, slave, opt, functions, done)

Constructor of emulator object

  • dev - communication device name (for example: /dev/ttyUSB0)
  • slave - slave address
  • opt - communication options
  • functions - hash array of user functions
  • done - callback function

Object opt may contain some of the following fields:

{
	baud: 9600, //communication speed
	fmt: "8n2", //data bits, parity and stop bits
}

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

Object functions may contain some of the following user functions:

{
	readCoils: function(addr, cnt) { /* user function for read coils */ },
	readDiscrInps: function(addr, cnt) { /* user function for read discrete inputs */ },
	readHoldRegs: function(addr, cnt) { /* user function for read holding registers */ },
	readInpRegs: function(addr, cnt) { /* user function for read input registers */ },
	writeCoils: function(addr, vals) { /* user function for write coils */ },
	writeRegs: function(addr, vals) { / *user function for write holding registers */ }
}

If some of the user functions are omitted, the corresponding Modbus protocol functions will return an exception code 1 (illegal function).

Function done take one argument:

done(err)

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

User functions

readCoils(addr, cnt)

User function for reading coils (need for Modbus function 0x01)

  • addr - first coil address
  • cnt - number of coils to read

Return value - an array of boolean values, that represent the status of coils

readDiscrInps(addr, cnt)

User function for reading discrete inputs (need for Modbus function 0x02)

  • addr - first discrete input address
  • cnt - number of discrete inputs to read

Return value - an array of boolean values, that represent the status of discrete inputs

readHoldRegs(addr, cnt)

User function for reading holding registers (need for Modbus function 0x03)

  • addr - first holding register address
  • cnt - number of holding registers to read

Return value - an array of unsigned integer values, that represent the status of holding registers

readInpRegs(addr, cnt)

User function for reading input registers (need for Modbus function 0x04)

  • addr - first input register address
  • cnt - number of input registers to read

Return value - an array of unsigned integer values, that represent the status of input registers

writeCoils(addr, vals)

User function for writing coils status (need for Modbus functions 0x05 and 0x0f)

  • addr - first coil address
  • vals - an array of boolean values, that represent the status of coils

Returns a boolean value: true if successful or false when attempting to go beyond the range of valid coils addresses

writeRegs(addr, vals)

User function for writing holding registers status (need for Modbus functions 0x06 and 0x10)

  • addr - first register address
  • vals - an array of unsigned integer values, that represent the status of registers

Returns a boolean value: true if successful or false when attempting to go beyond the range of valid holding registers addresses

License

MIT license (see the LICENSE file).