Socket
Socket
Sign inDemoInstall

jsmodbus

Package Overview
Dependencies
Maintainers
1
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsmodbus - npm Package Compare versions

Comparing version 0.5.2 to 1.0.0

examples/serial/ListSerialInterfaces.js

11

package.json
{
"name": "jsmodbus",
"version": "0.5.2",
"version": "1.0.0",
"description": "Implementation for the Serial/TCP Modbus protocol.",
"author": "Stefan Poeter <stefan.poeter@cloud-automation.de>",
"main": "./src/jsModbus.js",
"main": "./src/modbus.js",
"repository": {

@@ -13,3 +13,8 @@ "type": "git",

"put": "0.0.6",
"q" : "1.0.1"
"q": "1.0.1",
"serialport": "^2.0.6",
"stampit": "^2.1.1",
"stampit-event-bus": "^0.1.1",
"stampit-log": "^0.1.0",
"stampit-state-machine": "^0.2.1"
},

@@ -16,0 +21,0 @@ "devDependencies": {

@@ -22,3 +22,3 @@ A simple an easy to use Modbus TCP client/server implementation.

Client example
TCP Client example
--------------

@@ -29,50 +29,81 @@ ```javascript

// create a modbus client
var client = modbus.createTCPClient(8888, '127.0.0.1', function (err) {
if (err) {
console.log(err);
process.exit(0);
}
});
var client = modbus.client.tcp.complete({
'host' : host,
'port' : port,
'autoReconnect' : true,
'timeout' : 5000,
'unitId' : 0
});
// make some calls
client.connect();
client.readHoldingRegister(0, 10, function (resp, err) {
// resp will look like { fc: 3, byteCount: 20, register: [ values 0 - 10 ] }
console.log(err, resp);
});
// reconnect with client.reconnect()
client.readCoils(0, 13, function (resp, err) {
// resp will look like { fc: 1, byteCount: 20, coils: [ values 0 - 13 ] }
console.log(err, resp);
});
client.on('connect', function () {
client.readDiscreteInput(0, 13, function (resp, err) {
// resp will look like { fc: 2, byteCount: 20, coils: [ values 0 - 13 ] }
console.log(err, resp);
});
// make some calls
client.readInputRegister(0, 10, function (resp, err) {
// resp will look like { fc: 4, byteCount: 20, register: [ values 0 - 10 ] }
console.log(err, resp);
});
client.readCoils(0, 13).then(function (resp) {
client.readCoils(5, 3, function (resp, err) {
// resp will look like { fc: 1, byteCount: 1, register: [ true, false, true ] }
console.log(err, resp);
});
// resp will look like { fc: 1, byteCount: 20, coils: [ values 0 - 13 ] }
console.log(resp);
client.writeSingleCoil(5, true, function (resp, err) {
// resp will look like { fc: 5, byteCount: 4, outputAddress: 5, outputValue: true }
console.log(err, resp);
});
}).fail(console.log);
client.writeSingleRegister(13, 42, function (resp, err) {
// resp will look like { fc: 6, byteCount: 4, registerAddress: 13, registerValue: 42 }
console.log(err, resp);
client.readDiscreteInput(0, 13).then(function (resp) {
// resp will look like { fc: 2, byteCount: 20, coils: [ values 0 - 13 ] }
console.log(resp);
}).fail(console.log);
client.readHoldingRegisters(0, 10).then(function (resp) {
// resp will look like { fc: 3, byteCount: 20, register: [ values 0 - 10 ] }
console.log(resp);
}).fail(console.log);
client.readInputRegister(0, 10).then(function (resp) {
// resp will look like { fc: 4, byteCount: 20, register: [ values 0 - 10 ] }
console.log(resp);
}).fail(console.log);
client.writeSingleCoil(5, true).then(function (resp) {
// resp will look like { fc: 5, byteCount: 4, outputAddress: 5, outputValue: true }
console.log(resp);
}).fail(console.log);
client.writeSingleRegister(13, 42).then(function (resp) {
// resp will look like { fc: 6, byteCount: 4, registerAddress: 13, registerValue: 42 }
console.log(resp);
}).fail(console.log);
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);
}).fail(console.log);
client.writeMultipleRegisters(4, [1, 2, 3, 4]).then(function (resp) {
// resp will look like { fc : 16, startAddress: 4, quantity: 4 }
console.log(resp);
}).fail(console.log);
});
client.writeMultipleCoils(3, [1, 0, 1, 0, 1, 1], function (resp, err) {
// resp will look like { fc: 15, startAddress: 3, quantity: 6 }
console.log(err, resp);
});
client.on('error', function (err) {
console.log(err);
})
```

@@ -83,46 +114,62 @@

```javascript
var modbus = require('jsmodbus');
var stampit = require('stampit'),
modbus = require('jsmodbus');
// create readInputRegister handler
var rirHandler = function (start, quantity) {
var resp = [];
for (var i = start; i < start + quantity; i += 1) {
resp.push(i);
}
var customServer = stampit()
.refs({
'logEnabled' : true,
'port' : 8888,
'responseDelay' : 10, // so we do not fry anything when someone is polling this server
return [resp];
};
// specify coils, holding and input register here as buffer or leave it for them to be new Buffer(1024)
coils : new Buffer(1024),
holding : new Buffer(1024),
input : new Buffer(1024)
})
.compose(modbus.server.tcp.complete)
.init(function () {
var init = function () {
var coil = false;
var writeCoilHandler = function (addr, value) {
// 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
if (addr === 0) {
coil = value;
}
this.on('readCoilsRequest', function (start, quantity) {
// do something, this will be executed in sync before the
// read coils request is executed
});
return [addr, value];
// the write request have pre and post listener
this.on('[pre][post]WriteSingleCoilRequest', function (address, value) {
});
};
}.bind(this);
init();
});
// create Modbus TCP Server
modbus.createTCPServer(8888, '127.0.0.1', function (err, modbusServer) {
// addHandler
modbusServer.addHandler(4, rirHandler);
modbusServer.addHandler(5, writeCoilHandler);
});
```
customServer();
Development
-----------
// you can of course always use a standard server like so
To add other function codes on the client side see the test/serialClient.test.js and add a new test. To implement the test create an api call in src/serialClient.js and implement the pdu handler for the client in src/handler.js. That is mainly all.
var server = modbus.server.tcp.complete({ port : 8888 });
On the server side all you need to do is to implement the handler for the request and the response in the `exports.Server.RequestHandler` and `exports.Server.ResponseHandler`. Don't forget to test!
// and interact with the register via the getCoils(), getHolding() and getInput() calls
That's it for now. Feel free to fork and implement more.
server.getHolding().writeUInt16BE(123, 1);
````
License
-------
## License
Copyright (C) 2014 Stefan Poeter (Stefan.Poeter[at]cloud-automation.de)
Copyright (C) 2016 Stefan Poeter (Stefan.Poeter[at]cloud-automation.de)

@@ -129,0 +176,0 @@ 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:

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc