Comparing version 0.5.2 to 1.0.0
{ | ||
"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": { |
187
README.md
@@ -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: |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
98390
45
1
178
7
2135
6
+ Addedserialport@^2.0.6
+ Addedstampit@^2.1.1
+ Addedstampit-event-bus@^0.1.1
+ Addedstampit-log@^0.1.0
+ Addedstampit-state-machine@^0.2.1
+ Addedbindings@1.2.1(transitive)
+ Addedbluebird@3.7.2(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddefine-properties@1.2.1(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedlodash@3.10.14.17.21(transitive)
+ Addedminimist@0.0.10(transitive)
+ Addednan@2.2.1(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedobject.assign@4.1.5(transitive)
+ Addedoptimist@0.6.1(transitive)
+ Addedserialport@2.1.2(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedsf@0.1.7(transitive)
+ Addedstampit@2.1.2(transitive)
+ Addedstampit-event-bus@0.1.1(transitive)
+ Addedstampit-log@0.1.0(transitive)
+ Addedstampit-state-machine@0.2.1(transitive)
+ Addedsupermixer@1.0.5(transitive)
+ Addedwordwrap@0.0.3(transitive)