Permalink
Browse files

Implemented the `Client` class, with a couple examples. Still need te…

…st cases.
  • Loading branch information...
1 parent 1d77f7e commit ff1392385832a4a5804c340dbd39ea602848339b @TooTallNate committed Jan 5, 2011
Showing with 86 additions and 31 deletions.
  1. +11 −0 examples/readCoils.js
  2. +30 −0 examples/readInputRegisters-raw.js
  3. +6 −27 examples/readInputRegisters.js
  4. +32 −2 lib/client.js
  5. +7 −2 lib/server.js
View
11 examples/readCoils.js
@@ -0,0 +1,11 @@
+var FC = require('modbus-stack').FUNCTION_CODES;
+
+var client = require('modbus-stack/client').createClient(502);
+
+var req = client.request(FC.READ_COILS, 50, 3);
+
+req.on('response', function(err, response) {
+ if (err) throw err;
+ console.log(response);
+ client.end();
+});
View
30 examples/readInputRegisters-raw.js
@@ -0,0 +1,30 @@
+require('colors');
+var FUNCTION_CODES = require('modbus-stack').FUNCTION_CODES;
+var ModbusRequestStack = require('modbus-stack').ModbusRequestStack;
+
+var conn = require('net').createConnection(502, '10.1.10.134');
+
+// A 'client' instance is good for 1 single request/response transaction.
+var client = new ModbusRequestStack(conn);
+
+// We're gonna call for the "Read Input Registers" function on the remote
+// MODBUS device, requesting to read the current values of the first 4 registers.
+var fc = FUNCTION_CODES.READ_INPUT_REGISTERS;
+var startAddress = 0;
+var numToRead = 4;
+client.request(fc, startAddress, numToRead, function(err, response) {
+ if (err) {
+ throw err;
+ }
+
+ response.forEach(function(register, i) {
+ console.log(
+ ("Sensor " + String(startAddress + i).bold + ":\t").blue +
+ (String(register/10).bold + '\u00B0F').green
+ );
+ });
+
+ // Close the connection
+ conn.end();
+});
+
View
33 examples/readInputRegisters.js
@@ -1,30 +1,9 @@
-require('colors');
-var FUNCTION_CODES = require('modbus-stack').FUNCTION_CODES;
-var ModbusRequestStack = require('modbus-stack').ModbusRequestStack;
+var FC = require('modbus-stack').FUNCTION_CODES;
-var conn = require('net').createConnection(502, '10.1.10.134');
+var client = require('modbus-stack/client').createClient(502);
-// A 'client' instance is good for 1 single request/response transaction.
-var client = new ModbusRequestStack(conn);
-
-// We're gonna call for the "Read Input Registers" function on the remote
-// MODBUS device, requesting to read the current values of the first 4 registers.
-var fc = FUNCTION_CODES.READ_INPUT_REGISTERS;
-var startAddress = 0;
-var numToRead = 4;
-client.request(fc, startAddress, numToRead, function(err, response) {
- if (err) {
- throw err;
- }
-
- response.forEach(function(register, i) {
- console.log(
- ("Sensor " + String(startAddress + i).bold + ":\t").blue +
- (String(register/10).bold + '\u00B0F').green
- );
- });
-
- // Close the connection
- conn.end();
+client.request(FC.READ_INPUT_REGISTERS, 0, 5, function(err, response) {
+ if (err) throw err;
+ console.log(response);
+ client.end();
});
-
View
34 lib/client.js
@@ -2,16 +2,46 @@ var Put = require('put');
var BufferList = require('bufferlist').BufferList;
var Binary = require('bufferlist/binary').Binary;
var modbus = require('./modbus-stack');
+var netStream = require('net').Stream;
var FUNCTION_CODES = modbus.FUNCTION_CODES;
+/* TCP MODBUS Client interface, as it's the most usual use-case. */
+function Client () {
+ if (!(this instanceof Client)) return new Client();
+ netStream.call(this, {});
+}
+require('util').inherits(Client, netStream);
+module.exports = Client;
+
+// Causes the client instance to make a MODBUS request to the remote host.
+// This is done by creating a new ModbusRequetStack instance on `this`.
+// TODO: Either pipelining or throw an error if more than one
+// instance is active at a time.
+Client.prototype.request = function() {
+ var req = new modbus.ModbusRequestStack(this);
+ req.request.apply(req, arguments);
+ return req;
+}
+
+// Convenience function to create a new Client instance and have it
+// `connect()` to the specified remote address.
+Client.createClient = function(port, host) {
+ var s = new Client();
+ s.connect(port, host);
+ return s;
+}
+
+
+
+// This pattern is (re)used by a lot of the basic MODBUS commands.
function putTwoWord16be(first, second) {
return Put()
.word16be(first)
.word16be(second)
.buffer();
}
-exports.REQUESTS = {
+Client.REQUESTS = {
// READ_COILS
1: putTwoWord16be,
// READ_DISCRETE_INPUTS
@@ -27,7 +57,7 @@ exports.REQUESTS = {
}
};
-exports.RESPONSES = {
+Client.RESPONSES = {
// READ_INPUT_REGISTERS
4: function(bufferlist) {
var rtn = [];
View
9 lib/server.js
@@ -5,15 +5,19 @@ var Binary = require('bufferlist/binary').Binary;
var modbus = require('./modbus-stack');
var FUNCTION_CODES = modbus.FUNCTION_CODES;
-
+/* Streamlined TCP MODBUS server class. Can be used to respond to MODBUS requests
+ * from TCP clients. `handlers` can be a function which is invoked for every
+ * "request" event, or an Object with keys being the Function Codes your server
+ * is going to handle.
+ */
function Server (handlers) {
+ net.Server.call(this, this._setupConn);
if (typeof handlers == 'function') {
this.on('request', handlers);
} else {
this.on('request', this._handler);
this.handlers = handlers || {};
}
- net.Server.call(this, this._setupConn);
}
require('util').inherits(Server, net.Server);
module.exports = Server;
@@ -38,6 +42,7 @@ Server.prototype._handler = function(request, response) {
}
}
+// Convenience function to create a MODBUS Server.
function createServer (handlers) {
return new Server(handlers);
}

0 comments on commit ff13923

Please sign in to comment.