-
Notifications
You must be signed in to change notification settings - Fork 0
/
test2.js
44 lines (25 loc) · 946 Bytes
/
test2.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
const { response } = require("express");
var ModbusRTU = require("modbus-serial");
var client = new ModbusRTU();
client.connectTCP("192.168.5.243", { port: 502 }, () => {
console.log('connected');
client.setID(3);
let writeRegisterId = 1;
let registerId = 1;
let registerSize = 4;
let buf1 = Buffer.alloc(4);
buf1.writeFloatBE(24, 0);
console.log('write data', buf1);
client.writeRegisters(writeRegisterId, buf1).then((response) => {
console.log(response);
}).catch((err) => { console.log(err) });
console.log('read data');
setInterval(function() {
client.readInputRegisters(registerId, registerSize).then((response) => {
let buffer = response.buffer;
console.log('\nREAD =>');
//console.log(response);
console.log(buffer.readFloatBE(0));
});
}, 1000);
});