Git Product home page Git Product logo

node-modbus's Introduction

NPM version Build Status Standard - JavaScript Style Guide NPM download

nodemodbus64

node-modbus

NODE-MODBUS is a simple Modbus TCP/Serial Client/Server API.

Install

Run the following command in the root directory of your Node-RED install

npm install node-modbus

Run the following command for global install

npm install -g node-modbus

Testing

The test files are implemented using mocha and sinon. Simply use npm-update.sh in the source code project. To run the tests type from the projects root folder mocha test/*. Please feel free to fork and add your own tests.

Examples

Server TCP

let node_modbus = require('node-modbus')
let server = node_modbus.server.tcp.complete({ port : 502, responseDelay: 200 })

Client TCP

const node_modbus = require('node-modbus')

const client = node_modbus.client.tcp.complete({
    'host': 'modbus.server.local', /* IP or name of server host */
    'port': 502, /* well known Modbus port */
    'unitId': 1, 
    'timeout': 2000, /* 2 sec */
    'autoReconnect': true, /* reconnect on connection is lost */
    'reconnectTimeout': 15000, /* wait 15 sec if auto reconnect fails to often */
    'logLabel' : 'ModbusClientTCP', /* label to identify in log files */
    'logLevel': 'debug', /* for less log use: info, warn or error */
    'logEnabled': true
})

const time_interval = 1000
client.connect()
client.on('connect', function () {
  setInterval( function () {
     client.readCoils(0, 13).then((response) => console.log(response.payload))
  }, time_interval) /* reading coils every second */
})

Server Serial

TBD

Client Serial

const node_modbus = require('node-modbus')

const client = modbus.client.serial.complete({
    'portName': '/dev/ttyS0', /* COM1 */
    'baudRate': '9600', /* */
    'dataBits': '8', /* 5, 6, 7 */
    'stopBits': '1', /* 1.5, 2 */
    'parity': 'none', /* even, odd, mark, space */
    'connectionType': 'RTU', /* RTU or ASCII */
    'connectionDelay': 250, /* 250 msec - sometimes you need more on windows */
    'timeout': 2000, /* 2 sec */
    'autoReconnect': true, /* reconnect on connection is lost */
    'reconnectTimeout': 15000, /* wait 15 sec if auto reconnect fails to often */
    'logLabel' : 'ModbusClientTCP', /* label to identify in log files */
    'logLevel': 'debug', /* for less log use: info, warn or error */
    'logEnabled': true
})

/* here we need none connect call */

const time_interval = 1000
client.on('connect', function () {
  setInterval( function () {
     client.readCoils(0, 13).then((response) => console.log(response.payload))
  }, time_interval) /* reading coils every second */
})

License

MIT

Based on jsmodbus

node-modbus's People

Contributors

biancode avatar

Watchers

 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.