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

nodbus-plus

v1.0.0

Published

A nodejs modbus library made in javascript

Downloads

5

Readme

Nodbus-Plus

A Modbus protocol library for Node.js, written entirely in JavaScript. It supports both Modbus Serial and Modbus TCP.

Introduction

Nodbus Plus has been designed as a stack for the Modbus protocol. It can be used to create both your own Modbus client and server using its API, or you can use its build in client or server and have an application with Modbus communication in just minutes.

Installation

$ npm install nodbus-plus

Basic Usage:

Create a modbus server.

const nodbus = require('nodbus-plus');

//Basic config for tcp server. Default values.
const cfg = {
    inputs : 2048, //total inputs
    coils : 2048,  //total coils
    holdingRegisters : 2048, //total holding register
    inputRegisters : 2048,  //total input register
    port : 502,    //port to listen to
}

let server = nodbus.createTcpServer('tcp', cfg);

The first argument for createTcpServer function is the type of transport layer used. The nodbus-plus tcp server suport 'tcp', 'udp4' and 'udp6'. To create a serial server the procedure is the same, but calling createSerialServer function instead.

//Basic config for tcp server. Default values.
let cfg = {
    address : 1,
    transmitionMode: 0, //0-rtu, 1 - ascii
    inputs : 2048,
    coils : 2048,
    holdingRegisters : 2048,
    inputRegisters : 2048,  
    port : 502,    
}

let server = nodbus.createSerialServer('tcp', cfg);

The createSerialServer function can take the value 'serial' as fist argument as well, however the property port mus be a string with the path to a serial port and adicional port configuration property may be needed. See nodbus-plus api for more details.

Add listeners for srever's events

//listenning event
server.on('listening', function(port){
    console.log('Server listening on: ' + port);        
});

//event emited when a request are received
server.on('request', function(sock, req){
    console.log('Request received')
    console.log(req)
});

//Event emited when server send a response to client
server.on('response', function(sock, res){
    console.log('Responding')
    console.log(res)
});

server.on('error', function(err){
    console.log(err)
});

Finally the server must be started.

server.start();

Create a modbus client.

To create a modbus client use the functions createTcpClient or createSerialClient.

const nodbus = require('nodbus-plus');
let client = nodbus.createSerialClient();

//emitted when the client stablish connection with the server
client.on('connection', (id)=>{
    console.log('connection stablish')    
})

//emited when error occurs
client.on('error', (e)=>{    
    console.log(e)    
})

//emitted when a request is sended to server
client.on('request', (id, req)=>{
    console.log('request sended to device: ' + id);        
})

//emited when no response is received
client.on('req-timeout', (id, adu)=>{
    console.log('timeout')        
})

//emited when a response is received
client.on('response', (id, res)=>{
    console.log(res)        
})

Then channels must be add to the client. The client will create a connection per channel. The following example add a modbus serial over tcp server, and conect to it.


//channel
channelCfg = {        
    ip:'127.0.0.1',
    port:502,
    timeout:250,
}

client.addChannel('device', 'tcp1', channelCfg);
client.connect('device')

Once the client is connected, and event listener configured, data can be exchange using availables modbus function.


//reading from cannel 'device', modbus address 1, two coils from 0 coil's address
    client.readCoils('device', 1, 0, 2);

Official Documentation

For comprehensive information on how to use NodbusPlus, refer to the official documentation.

Getting Started

If you're new to NodbusPlus, the Getting Started Guide is a great place to begin. It will walk you through the installation process and provide a basic usage example.

Examples

Inside the root folder of Nodbus plus, there is a directory './samples' that contains three example programs that make use of the library.

Contributing

If you have a suggestion or found a issue, let us known and create an issue

License

This project is licensed under the MIT License - see the LICENSE.md file for details