-
-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathlogger_complete.js
executable file
·131 lines (111 loc) · 3.27 KB
/
logger_complete.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* eslint-disable no-console, spaced-comment */
// create an empty modbus client
//let ModbusRTU = require("modbus-serial");
const ModbusRTU = require("../index");
let client = new ModbusRTU();
let timeoutRunRefCoils = null;
let timeoutRunRefDiscreteInputs = null;
let timeoutRunRefInputs = null;
let timeoutRunRefHoldings = null;
let timeoutConnectRef = null;
const networkErrors = [
"ESOCKETTIMEDOUT",
"ETIMEDOUT",
"ECONNRESET",
"ECONNREFUSED",
"EHOSTUNREACH",
"ENETRESET",
"ECONNABORTED",
"ENETUNREACH",
"ENOTCONN",
"ESHUTDOWN",
"EHOSTDOWN",
"ENETDOWN",
"EWOULDBLOCK",
"EAGAIN"
];
// check error, and reconnect if needed
function checkError(e) {
if(e.errno && networkErrors.includes(e.errno)) {
console.log("we have to reconnect");
// close port
client.close();
// re open client
client = new ModbusRTU();
timeoutConnectRef = setTimeout(connect, 1000);
}
}
// open connection to a serial port
function connect() {
// clear pending timeouts
clearTimeout(timeoutConnectRef);
// if client already open, just run
if (client.isOpen) {
run();
}
// if client closed, open a new connection
client.connectTCP("127.0.0.1", { port: 8502 })
.then(setClient)
.then(function() {
console.log("Connected"); })
.catch(function(e) {
checkError(e);
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(3000);
// run program
run();
}
function run() {
// clear pending timeouts
clearTimeout(timeoutRunRefCoils);
client.readCoils(1, 5)
.then(function(d) {
console.log("Receive Coils:", d.data); })
.then(function() {
timeoutRunRefCoils = setTimeout(readDiscreteInputs, 1000);
})
.catch(function(e) {
checkError(e);
console.log(e.message); });
}
function readDiscreteInputs() {
clearTimeout(timeoutRunRefDiscreteInputs);
client.readDiscreteInputs(10001, 5)
.then(function(d) {
console.log("Receive Discrete Inputs:", d.data); })
.then(function() {
timeoutRunRefDiscreteInputs = setTimeout(readInputRegisters, 1000); })
.catch(function(e) {
checkError(e);
console.log(e.message); });
}
function readInputRegisters() {
clearTimeout(timeoutRunRefInputs);
client.readInputRegisters(1, 5)
.then(function(d) {
console.log("Receive Inputs:", d.data); })
.then(function() {
timeoutRunRefInputs = setTimeout(readHoldingRegisters, 1000); })
.catch(function(e) {
checkError(e);
console.log(e.message); });
}
function readHoldingRegisters() {
clearTimeout(timeoutRunRefHoldings);
client.readHoldingRegisters(10001, 5)
.then(function(d) {
console.log("Receive Holding Registers:", d.data); })
.then(function() {
timeoutRunRefHoldings = setTimeout(run, 1000); })
.catch(function(e) {
checkError(e);
console.log(e.message);
});
}
// connect and start logging
connect();