Giter Club home page Giter Club logo

node-modbus-stack's Introduction

node-modbus-stack

A StreamStack implementation of the MODBUS protocol for Node.

This module exposes two concrete StreamStack implementations: ModbusRequestStack can be used as a MODBUS client (i.e. Master), and can write MODBUS compliant requests and listen for the response. ModbusResponseStack can be used to create a MODBUS server (i.e. Slave), by listening for requests and providing a convenient API to respond with.

MODBUS is an open building automation protocol that is widely used in various monitoring and controlling equipment. It's used with a variety of different transports, including TCP.

Currently only communication through the TCP protocol is supported, however RS-485 serial support should be possible with node-serialport. I haven't had a chance to look into it yet.

A MODBUS Master (Client)

You will need to know which Function Code (defined in the MODBUS specification) you are invoking on the remote MODBUS slave. In this example, we'll request to read from the current values of the first 50 Input Registers on the slave:

// 'RIR' contains the "Function Code" that we are going to invoke on the remote device
var RIR = require('modbus-stack').FUNCTION_CODES.READ_INPUT_REGISTERS;

// IP and port of the MODBUS slave, default port is 502
var client = require('modbus-stack/client').createClient(502, '10.0.1.50');

// 'req' is an instance of the low-level `ModbusRequestStack` class
var req = client.request(RIR, // Function Code: 4
                         0,    // Start at address 0
                         50);  // Read 50 contiguous registers from 0

// 'response' is emitted after the entire contents of the response has been received.
req.on('response', function(registers) {
  // An Array of length 50 filled with Numbers of the current registers.
  console.log(registers);
  client.end();
});

A MODBUS Slave (Server)

node-modbus-stack makes it dead simple to create a compliant MODBUS Slave (or Server) written in pure JavaScript. Here's an example of a server that would respond to the request above:

var FC = require('modbus-stack').FUNCTION_CODES;

// 'handlers' is an Object with keys containing the "Function Codes" that your MODBUS
// server will handle. Anything function code requested without a handler defined here
// will have the Server transparently respond with Exception Code 1 ("Illegal Function")
var handlers = {};

// Define a handler for "Read Input Registers". We'll just respond with the register
// number requested. In a real-world situation, you'd probably look up these values from
// a database, etc.
handlers[FC.READ_INPUT_REGISTERS] = function(request, response) {
  var start = request.startAddress;
  var length = request.quantity;
  
  var resp = new Array(length);
  for (var i=0; i<length; i++) {
    resp[i] = start + i;
  }
  response.writeResponse(resp);
}

require('modbus-stack/server').createServer(handlers).listen(502);

A "catch-all" function can be passed to createServer() instead of a "handlers" object, if you'd rather have a single callback invoked for all MODBUS requests. Just be sure to call writeException() manually for any "Function Codes" your server isn't going to handle.

node-modbus-stack's People

Contributors

inindev avatar tootallnate avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-modbus-stack's Issues

sample response for write register

Hi,
I would like to ask, if possible to provide example for a server response on WRITE_SINGLE_REGISTER.
Getting the address and value is not a problem and works very well.
Where I am stuck is with response.writeResponse()

The response supposed to be same as request.

So, what I am currently doing is responding with the exception, but that is far from ideal.
I know this is more request for broaden up the example documentation for people not well skilled in programming.

App file snippet:
sample_server.js

handlers[FC.WRITE_SINGLE_REGISTER] = function(request, response) {
  var address = request.address;
  var value = request.value;

    console.log('Data recieved: add: ' + address + ' value: ' + value );
// I get correct data here as expected

// respond with exception as 
// response.writeResponse() 
// not done correctly in server.js
   response.writeException(5);
}

Snippet from server.js

Server.RESPONSES = {
  // READ_Holding_REGISTERS
  3: function(registers) {
    if (!Array.isArray(registers) || registers.length != this.request.quantity) {
      throw new Error('Expected to write an "Array" of length "'+this.request.quantity+'"');
    }
    var i=0, l=registers.length, put = Put()
      .word8(registers.length*2);
    for (; i<l; i++) {
      put.word16be(registers[i]);
    }
    return put.buffer();
  },
  // READ_INPUT_REGISTERS
  4: function(registers) {
    if (!Array.isArray(registers) || registers.length != this.request.quantity) {
      throw new Error('Expected to write an "Array" of length "'+this.request.quantity+'"');
    }
    var i=0, l=registers.length, put = Put()
      .word8(registers.length*2);
    for (; i<l; i++) {
      put.word16be(registers[i]);
    }
    return put.buffer();
  },
  // // WRITE_SINGLE_REGISTER
  6: function(registers) {
     if (!Array.isArray(registers || registers.length != this.request.quantity) ) {
      throw new Error('Expected to write an "Array" of length "'+this.request.quantity+'"');
    }
    var i=0, l=registers.length, put = Put()
      .word8(registers.length*2);
    for (; i<l; i++) {
      put.word16be(registers[i]);
    }
    return put.buffer();
  }
};

thank you for your help

consecutive change on same address not reflecting in node slave client

Hi,
I face issue below,we are using #4
READ_INPUT_REGISTERS slave client
Server is modbus hardware.

Step1 - read register x having address M.we got value a.
We are able to received value a at register x when app started initially

Step 2- make app running .

Step 3 - now change value at register x from a to b. Modbus hardware had new value b written.
But node app not reflecting responce event in realtime.

We are not able to received changes instantly on same register from master.

If I restart a app. Than only client slave app get fresh value.it does not reflect data in responce once it changed.

Please help.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.