-
-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathbuffertcp.js
executable file
·42 lines (36 loc) · 968 Bytes
/
buffertcp.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
/* eslint-disable no-console, spaced-comment */
// create an empty modbus client
//let ModbusRTU = require("modbus-serial");
const ModbusRTU = require("../index");
const client = new ModbusRTU();
// NPort Gateway 801D NetPort
client.connectTcpRTUBuffered("127.0.0.1", { port: 8502 })
.then(setClient)
.then(function() {
console.log("Connected");
})
.catch(function(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(2000);
// run program
run();
}
function run() {
// read the 4 registers starting at address 5
client.readDiscreteInputs(0, 22)
.then(function(d) {
console.log("Receive:", d.data);
})
.catch(function(e) {
console.log(e.message);
})
.then(close);
}
function close() {
client.close();
}