-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest12.js
More file actions
111 lines (95 loc) · 4.02 KB
/
test12.js
File metadata and controls
111 lines (95 loc) · 4.02 KB
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
console.log('-------------------');
const modbus = require('jsmodbus');
const net = require('net');
const socket = new net.Socket();
const options = {
host: '11.13.11.154',
port: 502,
unitId: 3,
timeout: 26,
autoReconnect: false,
reconnectTimeout: 26,
logLabel: 'huawei Inverter',
logLevel: 'error',
logEnabled: true,
};
const client = new modbus.client.TCP(socket, 1, 5500);
socket.setKeepAlive(false);
socket.connect(options);
socket.on('connect', () => {
// const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
const delay = (function() {
let timer = 0;
return function(callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
};
}());
delay(() => 5000);
console.log('Connected ...');
registers = {
// "inputPower": [32064, 2, 'INT32', "Input Power", 0], // kW 1000
// // rn.GRID_VOLTAGE: U16Register("V", 10, 32066, 1),
// "GRID_VOLTAGE": [32066, 1, 'UINT16', "GRID VOLTAGE", -1],
// // rn.ACCUMULATED_YIELD_ENERGY: U32Register("kWh", 100, 32106, 2),
// "ACCUMULATED_YIELD_ENERGY": [32106, 2, 'UINT32', "ACCUMULATED YIELD ENERGY", -2],
// // rn.DAY_ACTIVE_POWER_PEAK: I32Register("W", 1, 32078, 2),
// // "DAY_ACTIVE_POWER_PEAK": [32078, 2, 'INT32', "DAY_ACTIVE_POWER_PEAK", 0],
// // rn.ACTIVE_POWER: I32Register("W", 1, 32080, 2),
// "ACTIVE_POWER": [32080, 2, 'INT32', "ACTIVE_POWER", 0],
// // rn.GRID_FREQUENCY: U16Register("Hz", 100, 32085, 1),
// // "GRID_FREQUENCY": [32085, 1, 'UINT16', "GRID_FREQUENCY", -2],
// // rn.INTERNAL_TEMPERATURE: I16Register("°C", 10, 32087, 1),
// "INTERNAL_TEMPERATURE": [32087, 1, 'INT16', "INTERNAL_TEMPERATURE", -1],
// // rn.DEVICE_STATUS: U16Register(rv.DEVICE_STATUS_DEFINITIONS, 1, 32089, 1),
// "DEVICE_STATUS": [32089, 1, 'UINT16', "DEVICE_STATUS", 0],
// // rn.DAILY_YIELD_ENERGY: U32Register("kWh", 100, 32114, 2),
// "DAILY_YIELD_ENERGY": [32114, 2, 'UINT32', "DAILY_YIELD_ENERGY", -2],
// rn.MODEL_NAME: StringRegister(30000, 15),
modelName: [30000, 15, 'STRING', 'Model Name', 0],
// rn.MODEL_ID: U16Register(None, 1, 30070, 1),
// "modelId": [30070, 1, 'UINT16', "Model ID", 0],
};
for (const [key, value] of Object.entries(registers)) {
delay(() => 750);
// delay(250);
// console.log(key, value);
// start normale poll
client
.readHoldingRegisters(value[0], value[1])
// client.readHoldingRegisters(value[0],value[1])
.then((resp) => {
// console.log(resp.response._body);
if (value[2] == 'UINT16') {
console.log(`${value[3]}: ${resp.response._body._valuesAsBuffer.readUInt16BE()}`);
} else if (value[2] == 'STRING') {
console.log(`${value[3]}: ${Buffer.from(resp.response._body._valuesAsBuffer, 'hex').toString()}`);
} else if (value[2] == 'INT16' || value[2] == 'SCALE') {
console.log(`${value[3]}: ${resp.response._body._valuesAsBuffer.readInt16BE()}`);
} else if (value[2] == 'UINT32') {
// console.log(value[3] + ": " + resp.response._body._valuesAsBuffer.readUInt32LE());
console.log(`${value[3]}: ${(resp.response._body._valuesAsArray[1] << 16) | resp.response._body._valuesAsArray[0]}`);
console.log(resp.response._body._valuesAsArray[1] << 16);
console.log(resp.response._body._valuesAsArray[0]);
} else if (value[2] == 'INT32') {
// console.log(value[3] + ": " + resp.response._body._valuesAsBuffer.readInt32LE());
console.log(`${value[3]}: ${(resp.response._body._valuesAsArray[1] << 16) | resp.response._body._valuesAsArray[0] | 0}`);
console.log(resp.response._body._valuesAsArray[1] << 16);
console.log(resp.response._body._valuesAsArray[0]);
} else {
console.log(`${key}: type not found ${value[2]}`);
}
})
.catch((err) => {
console.log(err);
});
}
delay(() => {
socket.end();
}, 26000);
});
// avoid all the crash reports
socket.on('error', (err) => {
console.log(err);
socket.end();
});