-
Notifications
You must be signed in to change notification settings - Fork 37
/
client.js
85 lines (77 loc) · 2.39 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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();
}
Client.REQUESTS = {
// READ_COILS
1: putTwoWord16be,
// READ_DISCRETE_INPUTS
2: putTwoWord16be,
// READ_HOLDING_REGISTERS
3: putTwoWord16be,
// READ_INPUT_REGISTERS
4: putTwoWord16be,
// WRITE_SINGLE_COIL
5: function(address, value) {
if (typeof value !== 'boolean') throw new Error('"Write Single Coil" expects a \'boolean\' value');
return putTwoWord16be(address, value ? 0xff00 : 0x0000);
}
};
Client.RESPONSES = {
// READ_INPUT_REGISTERS
4: function(bufferlist) {
var rtn = [];
var binary = Binary(bufferlist)
.getWord8('byteLength').end();
rtn.byteLength = binary.vars.byteLength;
for (var i=0, l=binary.vars.byteLength/2; i<l; i++) {
binary.getWord16be("val");
rtn.push(binary.end().vars.val);
}
return rtn;
},
// WRITE_SINGLE_COIL
5: function(bufferlist) {
var rtn = [];
var binary = Binary(bufferlist)
.getWord8('byteLength').end();
rtn.byteLength = binary.vars.byteLength;
for (var i=0, l=binary.vars.byteLength/2; i<l; i++) {
binary.getWord16be("val");
rtn.push(binary.end().vars.val);
}
return rtn;
},
};