A simple an easy to use Modbus TCP client/server implementation.
Modbus
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');
var client = modbus.client.tcp.complete({
'host' : host,
'port' : port,
'autoReconnect' : true,
'reconnectTimeout' : 1000,
'timeout' : 5000,
'unitId' : 0
});
client.connect();
client.on('connect', function () {
client.readCoils(0, 13).then(function (resp) {
console.log(resp);
}, console.error);
client.readDiscreteInputs(0, 13).then(function (resp) {
console.log(resp);
}, console.error);
client.readHoldingRegisters(0, 10).then(function (resp) {
console.log(resp);
}, console.error);
client.readInputRegisters(0, 10).then(function (resp) {
console.log(resp);
}, console.error);
client.writeSingleCoil(5, true).then(function (resp) {
console.log(resp);
}, console.error);
client.writeSingleCoil(5, Buffer.from([0x01])).then(function (resp) {
console.log(resp);
}, console.error);
client.writeSingleRegister(13, 42).then(function (resp) {
console.log(resp);
}, console.error);
client.writeSingleRegister(13, Buffer.from([0x00 0x2A])).then(function (resp) {
console.log(resp);
}, console.error);
client.writeMultipleCoils(3, [1, 0, 1, 0, 1, 1]).then(function (resp) {
console.log(resp);
}, console.error);
client.writeMultipleCoils(3, Buffer.from([0x2B]), 6).then(function (resp) {
console.log(resp);
}, console.error);
client.writeMultipleRegisters(4, [1, 2, 3, 4]).then(function (resp) {
console.log(resp);
}, console.error);
client.writeMultipleRegisters(4, Buffer.from([0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04]).then(function (resp) {
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,
coils : new Buffer(1024),
holding : new Buffer(1024),
input : new Buffer(1024)
})
.compose(modbus.server.tcp.complete)
.init(function () {
var init = function () {
this.on('readCoilsRequest', function (start, quantity) {
});
this.on('[pre][post]WriteSingleCoilRequest', function (address, value) {
});
}.bind(this);
init();
});
customServer();
var server = modbus.server.tcp.complete({ port : 8888 });
server.getHolding().writeUInt16BE(123, 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.