-
-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathsimple_enron.js
executable file
·69 lines (59 loc) · 1.55 KB
/
simple_enron.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
/* eslint-disable no-console, spaced-comment */
const ModbusRTU = require("../index");
const client = new ModbusRTU();
const networkErrors = ["ESOCKETTIMEDOUT", "ETIMEDOUT", "ECONNRESET", "ECONNREFUSED", "EHOSTUNREACH"];
function write() {
console.log("write");
// write the values 1234567890 to register 5001
client
.writeRegisterEnron(5001, 0x499602d2)
.then(read)
.catch(function(e) {
console.log(e.message);
});
}
function read() {
console.log("read");
// read the 2 registers starting at address 5001
// on device number 1.
client.readRegistersEnron(5001, 2)
.then(console.log)
.catch(function(e) {
console.log(e.message);
})
.then(close);
}
client.connectTCP(
"127.0.0.1",
{
port: 8502,
enron: true,
enronTables: {
booleanRange: [1001, 1999],
shortRange: [3001, 3999],
longRange: [5001, 5999],
floatRange: [7001, 7999]
}
})
.then(setClient)
.then(function() {
console.log("Connected"); })
.catch(function(e) {
if(e.errno) {
if(networkErrors.includes(e.errno)) {
console.log("we have to reconnect");
}
}
console.log(e.message);
});
function setClient() {
// set the client's unit id
// set a timout for requests default is null (no timeout)
client.setID(1);
client.setTimeout(1000);
// run program
write();
}
function close() {
client.close();
}