Permalink
Browse files

Some support for more REQUESTS for both 'client' and 'server'.

  • Loading branch information...
1 parent 3bd2541 commit 9202aa84502a38424faa826d35ca72da0ca0e30d @TooTallNate committed Dec 31, 2010
Showing with 55 additions and 10 deletions.
  1. +18 −5 lib/client.js
  2. +37 −5 lib/server.js
View
@@ -4,13 +4,26 @@ var Binary = require('bufferlist/binary').Binary;
var modbus = require('./modbus-stack');
var FUNCTION_CODES = modbus.FUNCTION_CODES;
-exports.REQUESTS = {
- // READ_INPUT_REGISTERS
- 4: function(startAddress, quantity) {
+function putTwoWord16be(first, second) {
return Put()
- .word16be(startAddress)
- .word16be(quantity)
+ .word16be(first)
+ .word16be(second)
.buffer();
+}
+
+exports.REQUESTS = {
+ // READ_COILS
+ 1: putTwoWord16be,
+ // READ_DISCRETE_INPUTS
+ 2: putTwoWord16be,
+ // READ_HOLDING_REGISTERS
+ 3: putTwoWord16be,
+ // READ_INPUT_REGISTERS
+ 4: putTwoWord16be,
+ // WRITE_SINGLE_REGISTER:
+ 5: function(address, value) {
+ if (typeof value !== 'boolean') throw new Error('"Write Single Coil" expects a \'boolean\' value');
+ return putTwoWord16be(address, value ? 0xff00 : 0x0000);
}
};
View
@@ -4,14 +4,46 @@ var Binary = require('bufferlist/binary').Binary;
var modbus = require('./modbus-stack');
var FUNCTION_CODES = modbus.FUNCTION_CODES;
-exports.REQUESTS = {
- // READ_INPUT_REGISTERS
- 4: function(bufferlist) {
+// The pattern of 'startAddress' and 'quantity' is used by a lot of
+// MODBUS functions, and can be re-used in the code.
+function parseTwoWord16be(first, second) {
+ return function(bufferlist) {
return Binary(bufferlist)
- .getWord16be('startAddress')
- .getWord16be('quantity')
+ .getWord16be(first)
+ .getWord16be(second)
.end().vars;
}
+}
+
+var no_parameters = function() { return {}; }
+var startAddress_quantity = parseTwoWord16be('startAddress', 'quantity');
+var address_value = parseTwoWord16be('address', 'value');
+
+exports.REQUESTS = {
+ // READ_COILS
+ 1: startAddress_quantity,
+ // READ_DISCRETE_INPUTS
+ 2: startAddress_quantity,
+ // READ_HOLDING_REGISTERS
+ 3: startAddress_quantity,
+ // READ_INPUT_REGISTERS
+ 4: startAddress_quantity,
+ // WRITE_SINGLE_COIL
+ 5: function(bufferlist) {
+ var rtn = address_value.call(this, bufferlist);
+ rtn.value = rtn.value === 0xff00;
+ return rtn;
+ },
+ // WRITE_SINGLE_REGISTER
+ 6: address_value,
+ // READ_EXCEPTION_STATUS (Serial Line Only)
+ 7: no_parameters,
+ // GET_COMM_EVENT_COUNTER (Serial Line Only)
+ 11: no_parameters,
+ // GET_COMM_EVENT_LOG (Serial Line Only)
+ 12: no_parameters,
+ // REPORT_SLAVE_ID (Serial Line Only)
+ 17: no_parameters
};
exports.RESPONSES = {

0 comments on commit 9202aa8

Please sign in to comment.