-
Notifications
You must be signed in to change notification settings - Fork 2
/
modbustcp.js
54 lines (43 loc) · 1.19 KB
/
modbustcp.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
'use strict';
var net = require('net');
var modbus = require('modbus-tcp');
console.log('connecting...');
var socket = net.connect({
host: '192.168.1.7',
port: 502
}, function() {
console.log('connected');
function ReadAnaloge() {
modbusClient.readInputRegisters(1, 0, 4, function(error, buffers) {
if (error) {
console.log('error reading register:', error.stack);
return;
}
var value = buffers && buffers[0] && buffers[0].readUInt16BE(0);
var value2 = buffers && buffers[1] && buffers[1].readUInt16BE(0);
//console.log('value:', buffers[0].readUInt16BE(0));
var buff = [];
for (var i = 0; i < buffers.length; i++) {
buff[i]=buffers[i].readUInt16BE(0);
}
console.log(buff);
});
}
function ReadDigital() {
modbusClient.readCoils(1, 0, 4, function(error, coils) {
if (error) {
console.log('error reading coils:', error.stack);
return;
}
console.log('value:', coils);
});
}
setInterval(ReadAnaloge, 100);
setInterval(ReadDigital, 100);
});
socket.on('error', function(error) {
console.log('connection failed:', error.stack);
});
var modbusClient = new modbus.Client();
modbusClient.writer().pipe(socket);
socket.pipe(modbusClient.reader());