-
Notifications
You must be signed in to change notification settings - Fork 1
/
node-red-contrib-modbus-in.js
76 lines (68 loc) · 1.48 KB
/
node-red-contrib-modbus-in.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
/**
* node-red-contrib-modbus-serial module
* author: John Majerus
*
*/
function onOpen(err) {
var cmd = this.context().get("cmd");
if (err) {
this.warn("modbus open error");
this.warn(err.stack);
} else {
if (cmd.indexOf("read") != -1)
read();
}
}
function read() {
var cmd = this.context().get("cmd");
// read the 2 registers starting at address
// on device number 1.
// this.warn("read");
function readHoldingRegister(reg) {
this.client.readHoldingRegisters(reg, 1).then(display);
}
switch (cmd) {
case "open1": {
readHoldingRegister(1);
break;
}
case "open2": {
readHoldingRegister(2);
break;
}
case "close1": {
readHoldingRegister(1);
break;
}
case "close2": {
readHoldingRegister(2);
break;
}
case "read1": {
readHoldingRegister(1);
break;
}
case "read2": {
readHoldingRegister(2);
break;
}
}
}
module.exports = function(RED) {
function ModbusInNode(config) {
RED.nodes.createNode(this, config);
// Retrieve the config node
this.master = RED.nodes.getNode(config.master);
if (this.master) {
this.client = this.master.client;
if (!this.client) this.warn("Modbus client unavailable");
this.on('input', function(msg) {
this.context().set('cmd',msg.payload);
this.client.open(onOpen);
});
} else {
this.warn("Configuration node not set")
}
}
RED.nodes.registerType("modbus-in", ModbusInNode);
}