GithubHelp home page GithubHelp logo

klmurphy72 / node-modbus Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cloud-automation/node-modbus

0.0 2.0 0.0 222 KB

Modbus TCP Client/Server implementation for Node.JS

Vim Script 0.55% JavaScript 99.45%

node-modbus's Introduction

A simple an easy to use Modbus TCP client/server implementation.

JavaScript Style Guide

Modbus Build Status

Modbus is a simple Modbus TCP Client with a simple API.

Installation

Just type npm install jsmodbus and you are ready to go.

Testing

The test files are implemented using mocha and sinon.

Simply npm install -g mocha and npm install -g sinon. To run the tests type from the projects root folder mocha test/*.

Please feel free to fork and add your own tests.

TCP Client example

var modbus = require('jsmodbus');

// create a modbus client
var client = modbus.client.tcp.complete({ 
        'host'              : host, 
        'port'              : port,
        'autoReconnect'     : true,
        'reconnectTimeout'  : 1000,
        'timeout'           : 5000,
        'unitId'            : 0
    });

client.connect();

// reconnect with client.reconnect()

client.on('connect', function () {

    // make some calls

    client.readCoils(0, 13).then(function (resp) {

        // resp will look like { fc: 1, byteCount: 20, coils: [ values 0 - 13 ], payload: <Buffer> } 
        console.log(resp);

    }, console.error);

    client.readDiscreteInputs(0, 13).then(function (resp) {

        // resp will look like { fc: 2, byteCount: 20, coils: [ values 0 - 13 ], payload: <Buffer> } 
        console.log(resp);

    }, console.error);

    client.readHoldingRegisters(0, 10).then(function (resp) {

        // resp will look like { fc: 3, byteCount: 20, register: [ values 0 - 10 ], payload: <Buffer> }
        console.log(resp); 

    }, console.error);

    client.readInputRegisters(0, 10).then(function (resp) {

	    // resp will look like { fc: 4, byteCount: 20, register: [ values 0 - 10 ], payload: <Buffer> }
	    console.log(resp);

    }, console.error);

    client.writeSingleCoil(5, true).then(function (resp) {

	    // resp will look like { fc: 5, byteCount: 4, outputAddress: 5, outputValue: true }
	    console.log(resp);

    }, console.error);

    client.writeSingleCoil(5, Buffer.from([0x01])).then(function (resp) {

	    // resp will look like { fc: 5, byteCount: 4, outputAddress: 5, outputValue: true }
	    console.log(resp);

    }, console.error);

    client.writeSingleRegister(13, 42).then(function (resp) {

	    // resp will look like { fc: 6, byteCount: 4, registerAddress: 13, registerValue: 42 }
	    console.log(resp);

    }, console.error);

    client.writeSingleRegister(13, Buffer.from([0x00 0x2A])).then(function (resp) {

	    // resp will look like { fc: 6, byteCount: 4, registerAddress: 13, registerValue: 42 }
	    console.log(resp);

    }, console.error);

    client.writeMultipleCoils(3, [1, 0, 1, 0, 1, 1]).then(function (resp) {

        // resp will look like { fc: 15, startAddress: 3, quantity: 6 }
        console.log(resp); 

    }, console.error);

    client.writeMultipleCoils(3, Buffer.from([0x2B]), 6).then(function (resp) {

        // resp will look like { fc: 15, startAddress: 3, quantity: 6 }
        console.log(resp); 

    }, console.error);

    client.writeMultipleRegisters(4, [1, 2, 3, 4]).then(function (resp) {
        
        // resp will look like { fc : 16, startAddress: 4, quantity: 4 }
        console.log(resp);
        
    }, console.error);

    client.writeMultipleRegisters(4, Buffer.from([0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04]).then(function (resp) {
        
        // resp will look like { fc : 16, startAddress: 4, quantity: 4 }
        console.log(resp);
        
    }, console.error);

});

client.on('error', function (err) {

    console.log(err);
    
})

Server example

    
    var stampit = require('stampit'),
        modbus = require('jsmodbus');

    var customServer = stampit()
        .refs({
            'logEnabled'        : true,
            'port'              : 8888,
            'responseDelay'     : 10, // so we do not fry anything when someone is polling this server
            'whiteListIPs'      : null, // filter connection only from these IPs (ex. ['127.0.0.1', '192.168.0.1'])

            // specify coils, holding and input register here as buffer or leave it for them to be new Buffer(1024)
            coils               : Buffer.alloc(1024, 0),
            holding             : Buffer.alloc(1024, 0),
            input               : Buffer.alloc(1024, 0)
        })
        .compose(modbus.server.tcp.complete)
        .init(function () {
        
            var init = function () {

                // get the coils with this.getCoils() [ Buffer(1024) ]
                // get the holding register with this.getHolding() [ Buffer(1024) ]
                // get the input register with this.getInput() [ Buffer(1024) ]                
              
                // listen to requests 

                this.on('readCoilsRequest', function (start, quantity) {
                
                    // do something, this will be executed in sync before the 
                    // read coils request is executed 
                    
                });

                // the write request have pre and post listener
                this.on('[pre][post]WriteSingleCoilRequest', function (address, value) {
                    
                    
                });

            }.bind(this);    
            
            
            init();
            
        });

    customServer();

    // you can of course always use a standard server like so

    var server = modbus.server.tcp.complete({ port : 8888 });

    // and interact with the register via the getCoils(), getHolding() and getInput() calls

    server.getHolding().writeUInt16BE(123, 1);

    // you can filter only certain IP addresses to connect

    var server = modbus.server.tcp.complete({ port : 8888, whiteListIPs: ['127.0.0.1', '192.168.0.1'] });

License

Copyright (C) 2016 Stefan Poeter (Stefan.Poeter[at]cloud-automation.de)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

node-modbus's People

Contributors

stefanpoeter avatar psorowka avatar germanbluefox avatar joroaf avatar superjojo2001 avatar biancode avatar jayharper avatar michikuehne avatar bzhnja avatar cdynak avatar timelesshaze avatar

Watchers

James Cloos avatar  avatar

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.