-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest6.js
More file actions
274 lines (228 loc) · 10.3 KB
/
test6.js
File metadata and controls
274 lines (228 loc) · 10.3 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
console.log('-------------------');
const modbus = require('jsmodbus');
const net = require('net');
const socket = new net.Socket();
const options = {
host: '192.168.0.44', // '192.168.0.214'
port: 502,
unitId: 1,
timeout: 500,
autoReconnect: true,
reconnectTimeout: 62,
logLabel: 'sun grow Inverter',
logLevel: 'error',
logEnabled: true,
};
const client = new modbus.client.TCP(socket, 1, 1000);
socket.connect(options);
socket.on('connect', () => {
const delay = (function() {
let timer = 0;
return function(callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
};
}());
console.log('Connected ...');
registers = {
devicetypecode: [4999, 1, 'UINT16', 'device type code', 0],
DailyPVGeneration: [13001, 1, 'UINT16', 'Daily PV Generation', -1],
TotalOutputEnergy: [5003, 2, 'UINT32', 'Total Output Energy pv & battery discharge', 0],
TotalPVGeneration: [13002, 2, 'UINT32', 'Total PV Generation', 0],
TotalDCpower: [5016, 2, 'UINT32', 'Total DC power', 0],
battery_power: [13021, 1, 'UINT16', 'battery_power', 0],
'Load power': [13007, 2, 'INT32', 'Load power', 0],
'Export power': [13009, 2, 'INT32', 'Export power', 0],
// "Reactive power": [5032, 2, 'INT32', "Reactive power", 0],
// "Meter Reactive power": [5600, 2, 'INT32', "Meter Reactive power", 0],
'Total active power': [13033, 2, 'INT32', 'Total active power', 0],
Nominalactivepower: [5000, 1, 'UINT16', 'Nominal active power', -1],
'Output type': [5001, 1, 'UINT16', 'Output type 0-Single phase; 1-3P4L; 2-3P3L', 0],
DailyOutputEnergy: [5002, 1, 'UINT16', 'Daily Output Energy pv + batt discharge', 0],
// "DailyexportpowerfromPV": [13004, 1, 'UINT16', "Daily export power from PV", -1],
// "TotalexportpowerfromPV": [13005, 2, 'UINT32', "Total export power from PV", -1],
temperature: [5007, 1, 'UINT16', 'temperature', -1],
// "MPPT 1 Voltage": [5010, 1, 'UINT16', "MPPT 1 Voltage",-1],
// "MPPT 2 Voltage": [5012, 1, 'UINT16', "MPPT 2 Voltage",-1],
// "MPPT 1 Current": [5011, 1, 'UINT16', "MPPT 1 Current",-1],
// "MPPT 2 Current": [5013, 1, 'UINT16', "MPPT 2 Current",-1],
'A-Blinevoltage': [5018, 1, 'UINT16', 'A-B line voltage', -1],
'B-Clinevoltage': [5019, 1, 'UINT16', 'B-C line voltage', -1],
'C-Alinevoltage': [5020, 1, 'UINT16', 'C-A line voltage', -1],
'A-current': [13030, 1, 'INT16', 'Phase A current', -1],
'B-current': [13031, 1, 'INT16', 'Phase B current', -1],
'C-current': [13032, 1, 'INT16', 'Phase C current', -1],
// "Gridfrequency": [5035, 1, 'UINT16', "Grid frequency",-1],
// "Workstate": [5037, 1, 'UINT16', "Work state",0],
// DEVICE_WORK_STATE_1_CODES = {
// 0x0: "Run",
// 0x8000: "Stopped",
// 0x1300: "Key stop",
// 0x1500: "Emergency stop",
// 0x1400: "Standby",
// 0x1200: "Initial standby",
// 0x1600: "Starting",
// 0x9100: "Alarm run",
// 0x8100: "Derating run",
// 0x8200: "Dispatch run",
// 0x5500: "Fault",
// }
Systemstate: [12999, 1, 'UINT16', 'System state', 0],
// SYSTEM_STATE_CODES = {
// 0x2: "Stop",
// 0x8: "Standby",
// 0x10: "Initial Standby",
// 0x20: "Startup",
// 0x40: "Running",
// 0x100: "Falt",
// 0x400: "Running in maintain mode",
// 0x800: "Running in forced mode",
// 0x1000: "Running in off-grid mode",
// 0x2501: "Restarting",
// 0x4000: "Running in external EMS mode",
// }
Runningstate: [13000, 1, 'BITS', 'Running state', 0],
// RUNNING_STATE_BITS = {
// 0b00000001: "status_power_generated_from_pv",
// 0b00000010: "status_charging",
// 0b00000100: "status_discharging",
// 0b00001000: "status_load_is_active",
// 0b00010000: "status_exporting_power_to_grid",
// 0b00100000: "status_importing_power_from_grid",
// 0b10000000: "status_power_generated_from_load",
// }
// Load power 13008 - 13009 S32 1W
// Export power 13010 - 13011 S32
// Self-consumpti on of today 13029 U160.1%
battery_Capacity: [13038, 1, 'UINT16', 'battery_Capacity', -1],
battery_level: [13022, 1, 'UINT16', 'battery_level', 0],
CycleCount: [13110, 1, 'UINT16', 'Cycle Count', 0],
battery_state_of_health: [13023, 1, 'UINT16', 'battery_state_of_health', 0],
battery_temperature: [13024, 1, 'INT16', 'battery_temperature', -1],
battery_voltage: [13019, 1, 'UINT16', 'battery_voltage', 0],
'Daily import energy': [13035, 1, 'UINT16', 'Daily import energy', -1],
'Total import energy': [13036, 2, 'UINT32', 'Total import energy', -1],
'Daily export energy': [13044, 1, 'UINT16', 'Daily export energy', -1],
'Total export energy': [13045, 2, 'UINT32', 'Total export energy', -1],
'Daily battery charge energy from PV': [13011, 1, 'UINT16', 'Daily battery charge energy from PV', -1],
'Total battery charge energy from PV': [13012, 2, 'UINT32', 'Total battery charge energy from PV', -1],
'Daily battery discharge energy': [13025, 1, 'UINT16', 'Daily battery discharge energy', -1],
'Total battery discharge energy': [13026, 2, 'UINT32', 'Total battery discharge energy', -1],
'Daily battery charge energy': [13039, 1, 'UINT16', 'Daily battery charge energy', -1],
'Total battery charge energy': [13040, 2, 'UINT32', 'Total battery charge energy', -1],
// "Charge/discharge": [13050, 1, 'UINT16', "Charge/discharge",0],
// "Charge/discharge_power": [13051, 1, 'UINT16', "Charge/discharge power",0],
// "Max. discharge current": [13065, 1, 'UINT16', "Max. discharge current",0],
// "Max. charge current": [13066, 1, 'UINT16', "Max. charge current",0],
};
for (const [key, value] of Object.entries(registers)) {
// console.log(key, value);
// start normale poll
// U16: 16-bit unsigned integer, big-endian
// S16: 16-bit signed integer, big-endian
// U32: 32-bit unsigned integer; little-endian for double-word data. Big-endian for byte data
// S32: 32-bit signed integer; little-endian for double-word data. Big-endian for byte data
// if current_register.data_type == "U16":
// value = response.registers[register_index]
// elif current_register.data_type == "U32":
// value = (response.registers[register_index + 1] << 16) + response.registers[register_index]
// elif current_register.data_type == "S16":
// value = int.from_bytes(response.registers[register_index].to_bytes(2, "little"), "little", signed=True)
// elif current_register.data_type == "S32":
// value = int.from_bytes(((response.registers[register_index + 1] << 16) + response.registers[register_index]).to_bytes(4, "little"), "little", signed=True)
client
.readInputRegisters(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] == 'INT16') {
console.log(`${value[3]}: ${resp.response._body._valuesAsBuffer.readInt16BE()}`);
} else if (value[2] == 'INT32') {
resultValue = ((resp.response._body._valuesAsArray[1] << 16) | resp.response._body._valuesAsArray[0] | 0).toString();
console.log(`${value[3]}: ${resultValue}`);
} else if (value[2] == 'UINT32') {
resultValue = ((resp.response._body._valuesAsArray[1] << 16) | resp.response._body._valuesAsArray[0]).toString();
console.log(`${value[3]}: ${resultValue}`);
} else if (value[2] == 'BITS') {
const value2 = Number(resp.response._body._valuesAsArray[0].toString());
const lowVal = value2 & 0xff;
const highval = (value2 >> 8) & 0xff;
const bit0 = lowVal & (1 << 0);
const bit1 = lowVal & (1 << 1);
const bit2 = lowVal & (1 << 2);
const bit3 = lowVal & (1 << 3);
const bit4 = lowVal & (1 << 4);
const bit5 = lowVal & (1 << 5);
const bit6 = lowVal & (1 << 6);
const bit7 = lowVal & (1 << 7);
console.log(`${value[3]}: ${lowVal}`);
console.log(`${value[3]}: ${highval}`);
console.log(`bit0 ${bit0}`);
console.log(`bit1 ${bit1}`);
console.log(`bit2 ${bit2}`);
console.log(`bit3 ${bit3}`);
console.log(`bit4 ${bit4}`);
console.log(`bit5 ${bit5}`);
console.log(`bit6 ${bit6}`);
console.log(`bit7 ${bit7}`);
console.log(`bit0 ${bit0}`);
if (bit0 == 1) {
console.log('status_power_generated_from_pv true');
} else {
console.log('status_power_generated_from_pv false');
}
console.log(`bit1 ${bit1}`);
if (bit1 == 2) {
console.log('status_charging true');
} else {
console.log('status_charging false');
}
console.log(`bit2 ${bit2}`);
if (bit2 == 4) {
console.log('status_discharging true');
} else {
console.log('status_discharging false');
}
console.log(`bit3 ${bit3}`);
if (bit3 == 8) {
console.log('status_load_is_active true');
} else {
console.log('status_load_is_active false');
}
console.log(`bit4 ${bit4}`);
if (bit4 == 16) {
console.log('status_exporting_power_to_grid true');
} else {
console.log('status_exporting_power_to_grid false');
}
console.log(`bit5 ${bit5}`);
if (bit5 == 32) {
console.log('status_importing_power_from_grid true');
} else {
console.log('status_importing_power_from_grid false');
}
console.log(`bit7 ${bit7}`);
if (bit7 == 128) {
console.log('status_power_generated_from_load true');
} else {
console.log('status_power_generated_from_load false');
}
} else {
console.log(`${key}: type not found ${value[2]}`);
}
})
.catch((err) => {
console.log(key);
console.log(err);
});
}
delay(() => {
socket.end();
}, 20000);
});
// avoid all the crash reports
socket.on('error', (err) => {
console.log(err);
socket.end();
});