-
Notifications
You must be signed in to change notification settings - Fork 1
/
node-red-contrib-modbus-out.js
88 lines (80 loc) · 2.23 KB
/
node-red-contrib-modbus-out.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
86
87
88
/**
* node-red-contrib-modbus-serial module author: John Majerus input command in
* format: open/close <device#> <relay#>
*/
function onOpen(err) {
var cmd = node.cmd;
function write() {
if (cmd.search("close")) {
address = 0x0100;
} else if (cmd.search("open")) {
address = 0x0200;
}
registers = cmd.match(/\d+/g);
node.master.client.setID(parseInt(registers[0]));
// this.warn(registers[1]);
// write the values 0, 0xffff to registers starting at address
// on device number n.
if (!node.master.serialPort.isOpen()) {
node.log("opening serial port");
node.master.serialPort.open();
}
if (node.master.serialPort.isOpen()) {
node.log("modbus serial port is open");
}
result = node.master.client.writeRegister(parseInt(registers[1]),
address);
node.warn("write " + node.master.port);
}
if (err) {
node.warn("modbus open error");
node.warn(err.stack);
} else {
node.warn(cmd);
if (cmd.indexOf("read") != -1)
read();
else
write();
// setTimeout(serialPort.close(),500);
}
}
module.exports = function(RED) {
function ModbusOutNode(config) {
RED.nodes.createNode(this, config);
// Retrieve the config node
node = this;
node.master = RED.nodes.getNode(config.master);
if (this.master) {
if (!this.master.client)
node.warn("Modbus client unavailable");
if (!node.master.serialPort.isOpen()) {
node.log("modbus serial port connect");
node.master.connect();
}
this.on('input', function(msg) {
node.cmd = msg.payload;
node.master.client.open(onOpen);
});
this.on('close', function() {
// tidy up any async code here - shutdown connections and so
// on.
if (node.master.serialPort.isOpen()) {
node.master.serialPort.close();
}
});
node.master.serialPort.on('error', function(err) {
node.warn("serial port error");
node.warn(err.stack);
});
node.master.serialPort.on('open', function() {
node.log("serial port open");
});
node.master.serialPort.on('close', function() {
node.log("serial port close");
});
} else {
node.warn("Configuration node not set");
}
}
RED.nodes.registerType("modbus-out", ModbusOutNode);
}