-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomms.cpp
More file actions
2184 lines (1860 loc) · 60.5 KB
/
Copy pathcomms.cpp
File metadata and controls
2184 lines (1860 loc) · 60.5 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
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Copyright (C) 2012 by Jonathan Duddington *
* email: jonsd@users.sourceforge.net *
* *
* 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/>. *
***************************************************************************/
/* Special thanks to Curt Blank (http://www.curtronics.com/) for his "aurora"
* command-line program which shows how to communicate with, and retrieve
* information from, the Aurora inverters
*/
#ifdef __WXMSW__
//#include <windows.h>
//#include <wx/msw/registry.h>
#else
#include <termios.h>
#endif
#include <errno.h>
#include <wx/filename.h>
#include <wx/thread.h>
#include "auroramon.h"
#pragma region Declarations
#define STATE_OK 0
#define opGetDSP 59
#define opGetCE 78
#define TimeBase 946684800
int energy_today_adjust = 0; // use if the inverter starts with a spurious non-zero energy-today value
int big_endian = 0;
int SerialBlocking = 2; // x10mS serial port timeout for read (Linux)
int max_send_attempts = 3;
#ifdef __WXMSW__
static int max_read_attempts = 6;
#else
static int max_read_attempts = 5;
#endif
static int comms_error = 0;
static int transmission_state_error = 0;
static int serial_port_error = 0;
static int comms_inverter = 0;
#define N_CLR_ATTEMPTS 1000
#define N_SERIALBUF 11
static char SerialBuf[N_SERIALBUF];
static const char *SerialBufSpaces = " "; // fill SerialBuf with 10 spaces and /0
int command_inverter = 0;
int command_type = 0;
int command_queue = 0;
void SendCommand(int inv, int type);
INVERTER_RESPONSE inverter_response[N_INV];
INVERTER_RESPONSE *ir;
#define TENSEC_BASE 0x000a
#define TENSEC_MAX 8640
#define DAILY_BASE 0x438c
#define DAILY_NDAYS 366
#define RETRIEVE_10SEC 2
#define RETRIEVE_DAILY 3
int retrieving_data = 0;
int retrieving_inv = 0;
int retrieving_progress = 0;
int retrieving_finished = 0;
int retrieving_pvoutput;
wxString retrieving_fname = wxEmptyString;
wxString retrieve_message = wxEmptyString;
int settime_offset = 0;
static int CE_start = 0;
static int CE_next = 0;
static int CE_count = 0;
static int CE_errors = 0;
static int CE_inv_addr = 0;
static int CE_inv = 0;
static double CE_scale = 1;
static int CE_today_ix = 0;
static int CE_verbose = 0;
static unsigned short *CE_data = NULL;
#pragma endregion
// Done 09/08/2022 - fixed bug in QueueCommand() where inv parameter was missing from LogMessage resulting in an 'ArgType' error when Inverter 'Retrieve Daily Energy' and 'Retrieve 10sec Energy' requests were made
// Done 09/08/2022 - expanded logging to capture details for Communicat::Transmission_state_error message
void LogCommMsg(wxString string)
{//==============================
FILE *f;
char buf[256];
struct tm *btime;
time_t current_time;
//return;
if((inverters[comms_inverter].alive == 0) && (inverters[comms_inverter].fails > 3))
return;
time(¤t_time);
btime = localtime(¤t_time);
strncpy0(buf, wxString(data_dir+_T("/system/com_log.txt")).mb_str(wxConvLocal), sizeof(buf));
if((f = fopen(buf, "a")) != NULL)
{
strncpy0(buf, string.mb_str(wxConvLocal), sizeof(buf));
fprintf(f, "%.2d:%.2d:%.2d %s\n", btime->tm_hour, btime->tm_min, btime->tm_sec, buf);
fclose(f);
}
}
class InverterThread: public wxThread
{//============================
public:
InverterThread(void);
virtual void *Entry();
private:
int addr;
int type;
};
InverterThread::InverterThread(void)
: wxThread()
{//=================================
type = 0;
}
InverterThread *inverter_thread;
int QueueCommand(int inv, int type)
{//================================
wxString message = wxEmptyString;
if(command_queue != 0)
return(-1);
command_queue = (type << 8) + inv;
if((type == cmdInverter10SecEnergy) || (type == cmdInverterDailyEnergy))
{
message = _T("Retrieve ") + retrieve_message;
}
if(message != wxEmptyString)
{
if(inverter_address[1] != 0)
LogMessage(wxString::Format(_T("Inverter %d: %s"), inv, message.c_str()), 1);
else
LogMessage(message, 1);
}
return(0);
}
#ifdef __WXMSW__
static HANDLE fd_serial = INVALID_HANDLE_VALUE;
#ifdef deleted
void FindSerialPorts()
{//===================
int ix;
size_t nSubKeys;
wxString strTemp;
wxRegKey *pRegKey = new wxRegKey("HKEY_LOCAL_MACHINE\\HARDWARE\\DEVICEMAP\\SERIALCOMM");
if(!pRegKey->Exists())
{
}
//Retrive the number of SubKeys and enumerate them
pRegKey->GetKeyInfo(&nSubKeys,NULL,NULL,NULL);
pRegKey->GetFirstKey(strTemp,1);
for(int ix=0;ix<nSubKeys;ix++)
{
wxMessageBox(strTemp,"SubKey Name",0,this);
pRegKey->GetNextKey(strTemp,1);
}
}
#endif
void SerialClose()
{//===============
HANDLE handle;
if(fd_serial != INVALID_HANDLE_VALUE)
{
handle = fd_serial;
fd_serial = INVALID_HANDLE_VALUE;
CloseHandle(handle);
}
}
int SerialOpen()
{//=============
// Windows
int err =0;
wxString dev_name;
DCB dcbSerialParams = {0};
COMMTIMEOUTS timeouts = {0};
dev_name = serial_port.mb_str(wxConvLocal);
if(dev_name.empty())
return(-1); // no serial port set
fd_serial = CreateFile(dev_name.fn_str(), GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if(fd_serial == INVALID_HANDLE_VALUE)
{
if(GetLastError() == ERROR_FILE_NOT_FOUND)
{
// serial port does not exist
err = -1;
}
else
{
// some other error
err = -2;
}
fd_serial = INVALID_HANDLE_VALUE;
return(err);
}
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if(!GetCommState(fd_serial, &dcbSerialParams))
{
// error getting state
err = -3;
}
else
{
dcbSerialParams.BaudRate = CBR_19200;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
if(!SetCommState(fd_serial, &dcbSerialParams))
{
// error setting serial port state
err = -4;
}
else
{
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 80;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
if(!SetCommTimeouts(fd_serial, &timeouts))
{
// error in setting timeouts
err = -5;
}
}
}
if(err != 0)
{
SerialClose();
}
return(err);
}
#else // LINUX
static struct termios old_terminal; // saved previous serial device configuration
static int fd_serial = -1;
int SerialConfig()
{//===============
struct termios terminal; // serial device configuration
tcgetattr(fd_serial, &old_terminal); // save previous port settings
memset(&terminal, 0, sizeof(terminal)); // no parity, one stop bit, no hangup
terminal.c_cflag &= ~PARENB; // no parity
terminal.c_cflag &= ~CSTOPB; // one stop bit
terminal.c_cflag &= ~CSIZE; // character size mask
terminal.c_cflag &= ~HUPCL; // no hangup
// if (bRTSCTS)
// terminal.c_cflag |= CRTSCTS; /* enable hardware flow control */
// else
terminal.c_cflag &= ~CRTSCTS; /* disable hardware flow control */
terminal.c_cflag |= CS8 | CLOCAL | CREAD; // 8 bit - ignore modem control lines - enable receiver
// if (bXonXoff)
// terminal.c_iflag |= (IXON | IXOFF); /* enable XON/XOFF flow control on output & input */
// else
terminal.c_iflag &= ~(IXON | IXOFF); /* disable XON/XOFF flow control on output & input*/
terminal.c_iflag |= IGNBRK | IGNPAR; /* ignore BREAK condition on input & framing errors & parity errors */
terminal.c_oflag = 0; /* set serial device input mode (non-canonical, no echo,...) */
terminal.c_oflag &= ~OPOST; /* disable output processing */
terminal.c_lflag = 0;
terminal.c_cc[VTIME] = SerialBlocking; /* timeout in 1/10 sec intervals */
terminal.c_cc[VMIN] = 0; /* block until char or timeout */
if(cfsetospeed(&terminal, B19200) != 0)
{
// failed to set output speed
return(-3);
}
if(cfsetispeed(&terminal, B19200) != 0)
{
// failed to set input speed
return(-4);
}
if(tcflush(fd_serial, TCIFLUSH) != 0)
{
}
if(tcsetattr(fd_serial, TCSANOW, &terminal) != 0)
{
return(-5);
}
if(tcflush(fd_serial, TCIOFLUSH) != 0)
{
return(-6);
}
return(0);
}
int SerialOpen()
{//==============
// Linux
int result;
char dev_name[20];
struct stat statbuf;
strncpy0(dev_name, serial_port.mb_str(wxConvLocal), sizeof(dev_name));
if(dev_name[0] == 0)
return(-1); // no serial port set
if(stat(dev_name,&statbuf) != 0)
{
if(strcmp(dev_name, "/dev/ttyUSB0") == 0)
{
// Try a different ttyUSB port
strcpy(dev_name, "/dev/ttyUSB1");
if(stat(dev_name,&statbuf) != 0)
return(-1);
}
else
{
return(-1);
}
}
// open the serial port
if((fd_serial = open(dev_name, O_RDWR | O_NOCTTY )) < 0)
{
// failed to open serial port device
return(-2);
}
if((result = SerialConfig()) < 0)
{
close(fd_serial);
fd_serial = -1;
return(result);
}
return(0);
}
void SerialClose()
{//===============
int handle;
if(fd_serial < 0)
return;
handle = fd_serial;
fd_serial = -1;
if(tcsetattr(handle, TCSANOW, &old_terminal) != 0)
{
return;
}
if(tcflush(handle, TCIOFLUSH) != 0)
{
}
close(handle);
}
#endif
/*--------------------------------------------------------------------------
crc16
16 12 5
this is the CCITT CRC 16 polynomial X + X + X + 1.
This is 0x1021 when x is 2, but the way the algorithm works
we use 0x8408 (the reverse of the bit pattern). The high
bit is always assumed to be set, thus we only use 16 bits to
represent the 17 bit value.
----------------------------------------------------------------------------*/
#define POLY 0x8408 /* 0x1021 bit reversed */
unsigned short crc16(char *data_p, unsigned short length)
{//======================================================
unsigned char i;
unsigned int data;
unsigned int crc = 0xffff;
if (length == 0)
return (~crc);
do
{
for (i=0, data=(unsigned int)0xff & *data_p++;
i < 8;
i++, data >>= 1)
{
if ((crc & 0x0001) ^ (data & 0x0001))
crc = (crc >> 1) ^ POLY;
else
crc >>= 1;
}
} while (--length);
crc = ~crc;
return (crc);
}
int ReadNextChar(char *out, int timeout)
{//=====================================
#ifdef __WXMSW__
unsigned long n_read;
if(!ReadFile(fd_serial, out, 1, &n_read, NULL))
{
}
#else
int n_read;
errno = 0;
n_read = read(fd_serial, out, 1);
// we get result=0 if inverter is not alive
if(errno != 0)
{
}
if(n_read == -1)
{
// failed to read from serial device
}
#endif
return(n_read);
}
int ReadToBuffer(char *out, int n_chars)
{//=====================================
int ix;
char ch;
int result;
int retry = 0;
ix = 0;
while(ix < n_chars)
{
while((result = ReadNextChar(&ch, 0)) == 0)
{
retry++;
if(retry > max_read_attempts)
{
return(0);
}
}
if(result == 1)
{
out[ix++] = ch;
}
else
return(result);
}
if(retry > 1)
{
LogCommMsg(wxString::Format(_T(" Wait %d"), retry));
}
return(ix); // OK
}
int Communicate(int check_state)
{//=============================
int count = 0;
unsigned long nchars = 0;
int result = 0;
int crc_ok = 0;
int crcValue;
char ch;
int max_attempts;
int attempts;
char SerialBufSave[N_SERIALBUF];
int ix;
//wxStartTimer();
memcpy(SerialBufSave, SerialBuf, N_SERIALBUF);
max_attempts = max_send_attempts;
if(inverters[comms_inverter].alive == 0)
{
// This inverter is not alive. Don't do repeat attempts, and so don't slow down the polling rate of the other inverter too much.
max_attempts = 1;
if((inverters[comms_inverter].fails > 3) && (inverters[comms_inverter ^ 1].alive == 0))
{
// The other inverter is also not alive. Reduce the polling rate.
inverter_thread->Sleep(300);
max_attempts = 2;
}
}
for(attempts = 1; (crc_ok == 0) && (attempts <= max_attempts); attempts++)
{
if(attempts > 1)
{
SerialClose();
SerialOpen();
inverters[comms_inverter].comms_error = 1;
}
memcpy(SerialBuf, SerialBufSave, N_SERIALBUF);
while((ReadNextChar(&ch, 0) != 0) && (count < N_CLR_ATTEMPTS))
{
count++;
}
crcValue = crc16(SerialBuf, 8);
SerialBuf[8] = crcValue & 0xff;
SerialBuf[9] = (crcValue >> 8) & 0xff;
SerialBuf[10] = 0;
#ifdef __WXMSW__
WriteFile(fd_serial, SerialBuf, 10, &nchars, NULL);
#else
if(tcflush(fd_serial, TCIOFLUSH) != 0)
{
result = 1;
}
nchars = write(fd_serial, SerialBuf, 10); // should we repeat until all chars have been sent?
if(tcdrain(fd_serial) != 0)
{
result = 2;
}
#endif
if(nchars != 10)
{
LogCommMsg(wxString::Format(_T("%d Only written %d chars"), attempts, nchars));
if(nchars <= 0)
continue; // we havent sent anything, so no point in looking for a reply
}
strcpy(SerialBuf, SerialBufSpaces);
result = ReadToBuffer(SerialBuf, 8);
if(result == 8)
{
crcValue = crc16(SerialBuf, 6);
if(((unsigned char)SerialBuf[6] != (crcValue & 0xff)) || ((unsigned char)SerialBuf[7] != ((crcValue >> 8) & 0xff)))
{
// crc error
LogCommMsg(wxString::Format(_T("%d CRC error, nchars %d opcode %2d %2d"), attempts, result, SerialBufSave[1], SerialBufSave[2]));
}
else
{
crc_ok = 1;
}
}
else
if(result == 0)
{
inverter_thread->Sleep(100); // thread sleep
if(attempts >= max_attempts)
LogCommMsg(wxString::Format(_T("%d Wait %d timeout - fail"), attempts, max_read_attempts+1));
else
LogCommMsg(wxString::Format(_T("%d Wait %d timeout, retry"), attempts, max_read_attempts+1));
}
else
{
// failed to read from serial device
LogCommMsg(wxString::Format(_T("%d ReadToBuffer %d chars"), attempts, result));
}
}
if(crc_ok == 0)
{
if(result > 0)
{
LogCommMsg(_T("CRC error, failed\n"));
}
result = -1;
}
if((check_state==1) && (SerialBuf[0] != STATE_OK))
{
transmission_state_error = SerialBuf[0];
if(transmission_state_error != 0x20)
{
// 0x20 is the space character we put into SerialBuf[0], unchanged
LogMessage(wxString::Format(_T("----")), 1);
LogMessage(wxString::Format(_T("Transmission state error %d buf[0]=%d cmd=%d"), transmission_state_error, SerialBuf[0], SerialBufSave[1]),1);
LogCommMsg(wxString::Format(_T("Transmission state error %d buf[0]=%d cmd=%d\n"), transmission_state_error, SerialBuf[0], SerialBufSave[1]));
// list out the buffer contents here
for (ix = 0; SerialBuf[ix] !=0; ix++)
{
LogMessage(wxString::Format(_T("Transmission state error - buffer content buf[%d]=%d"), ix, SerialBuf[ix]),1);
LogCommMsg(wxString::Format(_T("Transmission state error - buffer content buf[%d]=%d\n"), ix, SerialBuf[ix]));
}
LogMessage(wxString::Format(_T("Transmission state error - End")), 1);
LogMessage(wxString::Format(_T("----")), 1);
}
result = -2;
}
if(result <= 0)
{
comms_error |= 1;
}
else
{
//LogCommMsg(wxString::Format(_T("OK opcode %2d %2d result %.2x %.2x %.2x %.2x %.2x %.2x"), SerialBufSave[1], SerialBufSave[2],
// SerialBuf[0], SerialBuf[1], SerialBuf[2]&0xff, SerialBuf[3]&0xff, SerialBuf[4]&0xff, SerialBuf[5]&0xff));
}
//LogCommMsg(wxString::Format(_T(" time %d"), wxGetElapsedTime()));
return(result);
}
unsigned long ConvertLong(char *buf)
{//=================================
unsigned long *p;
unsigned char buf2[4];
if(big_endian)
{
buf2[0] = buf[0];
buf2[1] = buf[1];
buf2[2] = buf[2];
buf2[3] = buf[3];
}
else
{
buf2[0] = buf[3];
buf2[1] = buf[2];
buf2[2] = buf[1];
buf2[3] = buf[0];
}
p = (unsigned long *)buf2;
return(*p & 0xffffffff);
}
unsigned short ConvertShort(char *buf)
{//=================================
unsigned short *p;
unsigned char buf2[4];
if(big_endian)
{
buf2[0] = buf[0];
buf2[1] = buf[1];
}
else
{
buf2[0] = buf[1];
buf2[1] = buf[0];
}
p = (unsigned short *)buf2;
return(*p);
}
float ConvertFloat(char *buf)
{//==========================
float *p;
unsigned char buf2[4];
if(big_endian)
{
buf2[0] = buf[0];
buf2[1] = buf[1];
buf2[2] = buf[2];
buf2[3] = buf[3];
}
else
{
buf2[0] = buf[3];
buf2[1] = buf[2];
buf2[2] = buf[1];
buf2[3] = buf[0];
}
p = (float *)buf2;
return(*p);
}
int GetCEdata(int addr, int param)
{//===============================
strcpy(SerialBuf, SerialBufSpaces);
SerialBuf[0] = addr;
SerialBuf[1] = opGetCE;
SerialBuf[2] = param;
SerialBuf[3] = 0;
if(Communicate(1) <= 0)
{
return(-1); // failed
}
return(ConvertLong(&SerialBuf[2]));
}
float GetDSPdata(int addr, int param)
{//==================================
float value;
int result;
strcpy(SerialBuf, SerialBufSpaces);
SerialBuf[0] = addr;
SerialBuf[1] = opGetDSP;
SerialBuf[2] = param;
SerialBuf[3] = 0;
if((result = Communicate(1)) <= 0)
{
return(-97999); // big negative number indicates fail
}
value = ConvertFloat(&SerialBuf[2]);
return(value);
}
typedef struct {
int value;
const char *name;
} NAME_TABLE;
typedef struct {
int value;
double scale; // for 10sec data. -1 means "these statistics are not available for this model of Inverter at the moment"
const char *name;
} MODEL_TABLE;
// eg. PVI-4.2-OUTD-UK-W
MODEL_TABLE model_names[] = {
{'1', 0.0970019, "PVI-3.0-OUTD"},
{'2', 0.1617742, "PVI-3.3-OUTD"},
{'3', 0.1617742, "PVI-3.6-OUTD"},
{'4', 0.1617742, "PVI-4.2-OUTD"},
{'5', 0.1617742, "PVI-5000-OUTD"},
{'6', 0.1617742, "PVI-6000-OUTD"},
{'A', 0.1617742, "PVI-CENTRAL-350"},
{'B', 0.1617742, "PVI-CENTRAL-350"},
{'C', 0.1617742, "PVI-MODULE-50"},
{'D', 0.5320955, "PVI-12.5-OUTD"},
{'G', -1, "UNO-2.5-I"},
{'H', -1, "PVI-4.6-I-OUTD"},
{'I', 0.1004004, "PVI-3600"},
{'L', 0.1617742, "PVI-CENTRAL-350"},
{'M', 0.5320955, "PVI-CENTRAL-250"},
{'O', 0.1004004, "PVI-3600-OUTD"},
{'P', 0.1617742, "3-PHASE-INTERFACE"},
{'T', 0.5320955, "PVI-12.5-I-OUTD"}, // scale ? (output 480 VAC)
{'U', 0.5320955, "PVI-12.5-I-OUTD"}, // scale ? (output 208 VAC)
{'V', 0.5320955, "PVI-12.5-I-OUTD"}, // scale ? (output 380 VAC)
{'X', 0.5320955, "PVI-10.0-OUTD"},
{'Y', 0.5320955, "PVI-TRIO-30-OUTD"}, // scale ?
{'Z', 0.5320955, "PVI-12.5-I-OUTD"}, // scale ? (output 600 VAC)
{'g', -1, "UNO-2.0-I"},
{'h', -1, "PVI-3.8-I"},
{'i', 0.0557842, "PVI-2000"},
{'o', 0.0557842, "PVI-2000-OUTD"},
{'t', 0.5320955, "PVI-10.0-I-OUTD"}, // scale ? (output 480 VAC)
{'u', 0.5320955, "PVI-10.0-I-OUTD"}, // scale ? (output 208 VAC)
{'v', 0.5320955, "PVI-10.0-I-OUTD"}, // scale ? (output 380 VAC)
{'w', 0.5320955, "PVI-10.0-I-OUTD"}, // scale ? (output 480 VAC current limit 12 A)
{'y', 0.5320955, "PVI-TRIO-20-OUTD"}, // scale ?
{'z', 0.5320955, "PVI-10.0-I-OUTD"}, // scale ? (output 600 VAC)
{-1, -1, NULL}
};
NAME_TABLE standard_names[] = {
{'A', "US UL1741"},
{'B', "BE VDE Belgium Model"},
{'C', "CZ Czech Republic"},
{'E', "DE VDE0126"},
{'F', "FR VDE0126"},
{'G', "GR VDE Greece Model"},
{'H', "HU Hungary"},
{'I', "IT ENEL DK 5950"},
{'K', "AU AS 4777"},
{'O', "KR Korea"},
{'P', "PT Portugal"},
{'Q', "CN China"},
{'R', "IE EN50438"},
{'S', "ES DR 1663/2000"},
{'T', "TW Taiwan"},
{'U', "UK G83"},
{'W', "DE BDEW"},
{'a', "US UL1741 Vout = 208 single phase"},
{'b', "US UL1741 Vout = 240 single phase"},
{'c', "US UL1741 Vout = 277 single phase"},
{'e', " VDE AR-N-4105"},
{'k', "IL Isreal - Derived from AS"},
{'o', " Corsica"},
{'u', "UK G59"},
{-1, NULL}
};
const char *LookupName(NAME_TABLE *t, int value)
{//=============================================
while(t->name != NULL)
{
if(t->value == value)
return(t->name);
t++;
}
return(NULL);
}
MODEL_TABLE *LookupModel(MODEL_TABLE *t, int value)
{//================================================
while(t->name != NULL)
{
if(t->value == value)
return(t);
t++;
}
return(NULL);
}
time_t GetInverterTime(int addr, time_t *timeval, int *timediff)
{//=============================================================
time_t computer_time;
time_t inverter_time;
int diff;
strcpy(SerialBuf, SerialBufSpaces);
SerialBuf[0] = addr;
SerialBuf[1] = 70;
SerialBuf[2] = 0;
if(Communicate(1) <= 0)
return(-1);
computer_time = time(NULL);
inverter_time = ConvertLong(&SerialBuf[2]);
inverter_time += (time_t)TimeBase;
inverter_time -= GetGmtOffset(0);
diff = inverter_time - computer_time;
if(timeval != NULL)
*timeval = inverter_time;
if(timediff != NULL)
*timediff = diff;
return(0);
}
int ResetPartial(int inv)
{//======================
// reset the partial energy total and partial time counter
strcpy(SerialBuf, SerialBufSpaces);
SerialBuf[0] = inverter_address[inv];
SerialBuf[1] = 80; // get counter
SerialBuf[2] = 3;
if(Communicate(1) <= 0) return(-1);
return(0);
}
int GetInverterInfo(int inv)
{//=========================
// -fgmnpv GetVerFW GetMfgDate GetConf GetSN GetPN GetVer
FILE *f_inv;
wxString fname;
int addr;
int ix;
const char *pc, *ps, *pt, *pw;
MODEL_TABLE *pm;
char firmware[4] = {' ',' ',' ',' '};
char serial_number[7] = {0};
char part_number[7] = {0};
char model_name_buf[50];
char standard_name_buf[50];
char country_letters[4];
static const char *unknown = "unknown";
static char manufacture_week[3] = " ";
static char manufacture_year[3] = " ";
addr = inverter_address[inv];
comms_error = 0;
pc = unknown;
strcpy(SerialBuf, SerialBufSpaces);
SerialBuf[0] = addr;
SerialBuf[1] = 72; // firmware release
SerialBuf[2] = 0;
if(Communicate(1) <= 0) return(-1);
firmware[0] = SerialBuf[2];
firmware[1] = SerialBuf[3];
firmware[2] = SerialBuf[4];
firmware[3] = SerialBuf[5];
strcpy(SerialBuf, SerialBufSpaces);
SerialBuf[0] = addr;
SerialBuf[1] = 65; // manufacturing week and year
SerialBuf[2] = 0;
if(Communicate(1) <= 0) return(-1);
manufacture_week[0] = SerialBuf[2];
manufacture_week[1] = SerialBuf[3];
manufacture_year[0] = SerialBuf[4];
manufacture_year[1] = SerialBuf[5];
strcpy(SerialBuf, SerialBufSpaces);
SerialBuf[0] = addr;
SerialBuf[1] = 77; // system configuration
SerialBuf[2] = 0;
if(Communicate(1) <= 0) return(-1);
switch(SerialBuf[2])
{
case 0: pc = "System operating with both strings."; break;
case 1: pc = "String 1 connected, String 2 disconnected."; break;
case 2: pc = "String 2 connected, String 1 disconnected."; break;
default: pc = unknown;
}
strcpy(SerialBuf, SerialBufSpaces);
SerialBuf[0] = addr;
SerialBuf[1] = 63; // serial number
SerialBuf[2] = 0;
if(Communicate(0) <= 0) return(-1); // note, buf[0] is used to return the serial number, not the transmission state
strncpy(serial_number, SerialBuf, 6);
serial_number[6] = 0;
strcpy(SerialBuf, SerialBufSpaces);
SerialBuf[0] = addr;
SerialBuf[1] = 52; // part number
SerialBuf[2] = 0;
if(Communicate(0) <= 0) return(-1);
strncpy(part_number, SerialBuf, 6);
part_number[6] = 0;
strcpy(SerialBuf, SerialBufSpaces);
SerialBuf[0] = addr;
SerialBuf[1] = 58; // inverter version
SerialBuf[2] = 0;
if(Communicate(1) <= 0) return(-1);
// Log Inverter Details