/
sunsaverEEPROM.c
544 lines (382 loc) · 13.1 KB
/
sunsaverEEPROM.c
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*
* sunsaverEEPROM.c - This program reads all the EEPROM registers on a Moringstar SunSaver MPPT and prints the results.
*
Copyright 2010 Tom Rinehart.
Updated 2014 ken restivo <ken@restivo.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* Compile with: cc sunsaverEEPROM.c -o sunsaverEEPROM -lmodbus */
#include <stdio.h>
#include <stdlib.h>
#include <modbus/modbus.h>
#include <getopt.h>
#include <string.h>
#include <unistd.h>
#define SUNSAVERMPPT 0x01 /* MODBUS Address of the SunSaver MPPT */
#define NO_SUCH_REG 0xFFFF
#define FORCE_EEPROM_UPDATE 0x16
#define RESET_CONTROL 0xff
#define MAX_SETS 255
#define MAX_STR 255
int debug = 0;
int dry = 0;
typedef struct reg_t {
int addr;
char * name;
uint16_t (* from_display)(float);
float (* to_display)(uint16_t);
} reg;
void usage()
{
printf("usage: [options] serial_port_path\noptions:\n\t-h half duplex communication\n\t-d /path/to/device\n\t-v verbose (debug)\n\t-n dry run (no comms)\n\t-s reg_name=val another_reg_name=another_val, etc... (set registers)\n");
}
uint16_t straight_cast_int(float f)
{
return (uint16_t)f;
}
float straight_cast_float(uint16_t i)
{
return (float)i;
}
uint16_t float_to_int(float f)
{
return (uint16_t)((f / 100) * 32768);
}
float to_float(uint16_t val)
{
return (val * 100.0) / 32768.0;
}
float unsigned_to_float(uint16_t val)
{
return (val * 100.0) / 65536.0;
}
float fl_conv_2(uint16_t val)
{
return (val * 79.16) / 32768.0;
}
uint32_t shift_32(uint16_t hi, uint16_t lo)
{
return (hi << 16) + lo;
}
float shift_float(uint16_t hi, uint16_t lo)
{
return shift_32(hi, lo) * 0.1;
}
#define NUM_REGS 15
static const reg regs[NUM_REGS] = {
{ 0xE000, "EV_reg", float_to_int, to_float },
{ 0xE001, "EV_float", float_to_int, to_float },
{ 0xE002, "Et_float", straight_cast_int, straight_cast_float},
{ 0xE003, "Et_floatlb", straight_cast_int, straight_cast_float},
{ 0xE004, "EV_floatlb_trip", float_to_int, to_float},
{ 0xE005, "EV_float_cancel", float_to_int, to_float},
{ 0xE006, "Et_float_exit_cum", straight_cast_int, straight_cast_float},
{ 0xE00D, "EV_reg2", float_to_int, to_float },
{ 0xE00E, "EV_float2", float_to_int, to_float },
{ 0xE00F, "Et_float2", straight_cast_int, straight_cast_float},
{ 0xE010, "Et_floatlb2", straight_cast_int, straight_cast_float},
{ 0xE011, "EV_floatlb_trip2", float_to_int, to_float},
{ 0xE012, "EV_float_cancel2", float_to_int, to_float},
{ 0xE013, "Et_float_exit_cum2", straight_cast_int, straight_cast_float},
// TODO: the rest of the registers!
{ NO_SUCH_REG, "NO SUCH REG", NULL, NULL } // last one must always be the incorrect one
};
reg lookup_reg(char * name)
{
int i;
for(i = 0; i < NUM_REGS; i++){
if(0 == strcmp(regs[i].name, name)){
// TODO: just pass the pointer, it's a const struct anyway
return regs[i];
}
}
return regs[NUM_REGS - 1]; // last one is the illegal reg
}
void commit_options(modbus_param_t * mb_param)
{
int ret;
// XXX Redundant? seems to do nothing
if(debug > 0){
printf ("\n\nnow forcing the eeprom\n");
}
ret = force_single_coil(mb_param, SUNSAVERMPPT, FORCE_EEPROM_UPDATE, 1);
if(debug > 0){
printf("set returnd %d\n", ret);
}
sleep(2); // just to be sure
if(debug > 0){
printf ("now forcing the reset. this will NOT return anything!\n");
}
ret = force_single_coil(mb_param, SUNSAVERMPPT, RESET_CONTROL, 1);
if(debug > 0){
printf("set returnd %d\n", ret);
}
printf("NOTE: you will need to physically power the unit off by disconnecting the battery, in order for the changes to take effect.\n\nWaiting for unit to reset and communications to return...\n");
sleep(10);
}
void set_options(modbus_param_t * mb_param, char * arg){
int ret;
uint16_t set_val;
reg r;
char key[256];
char * val;
strcpy (key, arg);
strtok_r (key, "=", &val);
if(debug > 0){
printf("arg %s, key %s, val %s\n", arg, key, val);
}
r = lookup_reg(key);
if(NO_SUCH_REG == r.addr){
printf("Error, %s is not a register\n", key);
usage();
exit(1);
}
set_val = (*(r.from_display))(strtof(val, NULL));
if(debug > 0){
printf("arg %s is %s at 0x%04x, as int is %d\n", key, r.name, r.addr, set_val);
}
if(dry > 0 ){
printf("Dry run, not setting anything\n");
} else {
// Now actually set the stuff
ret = preset_single_register(mb_param, SUNSAVERMPPT, r.addr, set_val);
if(debug > 0){
printf("set returnd %d\n", ret);
}
sleep(5); // just to be sure. Lots of lights flash on this thing when writing. Why?
}
}
void read_values(modbus_param_t * mb_param)
{
int ret;
float EV_reg, EV_float, EV_floatlb_trip, EV_float_cancel, EV_eq;
unsigned short Et_float, Et_floatlb, Et_float_exit_cum, Et_eqcalendar, Et_eq_above, Et_eq_reg;
float EV_reg2, EV_float2, EV_floatlb_trip2, EV_float_cancel2, EV_eq2;
unsigned short Et_float2, Et_floatlb2, Et_float_exit_cum2, Et_eqcalendar2, Et_eq_above2, Et_eq_reg2;
float EV_tempcomp, EV_hvd, EV_hvr, Evb_ref_lim;
short ETb_max, ETb_min;
float EV_lvd, EV_lvr, EV_lhvd, EV_lhvr, ER_icomp, Et_lvd_warn;
float EV_soc_y2g, EV_soc_g2y, EV_soc_y2r0, EV_soc_r2y;
unsigned short Emodbus_id, Emeter_id;
float Eic_lim;
unsigned int Ehourmeter;
short Etmr_eqcalendar;
float EAhl_r, EAhl_t, EAhc_r, EAhc_t, EkWhc, EVb_min, EVb_max, EVa_max;
uint16_t data[50];
// TODO: use the dispatch table regs, instead of this cut/paste stuff.
/* Read the 0xE000 EEPROM Registers and convert the results to their proper values */
ret = read_input_registers(mb_param, SUNSAVERMPPT, 0xE000, 11, data);
printf("EEPROM Registers\n\n");
printf("Charge Settings (bank 1)\n");
EV_reg = to_float(data[0]);
printf("EV_reg = %.2f V\n",EV_reg);
EV_float = to_float(data[1]);
printf("EV_float = %.2f V\n",EV_float);
Et_float = data[2];
printf("Et_float = %d s\n",Et_float);
Et_floatlb = data[3];
printf("Et_floatlb = %d s\n",Et_floatlb);
EV_floatlb_trip = to_float(data[4]);
printf("EV_floatlb_trip = %.2f V\n",EV_floatlb_trip);
EV_float_cancel = to_float(data[5]);
printf("EV_float_cancel = %.2f V\n",EV_float_cancel);
Et_float_exit_cum = data[6];
printf("Et_float_exit_cum = %d s\n",Et_float_exit_cum);
EV_eq = to_float(data[7]);
printf("EV_eq = %.2f V\n",EV_eq);
Et_eqcalendar = data[8];
printf("Et_eqcalendar = %d days\n",Et_eqcalendar);
Et_eq_above = data[9];
printf("Et_eq_above = %d s\n",Et_eq_above);
Et_eq_reg = data[10];
printf("Et_eq_reg = %d s\n",Et_eq_reg);
/* Read the 0xE00D EEPROM Registers and convert the results to their proper values */
ret = read_input_registers(mb_param, SUNSAVERMPPT, 0xE00D, 11, data);
printf("\nCharge Settings (bank 2)\n");
EV_reg2 = to_float(data[0]);
printf("EV_reg2 = %.2f V\n",EV_reg2);
EV_float2 = to_float(data[1]);
printf("EV_float2 = %.2f V\n",EV_float2);
Et_float2 = data[2];
printf("Et_float2 = %d s\n",Et_float2);
Et_floatlb2 = data[3];
printf("Et_floatlb2 = %d s\n",Et_floatlb2);
EV_floatlb_trip2 = to_float(data[4]);
printf("EV_floatlb_trip2 = %.2f V\n",EV_floatlb_trip2);
EV_float_cancel2 = to_float(data[5]);
printf("EV_float_cancel2 = %.2f V\n",EV_float_cancel2);
Et_float_exit_cum2 = data[6];
printf("Et_float_exit_cum2 = %d s\n",Et_float_exit_cum2);
EV_eq2 = to_float(data[7]);
printf("EV_eq2 = %.2f V\n",EV_eq2);
Et_eqcalendar2 = data[8];
printf("Et_eqcalendar2 = %d days\n",Et_eqcalendar2);
Et_eq_above2 = data[9];
printf("Et_eq_above2 = %d s\n",Et_eq_above2);
Et_eq_reg2 = data[10];
printf("Et_eq_reg2 = %d s\n",Et_eq_reg2);
/* Read the 0xE01A EEPROM Registers and convert the results to their proper values */
ret = read_input_registers(mb_param, SUNSAVERMPPT, 0xE01A, 6, data);
printf("\nCharge Settings (shared)\n");
EV_tempcomp = unsigned_to_float(data[0]);
printf("EV_tempcomp = %.2f V\n",EV_tempcomp);
EV_hvd = to_float(data[1]);
printf("EV_hvd = %.2f V\n",EV_hvd);
EV_hvr = to_float(data[2]);
printf("EV_hvr = %.2f V\n",EV_hvr);
Evb_ref_lim = to_float(data[3]);
printf("Evb_ref_lim = %.2f V\n",Evb_ref_lim);
ETb_max = data[4];
printf("ETb_max = %d °C\n",ETb_max);
ETb_min = data[5];
printf("ETb_min = %d °C\n",ETb_min);
/* Read the 0xE022 EEPROM Registers and convert the results to their proper values */
ret = read_input_registers(mb_param, SUNSAVERMPPT, 0xE022, 6, data);
printf("\nLoad Settings\n");
EV_lvd = to_float(data[0]);
printf("EV_lvd = %.2f V\n",EV_lvd);
EV_lvr = to_float(data[1]);
printf("EV_lvr = %.2f V\n",EV_lvr);
EV_lhvd = to_float(data[2]);
printf("EV_lhvd = %.2f V\n",EV_lhvd);
EV_lhvr = to_float(data[3]);
printf("EV_lhvr = %.2f V\n",EV_lhvr);
ER_icomp = data[4]*1.263/65536.0;
printf("ER_icomp = %.2f ohms\n",ER_icomp);
Et_lvd_warn = data[5]*0.1;
printf("Et_lvd_warn = %.2f s\n",Et_lvd_warn);
/* Read the 0xE030 EEPROM Registers and convert the results to their proper values */
ret = read_input_registers(mb_param, SUNSAVERMPPT, 0xE030, 6, data);
printf("\nMisc Settings\n");
EV_soc_y2g = to_float(data[0]);
printf("EV_soc_y2g = %.2f V\n",EV_soc_y2g);
EV_soc_g2y = to_float(data[1]);
printf("EV_soc_g2y = %.2f V\n",EV_soc_g2y);
EV_soc_y2r0 = to_float(data[2]);
printf("EV_soc_y2r0 = %.2f V\n",EV_soc_y2r0);
EV_soc_r2y = to_float(data[3]);
printf("EV_soc_r2y = %.2f V\n",EV_soc_r2y);
Emodbus_id = data[4];
printf("Emodbus_id = %d\n",Emodbus_id);
Emeter_id = data[5];
printf("Emeter_id = %d\n",Emeter_id);
/* Read the 0xE038 EEPROM Registers and convert the results to their proper values */
ret = read_input_registers(mb_param, SUNSAVERMPPT, 0xE038, 1, data);
printf("\nPPT Settings\n");
Eic_lim = fl_conv_2(data[0]);
printf("Eic_lim = %.2f A\n",Eic_lim);
/* Read the 0xE040 EEPROM Registers and convert the results to their proper values */
ret = read_input_registers(mb_param, SUNSAVERMPPT, 0xE040, 15, data);
printf("\nRead only section of EEPROM\n");
Ehourmeter = shift_32(data[1], data[0]);
printf("Ehourmeter = %d h\n",Ehourmeter);
EAhl_r = shift_float(data[3], data[2]);
printf("EAhl_r = %.2f Ah\n",EAhl_r);
EAhl_t = shift_float(data[5], data[4]);
printf("EAhl_t = %.2f Ah\n",EAhl_t);
EAhc_r = shift_float(data[7], data[6]);
printf("EAhc_r = %.2f Ah\n",EAhc_r);
EAhc_t = shift_float(data[9], data[8]);
printf("EAhc_t = %.2f Ah\n",EAhc_t);
EkWhc = data[10]*0.1;
printf("EkWhc = %.2f kWh\n",EkWhc);
EVb_min = to_float(data[11]);
printf("EVb_min = %.2f V\n",EVb_min);
EVb_max = to_float(data[12]);
printf("EVb_max = %.2f V\n",EVb_max);
EVa_max = to_float(data[13]);
printf("EVa_max = %.2f V\n",EVa_max);
Etmr_eqcalendar = data[14];
printf("Etmr_eqcalendar = %d days\n",Etmr_eqcalendar);
}
int main(int argc, char** argv)
{
modbus_param_t mb_param;
int half_duplex = 0;
int c;
char ** svp;
int i;
int set_args_index = -1;
char set_args[MAX_SETS][MAX_STR];
char *device = NULL;
memset(set_args, 0, sizeof(set_args));
while( (c = getopt(argc, argv, "hnvd:s:")) != -1) {
switch(c){
case 's':
/* NOTE IMPORTANT! This block MUST COME FIRST in the switch, before any opts,
otherwise GCC or getopts or something gets the indexes all wrong */
// Thanks to http://stackoverflow.com/questions/3939157/c-getopt-multiple-value
// Special case the first arg
set_args_index = 0;
strcpy(set_args[0], optarg);
// Loop the rest
i=1;
while(optind < argc && *argv[optind] != '-'){
strcpy(set_args[i], argv[optind]);
if(debug > 0){
printf("getopts multiopt: %s %s\n", argv[optind], set_args[i] );
}
// Ugly but explicit
set_args_index++;
i++;
optind++;
}
break;
case 'h':
half_duplex = 1;
break;
case 'v':
debug = 1;
break;
case 'n':
dry = 1;
break;
case 'd':
device = optarg;
default:
break;
}
}
if(!device){
printf("need to give serial port on command line\n");
usage();
return(1);
}
/* Setup the serial port parameters */
modbus_init_rtu(&mb_param, device, 9600, "none", 8, 2, half_duplex);
if(debug > 0){
modbus_set_debug(&mb_param, TRUE);
}
/* Open the MODBUS connection */
if (modbus_connect(&mb_param) == -1) {
printf("ERROR Connection failed\n");
return(1);
}
if(debug > 0){
printf("We have %d set args to process...\n", set_args_index + 1);
}
i = set_args_index;
while(i >= 0){
printf("\nSetting values... %s\n", set_args[i]);
set_options(&mb_param, set_args[i]);
i--;
}
if(dry < 1 && set_args_index >= 0){
commit_options(&mb_param);
}
if(dry < 1){
read_values(&mb_param);
}
/* Close the MODBUS connection */
modbus_close(&mb_param);
return(0);
}