Module_InternalComm.c 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125
  1. #include <sys/time.h>
  2. #include <sys/timeb.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #include <sys/shm.h>
  11. #include <sys/mman.h>
  12. #include <linux/wireless.h>
  13. #include <arpa/inet.h>
  14. #include <netinet/in.h>
  15. #include <unistd.h>
  16. #include <stdarg.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #include <termios.h>
  22. #include <errno.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <ctype.h>
  27. #include <ifaddrs.h>
  28. #include <math.h>
  29. #include <limits.h>
  30. #include "define.h"
  31. #include "main.h"
  32. #include "Module_InternalComm.h"
  33. //#define SIMULATION
  34. #define FAIL_SPEC_COMM 100
  35. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  36. #define PASS 1
  37. #define FAIL 0
  38. #define ON 1
  39. #define OFF 0
  40. #define PRIORITY_HIGH 0
  41. #define PRIORITY_LOW 1
  42. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  43. struct StatusCodeData *ShmStatusCodeData;
  44. struct PrimaryMcuData *ShmPrimaryMcuData;
  45. struct OCPP16Data *ShmOCPP16Data;
  46. struct Charger *ShmCharger;
  47. uint16_t stepIndex=21, logIndex;
  48. long long tsLast, tsNow, tsPrintLog[2];
  49. uint64_t tmpPowerConsumption;
  50. uint64_t tmpL1PowerConsumption;
  51. uint64_t tmpL2PowerConsumption;
  52. uint64_t tmpL3PowerConsumption;
  53. void trim(char *s);
  54. int mystrcmp(char *p1,char *p2);
  55. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  56. void split(char **arr, char *str, const char *del);
  57. int StoreLogMsg(const char *fmt, ...)
  58. {
  59. char Buf[4096+256];
  60. char buffer[4096];
  61. time_t CurrentTime;
  62. struct tm *tm;
  63. struct timeval tv;
  64. va_list args;
  65. va_start(args, fmt);
  66. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  67. va_end(args);
  68. memset(Buf,0,sizeof(Buf));
  69. CurrentTime = time(NULL);
  70. tm=localtime(&CurrentTime);
  71. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  72. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s\" >> /Storage/SystemLog/[%04d.%02d]Module_InterCommLog",
  73. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  74. buffer,
  75. tm->tm_year+1900,tm->tm_mon+1);
  76. #ifdef SystemLogMessage
  77. system(Buf);
  78. #endif
  79. #ifdef ConsloePrintLog
  80. printf("[%04d.%02d.%02d %02d:%02d:%02d.%06ld]%s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec, buffer);
  81. #endif
  82. return rc;
  83. }
  84. int DiffTimeb(struct timeb ST, struct timeb ET)
  85. {
  86. //return milli-second
  87. unsigned int StartTime,StopTime;
  88. StartTime=(unsigned int)ST.time;
  89. StopTime=(unsigned int)ET.time;
  90. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  91. }
  92. long long current_timestamp()
  93. {
  94. struct timeval te;
  95. gettimeofday(&te, NULL); // get current time
  96. long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // calculate milliseconds
  97. return milliseconds;
  98. }
  99. //=================================
  100. // Common routine
  101. //=================================
  102. void trim(char *s)
  103. {
  104. int i=0, j, k, l=0;
  105. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  106. i++;
  107. j = strlen(s)-1;
  108. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  109. j--;
  110. if(i==0 && j==strlen(s)-1) { }
  111. else if(i==0) s[j+1] = '\0';
  112. else {
  113. for(k=i; k<=j; k++) s[l++] = s[k];
  114. s[l] = '\0';
  115. }
  116. }
  117. int mystrcmp(char *p1,char *p2)
  118. {
  119. while(*p1==*p2)
  120. {
  121. if(*p1=='\0' || *p2=='\0')
  122. break;
  123. p1++;
  124. p2++;
  125. }
  126. if(*p1=='\0' && *p2=='\0')
  127. return(PASS);
  128. else
  129. return(FAIL);
  130. }
  131. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  132. {
  133. strncpy(dest, src + start, cnt);
  134. dest[cnt] = 0;
  135. }
  136. void split(char **arr, char *str, const char *del)
  137. {
  138. char *s = strtok(str, del);
  139. while(s != NULL)
  140. {
  141. *arr++ = s;
  142. s = strtok(NULL, del);
  143. }
  144. }
  145. //==========================================
  146. // Init all share memory
  147. //==========================================
  148. int InitShareMemory()
  149. {
  150. int result = PASS;
  151. int MeterSMId;
  152. //Initial ShmSysConfigAndInfo
  153. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  154. {
  155. #ifdef SystemLogMessage
  156. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  157. #endif
  158. result = FAIL;
  159. }
  160. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  161. {
  162. #ifdef SystemLogMessage
  163. DEBUG_ERROR("[shmat ShmSysConfigAndInfo NG\n");
  164. #endif
  165. result = FAIL;
  166. }
  167. //Initial ShmStatusCodeData
  168. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  169. {
  170. #ifdef SystemLogMessage
  171. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  172. #endif
  173. result = FAIL;
  174. }
  175. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  176. {
  177. #ifdef SystemLogMessage
  178. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  179. #endif
  180. result = FAIL;
  181. }
  182. //Initial ShmPrimaryMcuKey
  183. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), 0777)) < 0)
  184. {
  185. #ifdef SystemLogMessage
  186. DEBUG_ERROR("shmget ShmPrimaryMcuData NG\n");
  187. #endif
  188. result = FAIL;
  189. }
  190. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  191. {
  192. #ifdef SystemLogMessage
  193. DEBUG_ERROR("shmat ShmPrimaryMcuData NG\n");
  194. #endif
  195. result = FAIL;
  196. }
  197. //creat ShmOCPP16Data
  198. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
  199. {
  200. #ifdef SystemLogMessage
  201. DEBUG_ERROR("shmget ShmOCPP16Data NG");
  202. #endif
  203. result = FAIL;
  204. }
  205. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  206. {
  207. #ifdef SystemLogMessage
  208. DEBUG_ERROR("shmat ShmOCPP16Data NG");
  209. #endif
  210. result = FAIL;
  211. }
  212. else
  213. {}
  214. //Initial ShmCharger
  215. if ((MeterSMId = shmget(ShmChargerKey, sizeof(struct Charger), 0777)) < 0)
  216. {
  217. #ifdef SystemLogMessage
  218. DEBUG_ERROR("shmget ShmChargerKey NG\n");
  219. #endif
  220. result = FAIL;
  221. }
  222. else if ((ShmCharger = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  223. {
  224. #ifdef SystemLogMessage
  225. DEBUG_ERROR("shmat ShmChargerKey NG\n");
  226. #endif
  227. result = FAIL;
  228. }
  229. return result;
  230. }
  231. int InitComPort()
  232. {
  233. int fd;
  234. struct termios tios;
  235. fd = open("/dev/ttyS1", O_RDWR);
  236. if(fd<=0)
  237. {
  238. #ifdef SystemLogMessage
  239. DEBUG_ERROR("open /dev/ttyS1 NG\n");
  240. #endif
  241. return -1;
  242. }
  243. ioctl (fd, TCGETS, &tios);
  244. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  245. tios.c_lflag = 0;
  246. tios.c_iflag = 0;
  247. tios.c_oflag = 0;
  248. tios.c_cc[VMIN]=0;
  249. tios.c_cc[VTIME]=(unsigned char)5; // timeout 0.5 secod
  250. tios.c_lflag=0;
  251. tcflush(fd, TCIFLUSH);
  252. ioctl (fd, TCSETS, &tios);
  253. return fd;
  254. }
  255. void ack_delay(unsigned char cmd)
  256. {
  257. switch(cmd)
  258. {
  259. case CMD_UPDATE_START:
  260. case CMD_UPDATE_END:
  261. usleep(300000);
  262. break;
  263. default:
  264. usleep(100000);
  265. break;
  266. }
  267. }
  268. int tranceive(int fd, unsigned char* cmd, unsigned char cmd_len, unsigned char* rx)
  269. {
  270. int len;
  271. tcflush(fd,TCIOFLUSH);
  272. if(write(fd, cmd, cmd_len) >= cmd_len)
  273. {
  274. ack_delay(cmd[3]);
  275. len = read(fd, rx, 512);
  276. }
  277. else
  278. {
  279. #ifdef SystemLogMessage
  280. DEBUG_ERROR("Serial command %s response fail.\n", cmd);
  281. #endif
  282. }
  283. return len;
  284. }
  285. unsigned char Query_FW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
  286. {
  287. unsigned char result = FAIL;
  288. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_FW_VER, 0x00, 0x00, 0x00};
  289. unsigned char rx[512];
  290. unsigned char chksum = 0x00;
  291. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  292. if(len > 6)
  293. {
  294. if(len < 6+(rx[4] | rx[5]<<8))
  295. return result;
  296. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  297. {
  298. chksum ^= rx[6+idx];
  299. }
  300. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  301. (rx[2] == tx[1]) &&
  302. (rx[1] == tx[2]) &&
  303. (rx[3] == tx[3]))
  304. {
  305. strncpy(Ret_Buf->Version_FW, (char *)rx+6, (rx[4] | rx[5]<<8));
  306. result = PASS;
  307. }
  308. }
  309. return result;
  310. }
  311. unsigned char Query_HW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
  312. {
  313. unsigned char result = FAIL;
  314. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_HW_VER, 0x00, 0x00, 0x00};
  315. unsigned char rx[512];
  316. unsigned char chksum = 0x00;
  317. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  318. if(len > 6)
  319. {
  320. if(len < 6+(rx[4] | rx[5]<<8))
  321. return result;
  322. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  323. {
  324. chksum ^= rx[6+idx];
  325. }
  326. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  327. (rx[2] == tx[1]) &&
  328. (rx[1] == tx[2]) &&
  329. (rx[3] == tx[3]))
  330. {
  331. strncpy(Ret_Buf->Version_HW, (char *)rx+6, (rx[4] | rx[5]<<8));
  332. result = PASS;
  333. }
  334. }
  335. return result;
  336. }
  337. unsigned char Query_Present_InputVoltage(unsigned char fd, unsigned char targetAddr, PresentInputVoltage *Ret_Buf)
  338. {
  339. unsigned char result = FAIL;
  340. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_PRESENT_INPUTVOLTAGE, 0x00, 0x00, 0x00};
  341. unsigned char rx[512];
  342. unsigned char chksum = 0x00;
  343. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  344. if(len > 6)
  345. {
  346. if(len < 6+(rx[4] | rx[5]<<8))
  347. return result;
  348. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  349. {
  350. chksum ^= rx[6+idx];
  351. }
  352. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  353. (rx[2] == tx[1]) &&
  354. (rx[1] == tx[2]) &&
  355. (rx[3] == tx[3]))
  356. {
  357. Ret_Buf->inputType = rx[6];
  358. Ret_Buf->L1N_L12 =( rx[7] | (rx[8]<<8))/10.0;
  359. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  360. {
  361. Ret_Buf->L2N_L23 =( rx[9] | (rx[10]<<8))/10.0;
  362. Ret_Buf->L3N_L31 =( rx[11] | (rx[12]<<8))/10.0;
  363. }
  364. result = PASS;
  365. }
  366. }
  367. return result;
  368. }
  369. unsigned char Query_Present_OutputVoltage(unsigned char fd, unsigned char targetAddr, PresentOutputVoltage *Ret_Buf)
  370. {
  371. unsigned char result = FAIL;
  372. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_PRESENT_OUTPUTVOLTAGE, 0x00, 0x00, 0x00};
  373. unsigned char rx[512];
  374. unsigned char chksum = 0x00;
  375. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  376. if(len > 6)
  377. {
  378. if(len < 6+(rx[4] | rx[5]<<8))
  379. return result;
  380. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  381. {
  382. chksum ^= rx[6+idx];
  383. }
  384. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  385. (rx[2] == tx[1]) &&
  386. (rx[1] == tx[2]) &&
  387. (rx[3] == tx[3]))
  388. {
  389. Ret_Buf->behindFuse_Voltage_C1 =(rx[6] | (rx[7]<<8))/10.0;
  390. Ret_Buf->behindRelay_Voltage_C1 =(rx[8] | (rx[9]<<8))/10.0;
  391. if((rx[4] | rx[5]<<8) > 4)
  392. {
  393. Ret_Buf->behindFuse_Voltage_C2 =(rx[10] | (rx[11]<<8))/10.0;
  394. Ret_Buf->behindRelay_Voltage_C2 =(rx[12] | (rx[13]<<8))/10.0;
  395. }
  396. result = PASS;
  397. }
  398. }
  399. return result;
  400. }
  401. unsigned char Query_Fan_Speed(unsigned char fd, unsigned char targetAddr, FanSpeed *Ret_Buf)
  402. {
  403. unsigned char result = FAIL;
  404. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_FAN_SPEED, 0x00, 0x00, 0x00};
  405. unsigned char rx[512];
  406. unsigned char chksum = 0x00;
  407. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  408. if(len > 6)
  409. {
  410. if(len < 6+(rx[4] | rx[5]<<8))
  411. return result;
  412. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  413. {
  414. chksum ^= rx[6+idx];
  415. }
  416. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  417. (rx[2] == tx[1]) &&
  418. (rx[1] == tx[2]) &&
  419. (rx[3] == tx[3]))
  420. {
  421. for(int idx=0;idx<((rx[4] | rx[5]<<8)>>1);idx++)
  422. Ret_Buf->speed[idx] =(rx[6+(2*idx)] | (rx[6+(2*idx)+1]<<8));
  423. result = PASS;
  424. }
  425. }
  426. return result;
  427. }
  428. unsigned char Query_Temperature(unsigned char fd, unsigned char targetAddr, Temperature *Ret_Buf)
  429. {
  430. unsigned char result = FAIL;
  431. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_TEMPERATURE, 0x00, 0x00, 0x00};
  432. unsigned char rx[512];
  433. unsigned char chksum = 0x00;
  434. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  435. if(len > 6)
  436. {
  437. if(len < 6+(rx[4] | rx[5]<<8))
  438. return result;
  439. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  440. {
  441. chksum ^= rx[6+idx];
  442. }
  443. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  444. (rx[2] == tx[1]) &&
  445. (rx[1] == tx[2]) &&
  446. (rx[3] == tx[3]))
  447. {
  448. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  449. Ret_Buf->point[idx] = rx[6+idx] - 60;
  450. result = PASS;
  451. }
  452. }
  453. return result;
  454. }
  455. unsigned char Query_Aux_PowerVoltage(unsigned char fd, unsigned char targetAddr, AuxPower *Ret_Buf)
  456. {
  457. unsigned char result = FAIL;
  458. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AUX_POWERVOLTAGE, 0x00, 0x00, 0x00};
  459. unsigned char rx[512];
  460. unsigned char chksum = 0x00;
  461. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  462. if(len > 6)
  463. {
  464. if(len < 6+(rx[4] | rx[5]<<8))
  465. return result;
  466. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  467. {
  468. chksum ^= rx[6+idx];
  469. }
  470. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  471. (rx[2] == tx[1]) &&
  472. (rx[1] == tx[2]) &&
  473. (rx[3] == tx[3]))
  474. {
  475. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  476. Ret_Buf->voltage[idx] = rx[6+idx];
  477. result = PASS;
  478. }
  479. }
  480. return result;
  481. }
  482. unsigned char Query_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Ret_Buf)
  483. {
  484. unsigned char result = FAIL;
  485. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_RELAY_OUTPUT, 0x00, 0x00, 0x00};
  486. unsigned char rx[512];
  487. unsigned char chksum = 0x00;
  488. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  489. if(len > 6)
  490. {
  491. if(len < 6+(rx[4] | rx[5]<<8))
  492. return result;
  493. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  494. {
  495. chksum ^= rx[6+idx];
  496. }
  497. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  498. (rx[2] == tx[1]) &&
  499. (rx[1] == tx[2]) &&
  500. (rx[3] == tx[3]))
  501. {
  502. for(int idx_connector=0;idx_connector<(rx[4] | rx[5]<<8);idx_connector++)
  503. {
  504. for(int idx=0;idx<8;idx++)
  505. Ret_Buf->relay_status[idx_connector][idx] = (rx[6+idx_connector]>>idx) & 0x01;
  506. }
  507. result = PASS;
  508. }
  509. }
  510. return result;
  511. }
  512. unsigned char Query_Gfd_Adc(unsigned char fd, unsigned char targetAddr, Gfd *Ret_Buf)
  513. {
  514. unsigned char result = FAIL;
  515. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_GFD_ADC, 0x00, 0x00, 0x00};
  516. unsigned char rx[512];
  517. unsigned char chksum = 0x00;
  518. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  519. if(len > 6)
  520. {
  521. if(len < 6+(rx[4] | rx[5]<<8))
  522. return result;
  523. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  524. {
  525. chksum ^= rx[6+idx];
  526. }
  527. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  528. (rx[2] == tx[1]) &&
  529. (rx[1] == tx[2]) &&
  530. (rx[3] == tx[3]))
  531. {
  532. for(int idx_connector=0;idx_connector<((rx[4] | rx[5]<<8)>>2);idx_connector++)
  533. {
  534. Ret_Buf->adc_value_positive[idx_connector] = (rx[6+(4*idx_connector)] | rx[6+(4*idx_connector)+1]<<8);
  535. Ret_Buf->adc_value_negative[idx_connector] = (rx[6+(4*idx_connector)+2] | rx[6+(4*idx_connector)+3]<<8);;
  536. }
  537. result = PASS;
  538. }
  539. }
  540. return result;
  541. }
  542. unsigned char Query_Gpio_Input(unsigned char fd, unsigned char targetAddr, Gpio_in *Ret_Buf)
  543. {
  544. unsigned char result = FAIL;
  545. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_GPIO_INPUT, 0x00, 0x00, 0x00};
  546. unsigned char rx[512];
  547. unsigned char chksum = 0x00;
  548. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  549. if(len > 6)
  550. {
  551. if(len < 6+(rx[4] | rx[5]<<8))
  552. return result;
  553. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  554. {
  555. chksum ^= rx[6+idx];
  556. }
  557. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  558. (rx[2] == tx[1]) &&
  559. (rx[1] == tx[2]) &&
  560. (rx[3] == tx[3]))
  561. {
  562. Ret_Buf->AC_Connector = (rx[6] >> 0) & 0x01;
  563. Ret_Buf->AC_MainBreaker = (rx[6] >> 1) & 0x01;
  564. Ret_Buf->SPD = (rx[6] >> 2) & 0x01;
  565. Ret_Buf->Door_Open = (rx[6] >> 3) & 0x01;
  566. Ret_Buf->GFD[0] = (rx[6] >> 4) & 0x01;
  567. Ret_Buf->GFD[1] = (rx[6] >> 5) & 0x01;
  568. Ret_Buf->Button[0] = (rx[6] >> 6) & 0x01;
  569. Ret_Buf->Button[1] = (rx[6] >> 7) & 0x01;
  570. Ret_Buf->Button_Emergency = (rx[7] >> 0) & 0x01;
  571. Ret_Buf->Button_Mode_Switch = (rx[7] >> 7) & 0x01;
  572. result = PASS;
  573. }
  574. }
  575. return result;
  576. }
  577. unsigned char Query_Alarm_Log(unsigned char fd, unsigned char targetAddr, Alarm_Log *Ret_Buf)
  578. {
  579. unsigned char result = FAIL;
  580. unsigned char tx[10] = {0xaa, 0x00, targetAddr, CMD_QUERY_ALARM_LOG, 0x03, 0x00, Ret_Buf->logArea, Ret_Buf->alarmIndex&0xff, ((Ret_Buf->alarmIndex>>8)&0xff), 0x00};
  581. unsigned char rx[512];
  582. unsigned char chksum = Ret_Buf->logArea ^ (Ret_Buf->alarmIndex&0xff) ^ ((Ret_Buf->alarmIndex>>8)&0xff);
  583. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  584. if(len > 6)
  585. {
  586. if(len < 6+(rx[4] | rx[5]<<8))
  587. return result;
  588. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  589. {
  590. chksum ^= rx[6+idx];
  591. }
  592. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  593. (rx[2] == tx[1]) &&
  594. (rx[1] == tx[2]) &&
  595. (rx[3] == tx[3]))
  596. {
  597. memcpy(&Ret_Buf->log[0], &rx[8], 8);
  598. result = PASS;
  599. }
  600. }
  601. return result;
  602. }
  603. unsigned char Query_RTC(unsigned char fd, unsigned char targetAddr, Rtc *Ret_Buf)
  604. {
  605. unsigned char result = FAIL;
  606. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_RTC, 0x00, 0x00, 0x00};
  607. unsigned char rx[512];
  608. unsigned char chksum = 0x00;
  609. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  610. if(len > 6)
  611. {
  612. if(len < 6+(rx[4] | rx[5]<<8))
  613. return result;
  614. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  615. {
  616. chksum ^= rx[6+idx];
  617. }
  618. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  619. (rx[2] == tx[1]) &&
  620. (rx[1] == tx[2]) &&
  621. (rx[3] == tx[3]))
  622. {
  623. Ret_Buf->year = ((rx[6]-'0')*1000) + ((rx[7]-'0')*100) + ((rx[8]-'0')*10) + ((rx[9]-'0')*1);
  624. Ret_Buf->month = ((rx[10]-'0')*10) + ((rx[11]-'0')*1);
  625. Ret_Buf->day = ((rx[12]-'0')*10) + ((rx[13]-'0')*1);
  626. Ret_Buf->hour = ((rx[14]-'0')*10) + ((rx[15]-'0')*1);
  627. Ret_Buf->min = ((rx[16]-'0')*10) + ((rx[17]-'0')*1);
  628. Ret_Buf->sec = ((rx[18]-'0')*10) + ((rx[19]-'0')*1);
  629. result = PASS;
  630. }
  631. }
  632. return result;
  633. }
  634. unsigned char Query_AC_MCU_Status(unsigned char fd, unsigned char targetAddr, Ac_Primary_Mcu *Ret_Buf)
  635. {
  636. unsigned char result = FAIL;
  637. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AC_STATUS, 0x00, 0x00, 0x00};
  638. unsigned char rx[512];
  639. unsigned char chksum = 0x00;
  640. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  641. if(len > 6)
  642. {
  643. if(len < 6+(rx[4] | rx[5]<<8))
  644. return result;
  645. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  646. {
  647. chksum ^= rx[6+idx];
  648. }
  649. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  650. (rx[2] == tx[1]) &&
  651. (rx[1] == tx[2]) &&
  652. (rx[3] == tx[3]))
  653. {
  654. Ret_Buf->cp_state = rx[6];
  655. Ret_Buf->current_limit = rx[7] | (rx[8]<<0x08);
  656. Ret_Buf->cp_voltage_positive = (rx[9] | (rx[10]<<0x08))/100.0;
  657. Ret_Buf->cp_voltage_negtive = (rx[11] | (rx[12]<<0x08))/100.0;
  658. Ret_Buf->locker_state = rx[13];
  659. Ret_Buf->relay_state = rx[14];
  660. Ret_Buf->shutter_state = rx[15];
  661. Ret_Buf->meter_state = rx[16];
  662. Ret_Buf->pp_state = rx[17];
  663. Ret_Buf->rating_current = rx[18];
  664. Ret_Buf->rotatory_switch = (rx[19] & 0x0F);
  665. Ret_Buf->socket_e.isSocketEMode = (rx[20] & 0x01);
  666. Ret_Buf->socket_e.isSocketEPinOn = ((rx[20]>>1) & 0x01);
  667. result = PASS;
  668. }
  669. }
  670. return result;
  671. }
  672. unsigned char Query_AC_MCU_Alarm(unsigned char fd, unsigned char targetAddr, Ac_Primary_Mcu_Alarm *Ret_Buf)
  673. {
  674. unsigned char result = FAIL;
  675. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AC_ALARM, 0x00, 0x00, 0x00};
  676. unsigned char rx[512];
  677. unsigned char chksum = 0x00;
  678. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  679. if(len > 6)
  680. {
  681. if(len < 6+(rx[4] | rx[5]<<8))
  682. return result;
  683. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  684. {
  685. chksum ^= rx[6+idx];
  686. }
  687. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  688. (rx[2] == tx[1]) &&
  689. (rx[1] == tx[2]) &&
  690. (rx[3] == tx[3]))
  691. {
  692. //rx[6]
  693. Ret_Buf->bits.OVP_L1 = (((rx[6]>>0)&0x01)?1:0);
  694. Ret_Buf->bits.UVP_L1 = (((rx[6]>>1)&0x01)?1:0);
  695. Ret_Buf->bits.OCP_L1 = (((rx[6]>>2)&0x01)?1:0);
  696. Ret_Buf->bits.OTP = (((rx[6]>>3)&0x01)?1:0);
  697. Ret_Buf->bits.gmi_fault = (((rx[6]>>4)&0x01)?1:0);
  698. Ret_Buf->bits.cp_fault = (((rx[6]>>5)&0x01)?1:0);
  699. Ret_Buf->bits.ac_leak = (((rx[6]>>6)&0x01)?1:0);
  700. Ret_Buf->bits.dc_leak = (((rx[6]>>7)&0x01)?1:0);
  701. //rx[7]
  702. Ret_Buf->bits.mcu_selftest_fail = (((rx[7]>>0)&0x01)?1:0);
  703. Ret_Buf->bits.handshaking_timeout = (((rx[7]>>1)&0x01)?1:0);
  704. Ret_Buf->bits.emergency_stop = (((rx[7]>>2)&0x01)?1:0);
  705. Ret_Buf->bits.relay_welding = (((rx[7]>>3)&0x01)?1:0);
  706. Ret_Buf->bits.leak_module_fail = (((rx[7]>>4)&0x01)?1:0);
  707. Ret_Buf->bits.shutter_fault = (((rx[7]>>5)&0x01)?1:0);
  708. Ret_Buf->bits.locker_fault = (((rx[7]>>6)&0x01)?1:0);
  709. Ret_Buf->bits.power_drop = (((rx[7]>>7)&0x01)?1:0);
  710. //rx[8] bit 3 reserved
  711. Ret_Buf->bits.short_circuit_L1 = (((rx[8]>>0)&0x01)?1:0);
  712. Ret_Buf->bits.rotate_switch_fault = (((rx[8]>>1)&0x01)?1:0);
  713. Ret_Buf->bits.relay_drive_fault = (((rx[8]>>2)&0x01)?1:0);
  714. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  715. {
  716. Ret_Buf->bits.OVP_L2 = (((rx[8]>>4)&0x01)?1:0);
  717. Ret_Buf->bits.OVP_L3 = (((rx[8]>>5)&0x01)?1:0);
  718. Ret_Buf->bits.UVP_L2 = (((rx[8]>>6)&0x01)?1:0);
  719. Ret_Buf->bits.UVP_L3 = (((rx[8]>>7)&0x01)?1:0);
  720. }
  721. //rx[9] bits 6 & 7 Reserved
  722. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  723. {
  724. Ret_Buf->bits.OCP_L2 = (((rx[9]>>0)&0x01)?1:0);
  725. Ret_Buf->bits.OCP_L3 = (((rx[9]>>1)&0x01)?1:0);
  726. Ret_Buf->bits.short_circuit_L2 = (((rx[9]>>2)&0x01)?1:0);
  727. Ret_Buf->bits.short_circuit_L3 = (((rx[9]>>3)&0x01)?1:0);
  728. }
  729. Ret_Buf->bits.meter_comm_timeout = (((rx[9]>>4)&0x01)?1:0);
  730. Ret_Buf->bits.meter_ic_comm_timeout = (((rx[9]>>5)&0x01)?1:0);
  731. Ret_Buf->bits.pilot_negative_error = (((rx[9]>>6)&0x01)?1:0);
  732. result = PASS;
  733. }
  734. }
  735. return result;
  736. }
  737. unsigned char Query_Present_OutputCurrent(unsigned char fd, unsigned char targetAddr, Presentoutputcurrent *Ret_Buf)
  738. {
  739. unsigned char result = FAIL;
  740. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_PRESENT_OUTPUTCURRENT, 0x00, 0x00, 0x00};
  741. unsigned char rx[512];
  742. unsigned char chksum = 0x00;
  743. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  744. if(len > 0)
  745. {
  746. if(len < 6+(rx[4] | rx[5]<<8))
  747. return result;
  748. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  749. {
  750. chksum ^= rx[6+idx];
  751. }
  752. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  753. (rx[2] == tx[1]) &&
  754. (rx[1] == tx[2]) &&
  755. (rx[3] == tx[3]))
  756. {
  757. Ret_Buf->L1N_L12[0] = (rx[6] | (rx[7]<<8))/10.0;
  758. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  759. {
  760. Ret_Buf->L2N_L23[0] = (rx[8] | (rx[9]<<8))/10.0;
  761. Ret_Buf->L3N_L31[0] = (rx[10] | (rx[11]<<8))/10.0;
  762. }
  763. Ret_Buf->L1N_L12[1] = (rx[12] | (rx[13]<<8))/10.0;
  764. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  765. {
  766. Ret_Buf->L2N_L23[1] = (rx[14] | (rx[15]<<8))/10.0;
  767. Ret_Buf->L3N_L31[1] = (rx[16] | (rx[17]<<8))/10.0;
  768. }
  769. result = PASS;
  770. }
  771. }
  772. return result;
  773. }
  774. unsigned char Query_Ble_Config(unsigned char fd, unsigned char targetAddr, Ble_Config_Data *Ret_Buf)
  775. {
  776. unsigned char result = FAIL;
  777. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_BLE_CONFIG_DATA, 0x00, 0x00, 0x00};
  778. unsigned char rx[512];
  779. unsigned char chksum = 0x00;
  780. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  781. if(len > 0)
  782. {
  783. if(len < 6+(rx[4] | rx[5]<<8))
  784. return result;
  785. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  786. {
  787. chksum ^= rx[6+idx];
  788. }
  789. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  790. (rx[2] == tx[1]) &&
  791. (rx[1] == tx[2]) &&
  792. (rx[3] == tx[3]))
  793. {
  794. Ret_Buf->isLogin = (rx[6]?0x01:0x00);
  795. Ret_Buf->isRequestStart = (rx[7]?0x01:0x00);
  796. Ret_Buf->isRequestStop = (rx[8]?0x01:0x00);
  797. result = PASS;
  798. }
  799. }
  800. return result;
  801. }
  802. unsigned char Query_Ble_Central_ID(unsigned char fd, unsigned char targetAddr, Ble_Login_Central_Id *Ret_Buf)
  803. {
  804. unsigned char result = FAIL;
  805. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_BLE_CENTRAL_ID, 0x00, 0x00, 0x00};
  806. unsigned char rx[512];
  807. unsigned char chksum = 0x00;
  808. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  809. if(len > 0)
  810. {
  811. if(len < 6+(rx[4] | rx[5]<<8))
  812. return result;
  813. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  814. {
  815. chksum ^= rx[6+idx];
  816. }
  817. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  818. (rx[2] == tx[1]) &&
  819. (rx[1] == tx[2]) &&
  820. (rx[3] == tx[3]))
  821. {
  822. memset(Ret_Buf->id, 0x00, ARRAY_SIZE(Ret_Buf->id));
  823. memcpy(&Ret_Buf->id[0], &rx[6], (rx[4] | (rx[5]<<8)));
  824. result = PASS;
  825. }
  826. }
  827. return result;
  828. }
  829. unsigned char Query_Power_Consumption(unsigned char fd, unsigned char targetAddr, Power_Consumption *Ret_Buf_T, Power_Consumption *Ret_Buf_L1, Power_Consumption *Ret_Buf_L2, Power_Consumption *Ret_Buf_L3)
  830. {
  831. unsigned char result = FAIL;
  832. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_POWER_CONSUMPTION, 0x00, 0x00, 0x00};
  833. unsigned char rx[512];
  834. unsigned char chksum = 0x00;
  835. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  836. if(len > 0)
  837. {
  838. if(len < 6+(rx[4] | rx[5]<<8))
  839. return result;
  840. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  841. {
  842. chksum ^= rx[6+idx];
  843. }
  844. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  845. (rx[2] == tx[1]) &&
  846. (rx[1] == tx[2]) &&
  847. (rx[3] == tx[3]))
  848. {
  849. #ifndef SIMULATION
  850. Ret_Buf_T-> power_consumption = ((uint64_t)rx[6] | (((uint64_t)rx[7])<<8) | (((uint64_t)rx[8])<<16) | (((uint64_t)rx[9])<<24) | (((uint64_t)rx[10])<<32) | (((uint64_t)rx[11])<<40) | (((uint64_t)rx[12])<<48) | (((uint64_t)rx[13])<<56));
  851. Ret_Buf_L1-> power_consumption = ((uint64_t)rx[14] | (((uint64_t)rx[15])<<8) | (((uint64_t)rx[16])<<16) | (((uint64_t)rx[17])<<24) | (((uint64_t)rx[18])<<32) | (((uint64_t)rx[19])<<40) | (((uint64_t)rx[20])<<48) | (((uint64_t)rx[21])<<56));
  852. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  853. {
  854. Ret_Buf_L2-> power_consumption = ((uint64_t)rx[22] | (((uint64_t)rx[23])<<8) | (((uint64_t)rx[24])<<16) | (((uint64_t)rx[25])<<24) | (((uint64_t)rx[26])<<32) | (((uint64_t)rx[27])<<40) | (((uint64_t)rx[28])<<48) | (((uint64_t)rx[29])<<56));
  855. Ret_Buf_L3-> power_consumption = ((uint64_t)rx[30] | (((uint64_t)rx[31])<<8) | (((uint64_t)rx[32])<<16) | (((uint64_t)rx[33])<<24) | (((uint64_t)rx[34])<<32) | (((uint64_t)rx[35])<<40) | (((uint64_t)rx[36])<<48) | (((uint64_t)rx[37])<<56));
  856. }
  857. #else //SIMULATION
  858. tsNow = current_timestamp();
  859. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  860. {
  861. tmpPowerConsumption += ((uint64_t)((ShmSysConfigAndInfo->SysInfo.AcChargingData[0].PresentChargingVoltage*ShmSysConfigAndInfo->SysInfo.AcChargingData[0].PresentChargingCurrent)*((tsNow-tsLast)/360000.0)) +
  862. (uint64_t)((ShmSysConfigAndInfo->SysInfo.AcChargingData[0].PresentChargingVoltageL2*ShmSysConfigAndInfo->SysInfo.AcChargingData[0].PresentChargingCurrentL2)*((tsNow-tsLast)/360000.0)) +
  863. (uint64_t)((ShmSysConfigAndInfo->SysInfo.AcChargingData[0].PresentChargingVoltageL3*ShmSysConfigAndInfo->SysInfo.AcChargingData[0].PresentChargingCurrentL3)*((tsNow-tsLast)/360000.0)));
  864. tmpL1PowerConsumption += (uint64_t)((ShmSysConfigAndInfo->SysInfo.AcChargingData[0].PresentChargingVoltage*ShmSysConfigAndInfo->SysInfo.AcChargingData[0].PresentChargingCurrent)*((tsNow-tsLast)/360000.0));
  865. tmpL2PowerConsumption += (uint64_t)((ShmSysConfigAndInfo->SysInfo.AcChargingData[0].PresentChargingVoltageL2*ShmSysConfigAndInfo->SysInfo.AcChargingData[0].PresentChargingCurrentL2)*((tsNow-tsLast)/360000.0));
  866. tmpL3PowerConsumption += (uint64_t)((ShmSysConfigAndInfo->SysInfo.AcChargingData[0].PresentChargingVoltageL3*ShmSysConfigAndInfo->SysInfo.AcChargingData[0].PresentChargingCurrentL3)*((tsNow-tsLast)/360000.0));
  867. Ret_Buf_L1-> power_consumption = tmpL1PowerConsumption;
  868. Ret_Buf_L2-> power_consumption = tmpL2PowerConsumption;
  869. Ret_Buf_L3-> power_consumption = tmpL3PowerConsumption;
  870. }
  871. else
  872. {
  873. tmpPowerConsumption += (uint64_t)((ShmSysConfigAndInfo->SysInfo.AcChargingData[0].PresentChargingVoltage*ShmSysConfigAndInfo->SysInfo.AcChargingData[0].PresentChargingCurrent)*((tsNow-tsLast)/360000.0));
  874. }
  875. Ret_Buf_T-> power_consumption = tmpPowerConsumption;
  876. tsLast = tsNow;
  877. #endif //SIMULATION
  878. result = PASS;
  879. }
  880. }
  881. return result;
  882. }
  883. unsigned char Query_MeterIc_CorrectionPara(unsigned char fd, unsigned char targetAddr, MeterIcCorrection *Ret_Buf)
  884. {
  885. unsigned char result = FAIL;
  886. unsigned char tx[8] = {0xaa, 0x00, targetAddr, CMD_QUERY_METER_IC_CORRECTION_PARA, 0x01, 0x00, 0xb0, 0xb0};
  887. unsigned char rx[512];
  888. unsigned char chksum = 0x00;
  889. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  890. chksum ^= tx[6+idx];
  891. tx[7] = chksum;
  892. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  893. if(len > 0)
  894. {
  895. if(len < (6+(rx[4] | rx[5]<<8)))
  896. return result;
  897. chksum = 0x00;
  898. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  899. {
  900. chksum ^= rx[6+idx];
  901. }
  902. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  903. (rx[2] == tx[1]) &&
  904. (rx[1] == tx[2]) &&
  905. (rx[3] == tx[3]) &&
  906. (rx[6]) == 0xb0)
  907. {
  908. uint32_t cali_flag = (rx[7] | (rx[8]<<8) | (rx[9]<<16) | (rx[10]<<24));
  909. if(!(cali_flag & 0x80000000))
  910. {
  911. Ret_Buf->bits.isCalibratedVaGain = cali_flag & (1 << 0) ? 1 : 0;
  912. Ret_Buf->bits.isCalibratedVbGain = cali_flag & (1 << 1) ? 1 : 0;
  913. Ret_Buf->bits.isCalibratedVcGain = cali_flag & (1 << 2) ? 1 : 0;
  914. Ret_Buf->bits.isCalibratedVaOffset = cali_flag & (1 << 3) ? 1 : 0;
  915. Ret_Buf->bits.isCalibratedVbOffset = cali_flag & (1 << 4) ? 1 : 0;
  916. Ret_Buf->bits.isCalibratedVcOffset = cali_flag & (1 << 5) ? 1 : 0;
  917. Ret_Buf->bits.isCalibratedCaGain = cali_flag & (1 << 6) ? 1 : 0;
  918. Ret_Buf->bits.isCalibratedCbGain = cali_flag & (1 << 7) ? 1 : 0;
  919. Ret_Buf->bits.isCalibratedCcGain = cali_flag & (1 << 8) ? 1 : 0;
  920. Ret_Buf->bits.isCalibratedCaOffset = cali_flag & (1 << 9) ? 1 : 0;
  921. Ret_Buf->bits.isCalibratedCbOffset = cali_flag & (1 << 10) ? 1 : 0;
  922. Ret_Buf->bits.isCalibratedCcOffset = cali_flag & (1 << 11) ? 1 : 0;
  923. Ret_Buf->bits.isCalibratedPa = cali_flag & (1 << 12) ? 1 : 0;
  924. Ret_Buf->bits.isCalibratedPb = cali_flag & (1 << 13) ? 1 : 0;
  925. Ret_Buf->bits.isCalibratedPc = cali_flag & (1 << 14) ? 1 : 0;
  926. }
  927. else
  928. {
  929. Ret_Buf->value = 0;
  930. }
  931. result = PASS;
  932. }
  933. }
  934. return result;
  935. }
  936. unsigned char Config_Fan_Speed(unsigned char fd, unsigned char targetAddr, FanSpeed *Set_Buf)
  937. {
  938. unsigned char result = FAIL;
  939. unsigned char tx[15] = {0xaa, 0x00, targetAddr, CMD_CONFIG_FAN_SPEED, 0x08, 0x00, Set_Buf->speed[0]&0xff, (Set_Buf->speed[0]>>8)&0xff, Set_Buf->speed[1]&0xff, (Set_Buf->speed[1]>>8)&0xff, Set_Buf->speed[2]&0xff, (Set_Buf->speed[2]>>8)&0xff, Set_Buf->speed[3]&0xff, (Set_Buf->speed[3]>>8)&0xff, 0x00};
  940. unsigned char rx[512];
  941. unsigned char chksum = 0x00;
  942. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  943. chksum ^= tx[6+idx];
  944. tx[14] = chksum;
  945. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  946. if(len > 6)
  947. {
  948. if(len < 6+(rx[4] | rx[5]<<8))
  949. return result;
  950. chksum = 0x00;
  951. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  952. {
  953. chksum ^= rx[6+idx];
  954. }
  955. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  956. (rx[2] == tx[1]) &&
  957. (rx[1] == tx[2]) &&
  958. (rx[3] == tx[3]) &&
  959. (rx[6] == 0x01))
  960. {
  961. result = PASS;
  962. }
  963. }
  964. return result;
  965. }
  966. unsigned char Config_Serial_Number(unsigned char fd, unsigned char targetAddr, Evse_Id *Set_Buf)
  967. {
  968. unsigned char result = FAIL;
  969. unsigned char tx[27] = {0xaa, 0x00, targetAddr, CMD_CONFIG_SERIAL_NUMBER, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  970. unsigned char rx[512];
  971. unsigned char chksum = 0x00;
  972. memcpy(&tx[14], &Set_Buf->serial_number[0], ARRAY_SIZE(Set_Buf->serial_number));
  973. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  974. chksum ^= tx[6+idx];
  975. tx[26] = chksum;
  976. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  977. if(len > 6)
  978. {
  979. if(len < 6+(rx[4] | rx[5]<<8))
  980. return result;
  981. chksum = 0x00;
  982. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  983. {
  984. chksum ^= rx[6+idx];
  985. }
  986. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  987. (rx[2] == tx[1]) &&
  988. (rx[1] == tx[2]) &&
  989. (rx[3] == tx[3]) &&
  990. (rx[6] == 0x01))
  991. {
  992. result = PASS;
  993. }
  994. }
  995. return result;
  996. }
  997. unsigned char Config_Model_Name(unsigned char fd, unsigned char targetAddr, Evse_Id *Set_Buf)
  998. {
  999. unsigned char result = FAIL;
  1000. unsigned char tx[21] = {0xaa, 0x00, targetAddr, CMD_CONFIG_MODEL_NAME, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  1001. unsigned char rx[512];
  1002. unsigned char chksum = 0x00;
  1003. memcpy(&tx[6], &Set_Buf->model_name[0], ARRAY_SIZE(Set_Buf->model_name));
  1004. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1005. chksum ^= tx[6+idx];
  1006. tx[20] = chksum;
  1007. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1008. if(len > 6)
  1009. {
  1010. if(len < 6+(rx[4] | rx[5]<<8))
  1011. return result;
  1012. chksum = 0x00;
  1013. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1014. {
  1015. chksum ^= rx[6+idx];
  1016. }
  1017. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1018. (rx[2] == tx[1]) &&
  1019. (rx[1] == tx[2]) &&
  1020. (rx[3] == tx[3]) &&
  1021. (rx[6] == 0x01))
  1022. {
  1023. result = PASS;
  1024. }
  1025. }
  1026. return result;
  1027. }
  1028. unsigned char Config_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Set_Buf)
  1029. {
  1030. unsigned char result = FAIL;
  1031. unsigned char tx[15] = {0};
  1032. unsigned char rx[512];
  1033. unsigned char chksum = 0x00;
  1034. tx[0] = 0xaa;
  1035. tx[1] = 0x00;
  1036. tx[2] = targetAddr;
  1037. tx[3] = CMD_CONFIG_RELAY_OUTPUT;
  1038. tx[4] = 0x08;
  1039. tx[5] = 0x00;
  1040. tx[6] = 0x00;
  1041. tx[7] = 0x00;
  1042. tx[8] = 0x00;
  1043. for(int idx_connector=0;idx_connector<2;idx_connector++)
  1044. for(int idx = 0;idx<8;idx++)
  1045. tx[9+idx_connector] |= ((Set_Buf->relay_status[idx_connector][idx]?0x01:0x00)<<idx);
  1046. tx[11] = 0x00;
  1047. tx[12] = 0x00;
  1048. tx[13] = 0x00;
  1049. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1050. chksum ^= tx[6+idx];
  1051. tx[14] = chksum;
  1052. //for(int count = 0; count < ARRAY_SIZE(tx); count++)
  1053. //printf("TX[%d] : %x \n",count, tx[count]);
  1054. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1055. if(len > 6)
  1056. {
  1057. if(len < 6+(rx[4] | rx[5]<<8))
  1058. return result;
  1059. chksum = 0x00;
  1060. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1061. {
  1062. chksum ^= rx[6+idx];
  1063. }
  1064. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1065. (rx[2] == tx[1]) &&
  1066. (rx[1] == tx[2]) &&
  1067. (rx[3] == tx[3]) &&
  1068. (rx[6] == 0x01))
  1069. {
  1070. result = PASS;
  1071. }
  1072. }
  1073. return result;
  1074. }
  1075. unsigned char Config_Gpio_Output(unsigned char fd, unsigned char targetAddr, Gpio_out *Set_Buf)
  1076. {
  1077. unsigned char result = FAIL;
  1078. unsigned char tx[9] = {0xaa, 0x00, targetAddr, CMD_CONFIG_GPIO_OUTPUT, 0x01, 0x00, 0x00, 0x00};
  1079. unsigned char rx[512];
  1080. unsigned char chksum = 0x00;
  1081. tx[6] |= (Set_Buf->AC_Connector?0x01:0x00);
  1082. for(int idx=0;idx<2;idx++)
  1083. tx[6] |= (Set_Buf->Button_LED[idx]?0x01:0x00)<<(1+idx);
  1084. for(int idx=0;idx<4;idx++)
  1085. tx[6] |= (Set_Buf->System_LED[idx]?0x01:0x00)<<(3+idx);
  1086. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1087. chksum ^= tx[6+idx];
  1088. tx[8] = chksum;
  1089. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1090. if(len > 6)
  1091. {
  1092. if(len < 6+(rx[4] | rx[5]<<8))
  1093. return result;
  1094. chksum = 0x00;
  1095. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1096. {
  1097. chksum ^= rx[6+idx];
  1098. }
  1099. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1100. (rx[2] == tx[1]) &&
  1101. (rx[1] == tx[2]) &&
  1102. (rx[3] == tx[3]) &&
  1103. (rx[6] == tx[6]))
  1104. {
  1105. result = PASS;
  1106. }
  1107. }
  1108. return result;
  1109. }
  1110. unsigned char Config_RTC(unsigned char fd, unsigned char targetAddr, Rtc *Set_Buf)
  1111. {
  1112. unsigned char result = FAIL;
  1113. unsigned char tx[21] = {0};
  1114. unsigned char rx[512];
  1115. unsigned char chksum = 0x00;
  1116. tx[0] = 0xaa;
  1117. tx[1] = 0x00;
  1118. tx[2] = targetAddr;
  1119. tx[3] = CMD_CONFIG_RTC;
  1120. tx[4] = 0x0e;
  1121. tx[5] = 0x00;
  1122. tx[6] = ((Set_Buf->year)/1000)+'0';
  1123. tx[7] = ((Set_Buf->year)/100%10)+'0';
  1124. tx[8] = ((Set_Buf->year)/10%10)+'0';
  1125. tx[9] = ((Set_Buf->year)%10)+'0';
  1126. tx[10] = ((Set_Buf->month)/10)+'0';
  1127. tx[11] = ((Set_Buf->month)%10)+'0';
  1128. tx[12] = ((Set_Buf->day)/10)+'0';
  1129. tx[13] = ((Set_Buf->day)%10)+'0';
  1130. tx[14] = ((Set_Buf->hour)/10)+'0';
  1131. tx[15] = ((Set_Buf->hour)%10)+'0';
  1132. tx[16] = ((Set_Buf->min)/10)+'0';
  1133. tx[17] = ((Set_Buf->min)%10)+'0';
  1134. tx[18] = ((Set_Buf->sec)/10)+'0';
  1135. tx[19] = ((Set_Buf->sec)%10)+'0';
  1136. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1137. chksum ^= tx[6+idx];
  1138. tx[20] = chksum;
  1139. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1140. if(len > 6)
  1141. {
  1142. if(len < 6+(rx[4] | rx[5]<<8))
  1143. return result;
  1144. chksum = 0x00;
  1145. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1146. {
  1147. chksum ^= rx[6+idx];
  1148. }
  1149. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1150. (rx[2] == tx[1]) &&
  1151. (rx[1] == tx[2]) &&
  1152. (rx[3] == tx[3]) &&
  1153. (rx[6] == 0x01))
  1154. {
  1155. result = PASS;
  1156. }
  1157. }
  1158. return result;
  1159. }
  1160. unsigned char Config_AC_MCU_LED(unsigned char fd, unsigned char targetAddr, Ac_Primary_Mcu_Led *Set_Buf)
  1161. {
  1162. unsigned char result = FAIL;
  1163. unsigned char tx[12] = {0};
  1164. unsigned char rx[512];
  1165. unsigned char chksum = 0x00;
  1166. tx[0] = 0xaa;
  1167. tx[1] = 0x00;
  1168. tx[2] = targetAddr;
  1169. tx[3] = CMD_CONFIG_AC_LED;
  1170. tx[4] = 0x05;
  1171. tx[5] = 0x00;
  1172. tx[6] = Set_Buf->mode;
  1173. tx[7] = ((Set_Buf->alarm_code>>0)&0xff);
  1174. tx[8] = ((Set_Buf->alarm_code>>8)&0xff);
  1175. tx[9] = ((Set_Buf->alarm_code>>16)&0xff);
  1176. tx[10] = ((Set_Buf->alarm_code>>24)&0xff);
  1177. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1178. chksum ^= tx[6+idx];
  1179. tx[11] = chksum;
  1180. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1181. if(len > 6)
  1182. {
  1183. if(len < 6+(rx[4] | rx[5]<<8))
  1184. return result;
  1185. chksum = 0x00;
  1186. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1187. {
  1188. chksum ^= rx[6+idx];
  1189. }
  1190. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1191. (rx[2] == tx[1]) &&
  1192. (rx[1] == tx[2]) &&
  1193. (rx[3] == tx[3]) &&
  1194. (rx[6] == 0x01))
  1195. {
  1196. result = PASS;
  1197. }
  1198. }
  1199. return result;
  1200. }
  1201. unsigned char Config_AC_MCU_LEGACY_REQUEST(unsigned char fd, unsigned char targetAddr, Legacy_Request *Set_Buf)
  1202. {
  1203. unsigned char result = FAIL;
  1204. unsigned char tx[9] = {0};
  1205. unsigned char rx[512];
  1206. unsigned char chksum = 0x00;
  1207. tx[0] = 0xaa;
  1208. tx[1] = 0x00;
  1209. tx[2] = targetAddr;
  1210. tx[3] = CMD_CONFIG_LEGACY_REQUEST;
  1211. tx[4] = 0x02;
  1212. tx[5] = 0x00;
  1213. tx[6] = Set_Buf->isLegacyRequest;
  1214. tx[7] = 0x00;
  1215. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1216. chksum ^= tx[6+idx];
  1217. tx[8] = chksum;
  1218. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1219. if(len > 6)
  1220. {
  1221. if(len < 6+(rx[4] | rx[5]<<8))
  1222. return result;
  1223. chksum = 0x00;
  1224. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1225. {
  1226. chksum ^= rx[6+idx];
  1227. }
  1228. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1229. (rx[2] == tx[1]) &&
  1230. (rx[1] == tx[2]) &&
  1231. (rx[3] == tx[3]) &&
  1232. (rx[6] == 0x01))
  1233. {
  1234. result = PASS;
  1235. }
  1236. }
  1237. return result;
  1238. }
  1239. unsigned char Config_AC_MCU_RESET_REQUEST(unsigned char fd, unsigned char targetAddr, Mcu_Reset_Request *Set_Buf)
  1240. {
  1241. unsigned char result = FAIL;
  1242. unsigned char tx[9] = {0};
  1243. unsigned char rx[512];
  1244. unsigned char chksum = 0x00;
  1245. tx[0] = 0xaa;
  1246. tx[1] = 0x00;
  1247. tx[2] = targetAddr;
  1248. tx[3] = CMD_CONFIG_MCU_RESET_REQUEST;
  1249. tx[4] = 0x02;
  1250. tx[5] = 0x00;
  1251. tx[6] = Set_Buf->isMcuResetRequest;
  1252. tx[7] = 0x00;
  1253. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1254. chksum ^= tx[6+idx];
  1255. tx[8] = chksum;
  1256. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1257. if(len > 6)
  1258. {
  1259. if(len < 6+(rx[4] | rx[5]<<8))
  1260. return result;
  1261. chksum = 0x00;
  1262. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1263. {
  1264. chksum ^= rx[6+idx];
  1265. }
  1266. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1267. (rx[2] == tx[1]) &&
  1268. (rx[1] == tx[2]) &&
  1269. (rx[3] == tx[3]) &&
  1270. (rx[6] == 0x01))
  1271. {
  1272. result = PASS;
  1273. }
  1274. }
  1275. return result;
  1276. }
  1277. unsigned char Query_AC_GUN_PLUGIN_TIMES(unsigned char fd, unsigned char targetAddr, Gun_Plugin_Times *Ret_Buf)
  1278. {
  1279. unsigned char result = FAIL;
  1280. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_GUN_PLUGIN_TIMES, 0x00, 0x00, 0x00};
  1281. unsigned char rx[512];
  1282. unsigned char chksum = 0x00;
  1283. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1284. if(len > 6)
  1285. {
  1286. if(len < 6+(rx[4] | rx[5]<<8))
  1287. return result;
  1288. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1289. {
  1290. chksum ^= rx[6+idx];
  1291. }
  1292. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1293. (rx[2] == tx[1]) &&
  1294. (rx[1] == tx[2]) &&
  1295. (rx[3] == tx[3]))
  1296. {
  1297. Ret_Buf-> GunPluginTimes = ((uint32_t)rx[6] | (((uint32_t)rx[7])<<8) | (((uint32_t)rx[8])<<16) | (((uint32_t)rx[9])<<24));
  1298. result = PASS;
  1299. }
  1300. }
  1301. return result;
  1302. }
  1303. unsigned char Config_AC_MaxCurrent_And_CpPwmDuty(unsigned char fd, unsigned char targetAddr, Ac_Primary_Mcu_Cp_Pwm_Duty *Set_Buf)
  1304. {
  1305. unsigned char result = FAIL;
  1306. unsigned char tx[8] = {0};
  1307. unsigned char rx[512];
  1308. unsigned char chksum = 0x00;
  1309. tx[0] = 0xaa;
  1310. tx[1] = 0x00;
  1311. tx[2] = targetAddr;
  1312. tx[3] = CMD_CONFIG_CURRENT_LINIT;
  1313. tx[4] = 0x01;
  1314. tx[5] = 0x00;
  1315. tx[6] = Set_Buf->max_current;
  1316. //DEBUG_WARN("Config_AC_MaxCurrent_And_CpPwmDuty...%d\n", Set_Buf->max_current);
  1317. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1318. chksum ^= tx[6+idx];
  1319. tx[7] = chksum;
  1320. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1321. if(len > 6)
  1322. {
  1323. if(len < 6+(rx[4] | rx[5]<<8))
  1324. return result;
  1325. chksum = 0x00;
  1326. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1327. {
  1328. chksum ^= rx[6+idx];
  1329. }
  1330. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1331. (rx[2] == tx[1]) &&
  1332. (rx[1] == tx[2]) &&
  1333. (rx[3] == tx[3]) &&
  1334. ((rx[6] == 0x00) || (rx[6] == 0x01)))
  1335. {
  1336. result = PASS;
  1337. }
  1338. }
  1339. return result;
  1340. }
  1341. unsigned char Config_AC_Set_Breathe_Led_Timing(unsigned char fd, unsigned char targetAddr,Set_Breathe_Led_Timing *Set_Buf)
  1342. {
  1343. unsigned char result = FAIL;
  1344. unsigned char tx[19] = {0};
  1345. unsigned char rx[512];
  1346. unsigned char chksum = 0x00;
  1347. tx[0] = 0xaa;
  1348. tx[1] = 0x00;
  1349. tx[2] = targetAddr;
  1350. tx[3] = CMD_CONFIG_MCU_SET_BREATHE_LED_TIMING;
  1351. tx[4] = 0x0C;
  1352. tx[5] = 0x00;
  1353. // Increase LED_ACTION_CONNECTED
  1354. tx[6] = (Set_Buf->set_Led_Action_Connected_Fade_In & 0xff);
  1355. tx[7] = (Set_Buf->set_Led_Action_Connected_Fade_In >> 8);
  1356. // Decrease LED_ACTION_CONNECTED
  1357. tx[8] = (Set_Buf->set_Led_Action_Connected_Fade_Out & 0xff);
  1358. tx[9] = (Set_Buf->set_Led_Action_Connected_Fade_Out >> 8);
  1359. // Increase LED_ACTION_AUTHED
  1360. tx[10] = (Set_Buf->set_Led_Action_Authed_Fade_In & 0xff);
  1361. tx[11] = (Set_Buf->set_Led_Action_Authed_Fade_In >> 8);
  1362. // Decrease LED_ACTION_AUTHED
  1363. tx[12] = (Set_Buf->set_Led_Action_Authed_Fade_Out & 0xff);
  1364. tx[13] = (Set_Buf->set_Led_Action_Authed_Fade_Out >> 8);
  1365. // Increase_LED_ACTION_CHARGING
  1366. tx[14] = (Set_Buf->Set_Led_Action_Chaging_Fade_In & 0xff);
  1367. tx[15] = (Set_Buf->Set_Led_Action_Chaging_Fade_In >> 8);
  1368. // Decrease_LED_ACTION_CHARGING
  1369. tx[16] = (Set_Buf->set_Led_Action_Chaging_Fade_Out & 0xff);
  1370. tx[17] = (Set_Buf->set_Led_Action_Chaging_Fade_Out >> 8);
  1371. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1372. chksum ^= tx[6+idx];
  1373. tx[18] = chksum;
  1374. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1375. if(len > 6)
  1376. {
  1377. if(len < 6+(rx[4] | rx[5]<<8))
  1378. return result;
  1379. chksum = 0x00;
  1380. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1381. {
  1382. chksum ^= rx[6+idx];
  1383. }
  1384. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1385. (rx[2] == tx[1]) &&
  1386. (rx[1] == tx[2]) &&
  1387. (rx[3] == tx[3]) &&
  1388. (rx[6] == 0x01))
  1389. {
  1390. result = PASS;
  1391. }
  1392. }
  1393. return result;
  1394. }
  1395. unsigned char Config_AC_Set_Led_Brightness(unsigned char fd, unsigned char targetAddr,Set_Led_Brightness *Set_Buf)
  1396. {
  1397. unsigned char result = FAIL;
  1398. unsigned char tx[19] = {0};
  1399. unsigned char rx[512];
  1400. unsigned char chksum = 0x00;
  1401. tx[0] = 0xaa;
  1402. tx[1] = 0x00;
  1403. tx[2] = targetAddr;
  1404. tx[3] = CMD_CONFIG_MCU_SET_LED_BRIGHTNESS;
  1405. tx[4] = 0x0C;
  1406. tx[5] = 0x00;
  1407. tx[6] = Set_Buf-> sector_1; // 0~1 AM and 1~2 AM
  1408. tx[7] = Set_Buf-> sector_2; // 2~3 AM and 3~4 AM
  1409. tx[8] = Set_Buf-> sector_3; // 4~5 AM and 5~6 AM
  1410. tx[9] = Set_Buf-> sector_4; // 6~7 AM and 7~8 AM
  1411. tx[10] = Set_Buf-> sector_5; // 8~9 AM and 9~10 AM
  1412. tx[11] = Set_Buf-> sector_6; // 10~11 AM and 11~12 AM
  1413. tx[12] = Set_Buf-> sector_7; // 12~13 PM and 13~14 PM
  1414. tx[13] = Set_Buf-> sector_8; // 14~15 PM and 15~16 PM
  1415. tx[14] = Set_Buf-> sector_9; // 16~17 PM and 17~18 PM
  1416. tx[15] = Set_Buf-> sector_10; // 18~19 PM and 19~20 PM
  1417. tx[16] = Set_Buf-> sector_11; // 20~21 PM and 21~22 PM
  1418. tx[17] = Set_Buf-> sector_12; // 22~23 PM and 23~24 PM
  1419. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1420. chksum ^= tx[6+idx];
  1421. tx[18] = chksum;
  1422. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1423. if(len > 6)
  1424. {
  1425. if(len < 6+(rx[4] | rx[5]<<8))
  1426. return result;
  1427. chksum = 0x00;
  1428. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1429. {
  1430. chksum ^= rx[6+idx];
  1431. }
  1432. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1433. (rx[2] == tx[1]) &&
  1434. (rx[1] == tx[2]) &&
  1435. (rx[3] == tx[3]) &&
  1436. (rx[6] == 0x01))
  1437. {
  1438. result = PASS;
  1439. }
  1440. }
  1441. return result;
  1442. }
  1443. unsigned char Config_Aux_Power_Switch(unsigned char fd, unsigned char targetAddr, Set_Aux_Power_Switch *Set_Buf)
  1444. {
  1445. unsigned char result = FAIL;
  1446. unsigned char tx[9];
  1447. unsigned char rx[512];
  1448. unsigned char chksum = 0x00;
  1449. tx[0] = 0xaa;
  1450. tx[1] = 0x00;
  1451. tx[2] = targetAddr;
  1452. tx[3] = CMD_CONFIG_AUX_POWER_SWITCH;
  1453. tx[4] = 0x02;
  1454. tx[5] = 0x00;
  1455. tx[6] = Set_Buf->power_type; // 0~1 AM and 1~2 AM
  1456. tx[7] = Set_Buf->power_switch; // 2~3 AM and 3~4 AM
  1457. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1458. chksum ^= tx[6+idx];
  1459. tx[8] = chksum;
  1460. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1461. if(len > 6)
  1462. {
  1463. if(len < 6+(rx[4] | rx[5]<<8))
  1464. return result;
  1465. chksum = 0x00;
  1466. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1467. {
  1468. chksum ^= rx[6+idx];
  1469. }
  1470. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1471. (rx[2] == tx[1]) &&
  1472. (rx[1] == tx[2]) &&
  1473. (rx[3] == tx[3]) &&
  1474. (rx[6] == 0x01))
  1475. {
  1476. result = PASS;
  1477. }
  1478. }
  1479. return result;
  1480. }
  1481. unsigned char Update_Start(unsigned char fd, unsigned char targetAddr, unsigned int crc32)
  1482. {
  1483. unsigned char result = FAIL;
  1484. unsigned char tx[11] = {0xaa, 0x00, targetAddr, CMD_UPDATE_START, 0x04, 0x00, (crc32>>0)&0xff, (crc32>>8)&0xff, (crc32>>16)&0xff, (crc32>>24)&0xff, 0x00};
  1485. unsigned char rx[512];
  1486. unsigned char chksum = 0x00;
  1487. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1488. chksum ^= tx[6+idx];
  1489. tx[10] = chksum;
  1490. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1491. if(len > 6)
  1492. {
  1493. if(len < 6+(rx[4] | rx[5]<<8))
  1494. return result;
  1495. chksum = 0x00;
  1496. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  1497. {
  1498. chksum ^= rx[6+idx];
  1499. }
  1500. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1501. (rx[2] == tx[1]) &&
  1502. (rx[1] == tx[2]) &&
  1503. (rx[3] == tx[3]) &&
  1504. (rx[6] == 0x00))
  1505. {
  1506. result = PASS;
  1507. }
  1508. }
  1509. return result;
  1510. }
  1511. unsigned char Update_Abord(unsigned char fd, unsigned char targetAddr)
  1512. {
  1513. unsigned char result = FAIL;
  1514. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_UPDATE_ABOARD, 0x04, 0x00, 0x00};
  1515. unsigned char rx[512];
  1516. unsigned char chksum = 0x00;
  1517. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1518. if(len > 6)
  1519. {
  1520. if(len < 6+(rx[4] | rx[5]<<8))
  1521. return result;
  1522. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  1523. {
  1524. chksum ^= rx[6+idx];
  1525. }
  1526. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1527. (rx[2] == tx[1]) &&
  1528. (rx[1] == tx[2]) &&
  1529. (rx[3] == tx[3]) &&
  1530. (rx[6] == 0x00))
  1531. {
  1532. result = PASS;
  1533. }
  1534. }
  1535. return result;
  1536. }
  1537. unsigned char Update_Transfer(unsigned char fd, unsigned char targetAddr, unsigned int startAddr, unsigned char *data, unsigned short int length)
  1538. {
  1539. unsigned char result = FAIL;
  1540. unsigned char tx[11 + length];
  1541. unsigned char rx[512];
  1542. unsigned char chksum = 0x00;
  1543. tx[0] = 0xaa;
  1544. tx[1] = 0x00;
  1545. tx[2] = targetAddr;
  1546. tx[3] = CMD_UPDATE_TRANSFER;
  1547. tx[4] = (4 + length) & 0xff;
  1548. tx[5] = ((4 + length)>>8) & 0xff;
  1549. tx[6] = (startAddr>>0) & 0xff;
  1550. tx[7] = (startAddr>>8) & 0xff;
  1551. tx[8] = (startAddr>>16) & 0xff;
  1552. tx[9] = (startAddr>>24) & 0xff;
  1553. memcpy(tx+10, data, length);
  1554. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1555. chksum ^= tx[6+idx];
  1556. tx[ARRAY_SIZE(tx)-1] = chksum;
  1557. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1558. if(len > 6)
  1559. {
  1560. if(len < 6+(rx[4] | rx[5]<<8))
  1561. return result;
  1562. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  1563. {
  1564. chksum ^= rx[6+idx];
  1565. }
  1566. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1567. (rx[2] == tx[1]) &&
  1568. (rx[1] == tx[2]) &&
  1569. (rx[3] == tx[3]) &&
  1570. (rx[6] == 0x00))
  1571. {
  1572. result = PASS;
  1573. }
  1574. }
  1575. return result;
  1576. }
  1577. unsigned char Update_Finish(unsigned char fd, unsigned char targetAddr)
  1578. {
  1579. unsigned char result = FAIL;
  1580. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_UPDATE_END, 0x04, 0x00, 0x00};
  1581. unsigned char rx[512];
  1582. unsigned char chksum = 0x00;
  1583. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1584. if(len > 6)
  1585. {
  1586. if(len < 6+(rx[4] | rx[5]<<8))
  1587. return result;
  1588. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1589. {
  1590. chksum ^= rx[6+idx];
  1591. }
  1592. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1593. (rx[2] == tx[1]) &&
  1594. (rx[1] == tx[2]) &&
  1595. (rx[3] == tx[3]) &&
  1596. (rx[6] == 0x01))
  1597. {
  1598. result = PASS;
  1599. }
  1600. }
  1601. return result;
  1602. }
  1603. //================================================
  1604. // Main process
  1605. //================================================
  1606. int main(void)
  1607. {
  1608. int Uart1Fd;
  1609. unsigned short int failCount[2] = {0,0};
  1610. struct Charger previousCharger;
  1611. if(InitShareMemory() == FAIL)
  1612. {
  1613. #ifdef SystemLogMessage
  1614. DEBUG_ERROR("InitShareMemory NG\n");
  1615. #endif
  1616. if(ShmStatusCodeData!=NULL)
  1617. {
  1618. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  1619. }
  1620. sleep(5);
  1621. return 0;
  1622. }
  1623. else
  1624. {
  1625. DEBUG_INFO("InitShareMemory OK.\n");
  1626. }
  1627. Uart1Fd=InitComPort();
  1628. if(Uart1Fd<0)
  1629. {
  1630. DEBUG_ERROR("InitComPort NG\n");
  1631. if(ShmStatusCodeData!=NULL)
  1632. {
  1633. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1634. }
  1635. sleep(5);
  1636. return 0;
  1637. }
  1638. else
  1639. {
  1640. DEBUG_INFO("ttyS1 port open success.\n");
  1641. }
  1642. if(fork() == 0)
  1643. {
  1644. for(;;)
  1645. {
  1646. for(int gun_index=0;gun_index<AC_QUANTITY;gun_index++)
  1647. {
  1648. //==========================================================
  1649. // High priority polling log print out
  1650. //==========================================================
  1651. //==================================================
  1652. // Case 1: Config primary MCU LED
  1653. //==================================================
  1654. if((previousCharger.gun_info[gun_index].primaryMcuLed.mode != ShmCharger->gun_info[gun_index].primaryMcuLed.mode) ||
  1655. (previousCharger.gun_info[gun_index].primaryMcuLed.alarm_code != ShmCharger->gun_info[gun_index].primaryMcuLed.alarm_code))
  1656. {
  1657. DEBUG_INFO("*******************************************\n");
  1658. DEBUG_INFO("***** High priority polling : Case 1 ******\n");
  1659. DEBUG_INFO("*******************************************\n");
  1660. DEBUG_INFO("MCU-%d set Led mode : %d\n",gun_index, ShmCharger->gun_info[gun_index].primaryMcuLed.mode);
  1661. DEBUG_INFO("MCU-%d set Alarm code : %x\n",gun_index, ShmCharger->gun_info[gun_index].primaryMcuLed.alarm_code);
  1662. previousCharger.gun_info[gun_index].primaryMcuLed.mode = ShmCharger->gun_info[gun_index].primaryMcuLed.mode;
  1663. previousCharger.gun_info[gun_index].primaryMcuLed.alarm_code = ShmCharger->gun_info[gun_index].primaryMcuLed.alarm_code;
  1664. }
  1665. //==================================================
  1666. // Case 2: Config primary Legacy request
  1667. //==================================================
  1668. if(previousCharger.gun_info[gun_index].legacyRequest.isLegacyRequest != ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest)
  1669. {
  1670. DEBUG_INFO("*******************************************\n");
  1671. DEBUG_INFO("***** High priority polling : Case 2 ******\n");
  1672. DEBUG_INFO("*******************************************\n");
  1673. DEBUG_INFO("MCU-%d set PWN request : %d\n", gun_index, ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest);
  1674. previousCharger.gun_info[gun_index].legacyRequest.isLegacyRequest = ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest;
  1675. }
  1676. //==================================================
  1677. // Case 2: Config primary Relay
  1678. //==================================================
  1679. if((previousCharger.gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] != ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0]) ||
  1680. (previousCharger.gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] != ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1]) ||
  1681. (previousCharger.gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] != ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2]) ||
  1682. (previousCharger.gun_info[gun_index].primaryMcuState.relayState.relay_status[0][3] != ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][3]))
  1683. {
  1684. DEBUG_INFO("*******************************************\n");
  1685. DEBUG_INFO("**** High priority polling : Case 2-X *****\n");
  1686. DEBUG_INFO("*******************************************\n");
  1687. DEBUG_INFO("MCU-%d set relay status [0][0] : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0]);
  1688. DEBUG_INFO("MCU-%d set relay status [0][1] : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1]);
  1689. DEBUG_INFO("MCU-%d set relay status [0][2] : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2]);
  1690. DEBUG_INFO("MCU-%d set relay status [0][3] : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][3]);
  1691. previousCharger.gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0];
  1692. previousCharger.gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1];
  1693. previousCharger.gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2];
  1694. previousCharger.gun_info[gun_index].primaryMcuState.relayState.relay_status[0][3] = ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][3];
  1695. }
  1696. //==================================================
  1697. // Case 3: Query primary MCU status
  1698. //==================================================
  1699. if((previousCharger.gun_info[gun_index].primaryMcuState.cp_state != ShmCharger->gun_info[gun_index].primaryMcuState.cp_state) ||
  1700. (previousCharger.gun_info[gun_index].primaryMcuState.current_limit != ShmCharger->gun_info[gun_index].primaryMcuState.current_limit) ||
  1701. (previousCharger.gun_info[gun_index].primaryMcuState.relay_state != ShmCharger->gun_info[gun_index].primaryMcuState.relay_state) ||
  1702. (previousCharger.gun_info[gun_index].primaryMcuState.rotatory_switch != ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch) ||
  1703. (previousCharger.gun_info[gun_index].primaryMcuState.socket_e.isSocketEMode != ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEMode) ||
  1704. (previousCharger.gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn != ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn))
  1705. {
  1706. DEBUG_INFO("*******************************************\n");
  1707. DEBUG_INFO("***** High priority polling : Case 3 ******\n");
  1708. DEBUG_INFO("*******************************************\n");
  1709. DEBUG_INFO("MCU-%d get Pilot State : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.cp_state);
  1710. DEBUG_INFO("MCU-%d get Pilot Duty : %.2f\n", gun_index, (float)ShmCharger->gun_info[gun_index].primaryMcuState.current_limit);
  1711. DEBUG_INFO("MCU-%d get Pilot Voltage Positive : %.2f\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_positive);
  1712. DEBUG_INFO("MCU-%d get Pilot Voltage Negative : %.2f\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_negtive);
  1713. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON)
  1714. {
  1715. if(ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)
  1716. DEBUG_INFO("Relay mode : CHARGING_MODE_SOCKETE. \n");
  1717. else
  1718. DEBUG_INFO("Relay mode : CHARGING_MODE_BS / CHARGING_MODE_HLC. \n");
  1719. }
  1720. DEBUG_INFO("MCU-%d get Relay State : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.relay_state);
  1721. DEBUG_INFO("MCU-%d get Rating Current : %.2f\n", gun_index, (float)ShmCharger->gun_info[gun_index].primaryMcuState.rating_current);
  1722. DEBUG_INFO("MCU-%d get Rotary switch : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch);
  1723. DEBUG_INFO("MCU-%d get is on Socket-E mode : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEMode);
  1724. DEBUG_INFO("MCU-%d get Socket-E detect pin : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn);
  1725. DEBUG_INFO("MCU-%d get Locker State : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.locker_state);
  1726. DEBUG_INFO("MCU-%d get Shutter State : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.shutter_state);
  1727. DEBUG_INFO("MCU-%d get Meter State : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.meter_state);
  1728. DEBUG_INFO("MCU-%d get PP State : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.pp_state);
  1729. previousCharger.gun_info[gun_index].primaryMcuState.cp_state = ShmCharger->gun_info[gun_index].primaryMcuState.cp_state;
  1730. previousCharger.gun_info[gun_index].primaryMcuState.current_limit = ShmCharger->gun_info[gun_index].primaryMcuState.current_limit;
  1731. previousCharger.gun_info[gun_index].primaryMcuState.rotatory_switch = ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch;
  1732. previousCharger.gun_info[gun_index].primaryMcuState.relay_state = ShmCharger->gun_info[gun_index].primaryMcuState.relay_state;
  1733. previousCharger.gun_info[gun_index].primaryMcuState.socket_e.isSocketEMode = ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEMode;
  1734. previousCharger.gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn = ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn;
  1735. }
  1736. //==================================================
  1737. // Case 4: Query primary MCU Alarm code
  1738. //==================================================
  1739. if((previousCharger.gun_info[gun_index].primaryMcuAlarm.InputAlarmCode != ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode))
  1740. {
  1741. if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode>0))
  1742. {
  1743. DEBUG_INFO("*******************************************\n");
  1744. DEBUG_INFO("***** High priority polling : Case 4 ******\n");
  1745. DEBUG_INFO("*******************************************\n");
  1746. DEBUG_INFO("MCU-%d get OVP_L1 : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OVP_L1);
  1747. DEBUG_INFO("MCU-%d get UVP_L1 : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.UVP_L1);
  1748. DEBUG_INFO("MCU-%d get OCP_L1 : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP_L1);
  1749. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  1750. {
  1751. DEBUG_INFO("MCU-%d get OVP_L2 : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OVP_L2);
  1752. DEBUG_INFO("MCU-%d get UVP_L2 : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.UVP_L2);
  1753. DEBUG_INFO("MCU-%d get OCP_L2 : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP_L2);
  1754. DEBUG_INFO("MCU-%d get OVP_L3 : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OVP_L3);
  1755. DEBUG_INFO("MCU-%d get UVP_L3 : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.UVP_L3);
  1756. DEBUG_INFO("MCU-%d get OCP_L3 : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP_L3);
  1757. }
  1758. DEBUG_INFO("MCU-%d get OTP : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OTP);
  1759. DEBUG_INFO("MCU-%d get gmi_fault : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.gmi_fault);
  1760. DEBUG_INFO("MCU-%d get cp_fault : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.cp_fault);
  1761. DEBUG_INFO("MCU-%d get ac_leak : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.ac_leak);
  1762. DEBUG_INFO("MCU-%d get dc_leak : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.dc_leak);
  1763. DEBUG_INFO("MCU-%d get mcu_selftest_fail : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.mcu_selftest_fail);
  1764. DEBUG_INFO("MCU-%d get handshaking_timeout : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.handshaking_timeout);
  1765. DEBUG_INFO("MCU-%d get emergency_stop : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.emergency_stop);
  1766. DEBUG_INFO("MCU-%d get relay_welding : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.relay_welding);
  1767. DEBUG_INFO("MCU-%d get leak_module_fail : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.leak_module_fail);
  1768. DEBUG_INFO("MCU-%d get shutter_fault : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.shutter_fault);
  1769. DEBUG_INFO("MCU-%d get locker_fault : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.locker_fault);
  1770. DEBUG_INFO("MCU-%d get power_drop : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.power_drop);
  1771. DEBUG_INFO("MCU-%d get rotate_switch_fault : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.rotate_switch_fault);
  1772. DEBUG_INFO("MCU-%d get short_circuit_L1 : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.short_circuit_L1);
  1773. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  1774. {
  1775. DEBUG_INFO("MCU-%d get short_circuit_L2 : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.short_circuit_L2);
  1776. DEBUG_INFO("MCU-%d get short_circuit_L3 : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.short_circuit_L3);
  1777. }
  1778. DEBUG_INFO("MCU-%d get relay_drive_fault : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.relay_drive_fault);
  1779. DEBUG_INFO("MCU-%d get meter_comm_timeout : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.meter_comm_timeout);
  1780. DEBUG_INFO("MCU-%d get meter_ic_comm_timeout : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.meter_ic_comm_timeout);
  1781. DEBUG_INFO("MCU-%d get pilot_negative_error : %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.pilot_negative_error);
  1782. DEBUG_INFO("MCU-%d get InputAlarmCode : %x\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode);
  1783. }
  1784. previousCharger.gun_info[gun_index].primaryMcuAlarm.InputAlarmCode = ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode;
  1785. }
  1786. //==================================================
  1787. // Case 5: Query primary MCU BLE config
  1788. //==================================================
  1789. /*
  1790. if((previousCharger.gun_info[gun_index].bleConfigData.isLogin != ShmCharger->gun_info[gun_index].bleConfigData.isLogin) ||
  1791. (previousCharger.gun_info[gun_index].bleConfigData.isRequestStart != ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart) ||
  1792. (previousCharger.gun_info[gun_index].bleConfigData.isRequestStop != ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop))
  1793. {
  1794. if(ShmCharger->gun_info[gun_index].bleConfigData.isLogin == ON)
  1795. {
  1796. DEBUG_INFO("*******************************************\n");
  1797. DEBUG_INFO("***** High priority polling : Case 5 ******\n");
  1798. DEBUG_INFO("*******************************************\n");
  1799. DEBUG_INFO("MCU-%d get isUserLogin : %d\n", gun_index, ShmCharger->gun_info[gun_index].bleConfigData.isLogin);
  1800. DEBUG_INFO("MCU-%d get isRequestStartCharger : %d\n", gun_index, ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart);
  1801. DEBUG_INFO("MCU-%d get isRequestStopCharger : %d\n", gun_index, ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop);
  1802. }
  1803. previousCharger.gun_info[gun_index].bleConfigData.isLogin = ShmCharger->gun_info[gun_index].bleConfigData.isLogin;
  1804. previousCharger.gun_info[gun_index].bleConfigData.isRequestStart = ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart;
  1805. previousCharger.gun_info[gun_index].bleConfigData.isRequestStop = ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop;
  1806. }
  1807. */
  1808. //==================================================
  1809. // Case 6: Query primary MCU ble login id
  1810. //==================================================
  1811. /*
  1812. if((strcmp((char *)&previousCharger.gun_info[gun_index].bleLoginCentralId.id,(char *)&ShmCharger->gun_info[gun_index].bleLoginCentralId.id) != 0))
  1813. {
  1814. if(strcmp((char *)&ShmCharger->gun_info[gun_index].bleLoginCentralId.id,"") != 0)
  1815. {
  1816. DEBUG_INFO("*******************************************\n");
  1817. DEBUG_INFO("***** High priority polling : Case 6 ******\n");
  1818. DEBUG_INFO("*******************************************\n");
  1819. DEBUG_INFO("MCU-%d get ble central id : %s\n", gun_index, ShmCharger->gun_info[gun_index].bleLoginCentralId.id);
  1820. }
  1821. memcpy(&previousCharger.gun_info[gun_index].bleLoginCentralId.id, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, ARRAY_SIZE(ShmCharger->gun_info[gun_index].bleLoginCentralId.id));
  1822. }
  1823. */
  1824. //==================================================
  1825. // Case 8: Config primary MCU duty
  1826. //==================================================
  1827. if(previousCharger.gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  1828. {
  1829. DEBUG_INFO("*******************************************\n");
  1830. DEBUG_INFO("***** High priority polling : Case 8 ******\n");
  1831. DEBUG_INFO("*******************************************\n");
  1832. DEBUG_INFO("MCU-%d set cp pwn duty : %d\n",gun_index, ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
  1833. previousCharger.gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current;
  1834. }
  1835. //==================================================
  1836. // Case 10: Query primary MCU power consumption
  1837. //==================================================
  1838. if((abs(ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption - previousCharger.gun_info[gun_index].powerConsumptionTotal.power_consumption) > 200) ||
  1839. (abs(ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption - previousCharger.gun_info[gun_index].powerConsumption[0].power_consumption) > 200) ||
  1840. (abs(ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption - previousCharger.gun_info[gun_index].powerConsumption[1].power_consumption) > 200) ||
  1841. (abs(ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption - previousCharger.gun_info[gun_index].powerConsumption[2].power_consumption) > 200))
  1842. {
  1843. DEBUG_INFO("*******************************************\n");
  1844. DEBUG_INFO("***** High priority polling : Case 10 *****\n");
  1845. DEBUG_INFO("*******************************************\n");
  1846. DEBUG_INFO("MCU-%d get total power consumption : %f kWh\n",gun_index, ((float)ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/10000.0));
  1847. DEBUG_INFO("MCU-%d get L1N_L12 power consumption : %f kWh\n",gun_index, ((float)ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/10000.0));
  1848. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  1849. {
  1850. DEBUG_INFO("MCU-%d get L2N_L23 power consumption : %f kWh\n",gun_index, ((float)ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/10000.0));
  1851. DEBUG_INFO("MCU-%d get L3N_L31 power consumption : %f kWh\n",gun_index, ((float)ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/10000.0));
  1852. }
  1853. previousCharger.gun_info[gun_index].powerConsumptionTotal.power_consumption = ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption;
  1854. previousCharger.gun_info[gun_index].powerConsumption[0].power_consumption = ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption;
  1855. previousCharger.gun_info[gun_index].powerConsumption[1].power_consumption = ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption;
  1856. previousCharger.gun_info[gun_index].powerConsumption[2].power_consumption = ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption;
  1857. }
  1858. //==================================================
  1859. // Case 11: Query primary Out put current config
  1860. //==================================================
  1861. if((abs((int)ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0] - (int)previousCharger.gun_info[gun_index].outputCurrent.L1N_L12[0]) >= 2) ||
  1862. (abs((int)ShmCharger->gun_info[gun_index].outputCurrent.L2N_L23[0] - (int)previousCharger.gun_info[gun_index].outputCurrent.L2N_L23[0]) >= 2) ||
  1863. (abs((int)ShmCharger->gun_info[gun_index].outputCurrent.L3N_L31[0] - (int)previousCharger.gun_info[gun_index].outputCurrent.L3N_L31[0]) >= 2))
  1864. {
  1865. DEBUG_INFO("*******************************************\n");
  1866. DEBUG_INFO("***** High priority polling : Case 11 *****\n");
  1867. DEBUG_INFO("*******************************************\n");
  1868. DEBUG_INFO("MCU-%d get output current L1: %f\n", gun_index, (float)ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0]);
  1869. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  1870. {
  1871. DEBUG_INFO("MCU-%d get output current L2: %f\n", gun_index, (float)ShmCharger->gun_info[gun_index].outputCurrent.L2N_L23[0]);
  1872. DEBUG_INFO("MCU-%d get output current L3: %f\n", gun_index, (float)ShmCharger->gun_info[gun_index].outputCurrent.L3N_L31[0]);
  1873. }
  1874. previousCharger.gun_info[gun_index].outputCurrent.L1N_L12[0] = ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0];
  1875. previousCharger.gun_info[gun_index].outputCurrent.L2N_L23[0] = ShmCharger->gun_info[gun_index].outputCurrent.L2N_L23[0];
  1876. previousCharger.gun_info[gun_index].outputCurrent.L3N_L31[0] = ShmCharger->gun_info[gun_index].outputCurrent.L3N_L31[0];
  1877. }
  1878. //==========================================================
  1879. // Low priority polling log print out
  1880. //==========================================================
  1881. //==================================================
  1882. // Case 1: Query primary In put voltage
  1883. //==================================================
  1884. if((abs((int)ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12 - (int)previousCharger.gun_info[gun_index].inputVoltage.L1N_L12) >= 2) ||
  1885. (abs((int)ShmCharger->gun_info[gun_index].inputVoltage.L2N_L23 - (int)previousCharger.gun_info[gun_index].inputVoltage.L2N_L23) >= 2) ||
  1886. (abs((int)ShmCharger->gun_info[gun_index].inputVoltage.L3N_L31 - (int)previousCharger.gun_info[gun_index].inputVoltage.L3N_L31) >= 2))
  1887. {
  1888. DEBUG_INFO("===========================================\n");
  1889. DEBUG_INFO("==== Normal priority polling : Case 1 =====\n");
  1890. DEBUG_INFO("===========================================\n");
  1891. DEBUG_INFO("MCU-%d get Input voltage L1: %.2f\n", gun_index, (float)ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12);
  1892. DEBUG_INFO("MCU-%d get PresentChargingVoltage L1: %.2f\n", gun_index, (float)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltage);
  1893. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  1894. {
  1895. DEBUG_INFO("MCU-%d get Input voltage L2: %f\n", gun_index, (float)ShmCharger->gun_info[gun_index].inputVoltage.L2N_L23);
  1896. DEBUG_INFO("MCU-%d get PresentChargingVoltage L2: %.2f\n", gun_index, (float)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltageL2);
  1897. DEBUG_INFO("MCU-%d get Input voltage L3: %f\n", gun_index, (float)ShmCharger->gun_info[gun_index].inputVoltage.L3N_L31);
  1898. DEBUG_INFO("MCU-%d get PresentChargingVoltage L3: %.2f\n", gun_index, (float)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltageL3);
  1899. }
  1900. previousCharger.gun_info[gun_index].inputVoltage.L1N_L12 = ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12;
  1901. previousCharger.gun_info[gun_index].inputVoltage.L2N_L23 = ShmCharger->gun_info[gun_index].inputVoltage.L2N_L23;
  1902. previousCharger.gun_info[gun_index].inputVoltage.L3N_L31 = ShmCharger->gun_info[gun_index].inputVoltage.L3N_L31;
  1903. }
  1904. //==================================================
  1905. // Case 5: Query primary plug in times
  1906. //==================================================
  1907. if(previousCharger.gun_info[gun_index].gunPluginTimes.GunPluginTimes != ShmCharger->gun_info[gun_index].gunPluginTimes.GunPluginTimes)
  1908. {
  1909. DEBUG_INFO("===========================================\n");
  1910. DEBUG_INFO("==== Normal priority polling : Case 5 =====\n");
  1911. DEBUG_INFO("===========================================\n");
  1912. DEBUG_INFO("MCU-%d get gun plugin times : %ld\n", gun_index, (long)ShmCharger->gun_info[gun_index].gunPluginTimes.GunPluginTimes);
  1913. previousCharger.gun_info[gun_index].gunPluginTimes.GunPluginTimes = ShmCharger->gun_info[gun_index].gunPluginTimes.GunPluginTimes;
  1914. }
  1915. //==================================================
  1916. // Case 7: Query primary temperature
  1917. //==================================================
  1918. if(previousCharger.gun_info[gun_index].temperature.point[0] != ShmCharger->gun_info[gun_index].temperature.point[0])
  1919. {
  1920. DEBUG_INFO("===========================================\n");
  1921. DEBUG_INFO("==== Normal priority polling : Case 7 =====\n");
  1922. DEBUG_INFO("===========================================\n");
  1923. DEBUG_INFO("MCU-%d get temperature : %d\n", gun_index, ShmCharger->gun_info[gun_index].temperature.point[0]);
  1924. previousCharger.gun_info[gun_index].temperature.point[0] = ShmCharger->gun_info[gun_index].temperature.point[0];
  1925. }
  1926. //==================================================
  1927. // Case 11: Config primary RTC time
  1928. //==================================================
  1929. if(previousCharger.gun_info[gun_index].rtc.min != ShmCharger->gun_info[gun_index].rtc.min)
  1930. {
  1931. DEBUG_INFO("===========================================\n");
  1932. DEBUG_INFO("==== Normal priority polling : Case 11 ====\n");
  1933. DEBUG_INFO("===========================================\n");
  1934. if(ShmCharger->gun_info[gun_index].bleConfigData.isLogin && !ShmOCPP16Data->OcppConnStatus)
  1935. {
  1936. DEBUG_INFO("Sync from MCU-%d rtc OK...%04d-%02d-%02d %02d:%02d:%02d\n", gun_index,
  1937. ShmCharger->gun_info[gun_index].rtc.year,
  1938. ShmCharger->gun_info[gun_index].rtc.month,
  1939. ShmCharger->gun_info[gun_index].rtc.day,
  1940. ShmCharger->gun_info[gun_index].rtc.hour,
  1941. ShmCharger->gun_info[gun_index].rtc.min,
  1942. ShmCharger->gun_info[gun_index].rtc.sec);
  1943. }
  1944. else
  1945. {
  1946. DEBUG_INFO("MCU-%d set rtc OK...%04d-%02d-%02d %02d:%02d:%02d\n", gun_index,
  1947. ShmCharger->gun_info[gun_index].rtc.year,
  1948. ShmCharger->gun_info[gun_index].rtc.month,
  1949. ShmCharger->gun_info[gun_index].rtc.day,
  1950. ShmCharger->gun_info[gun_index].rtc.hour,
  1951. ShmCharger->gun_info[gun_index].rtc.min,
  1952. ShmCharger->gun_info[gun_index].rtc.sec);
  1953. }
  1954. previousCharger.gun_info[gun_index].rtc.min = ShmCharger->gun_info[gun_index].rtc.min;
  1955. }
  1956. //==================================================
  1957. // Case 15: Config led breathe timing
  1958. //==================================================
  1959. if((previousCharger.gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Authed_Fade_In != ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Authed_Fade_In) ||
  1960. (previousCharger.gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Authed_Fade_Out != ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Authed_Fade_Out) ||
  1961. (previousCharger.gun_info[gun_index].setBreatheLedTiming.Set_Led_Action_Chaging_Fade_In != ShmCharger->gun_info[gun_index].setBreatheLedTiming.Set_Led_Action_Chaging_Fade_In) ||
  1962. (previousCharger.gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Chaging_Fade_Out != ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Chaging_Fade_Out) ||
  1963. (previousCharger.gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Connected_Fade_In != ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Connected_Fade_In) ||
  1964. (previousCharger.gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Connected_Fade_Out != ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Connected_Fade_Out))
  1965. {
  1966. if(ShmCharger->gun_info[gun_index].isSetBreatheLedTiming == ON)
  1967. {
  1968. DEBUG_INFO("===========================================\n");
  1969. DEBUG_INFO("==== Normal priority polling : Case 15 ====\n");
  1970. DEBUG_INFO("===========================================\n");
  1971. DEBUG_INFO("MCU-%d set breathe led timing : Authed Fade in [%ld].\n", gun_index, (long)ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Authed_Fade_In);
  1972. DEBUG_INFO("MCU-%d set breathe led timing : Authed Fade out [%ld].\n", gun_index, (long)ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Authed_Fade_Out);
  1973. DEBUG_INFO("MCU-%d set breathe led timing : Charging Fade in [%ld].\n", gun_index, (long)ShmCharger->gun_info[gun_index].setBreatheLedTiming.Set_Led_Action_Chaging_Fade_In);
  1974. DEBUG_INFO("MCU-%d set breathe led timing : Charging Fade out [%ld].\n", gun_index, (long)ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Chaging_Fade_Out);
  1975. DEBUG_INFO("MCU-%d set breathe led timing : Connected Fade in [%ld].\n", gun_index, (long)ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Connected_Fade_In);
  1976. DEBUG_INFO("MCU-%d set breathe led timing : Connected Fade out [%ld].\n", gun_index, (long)ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Connected_Fade_Out);
  1977. }
  1978. previousCharger.gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Authed_Fade_In = ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Authed_Fade_In;
  1979. previousCharger.gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Authed_Fade_Out = ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Authed_Fade_Out;
  1980. previousCharger.gun_info[gun_index].setBreatheLedTiming.Set_Led_Action_Chaging_Fade_In = ShmCharger->gun_info[gun_index].setBreatheLedTiming.Set_Led_Action_Chaging_Fade_In;
  1981. previousCharger.gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Chaging_Fade_Out = ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Chaging_Fade_Out;
  1982. previousCharger.gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Connected_Fade_In = ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Connected_Fade_In;
  1983. previousCharger.gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Connected_Fade_Out = ShmCharger->gun_info[gun_index].setBreatheLedTiming.set_Led_Action_Connected_Fade_Out;
  1984. }
  1985. //==================================================
  1986. // Case 17: Config led brightness
  1987. //==================================================
  1988. if((previousCharger.gun_info[gun_index].setLedBrightness.sector_1 != ShmCharger->gun_info[gun_index].setLedBrightness.sector_1) ||
  1989. (previousCharger.gun_info[gun_index].setLedBrightness.sector_2 != ShmCharger->gun_info[gun_index].setLedBrightness.sector_2) ||
  1990. (previousCharger.gun_info[gun_index].setLedBrightness.sector_3 != ShmCharger->gun_info[gun_index].setLedBrightness.sector_3) ||
  1991. (previousCharger.gun_info[gun_index].setLedBrightness.sector_4 != ShmCharger->gun_info[gun_index].setLedBrightness.sector_4) ||
  1992. (previousCharger.gun_info[gun_index].setLedBrightness.sector_5 != ShmCharger->gun_info[gun_index].setLedBrightness.sector_5) ||
  1993. (previousCharger.gun_info[gun_index].setLedBrightness.sector_6 != ShmCharger->gun_info[gun_index].setLedBrightness.sector_6) ||
  1994. (previousCharger.gun_info[gun_index].setLedBrightness.sector_7 != ShmCharger->gun_info[gun_index].setLedBrightness.sector_7) ||
  1995. (previousCharger.gun_info[gun_index].setLedBrightness.sector_8 != ShmCharger->gun_info[gun_index].setLedBrightness.sector_8) ||
  1996. (previousCharger.gun_info[gun_index].setLedBrightness.sector_9 != ShmCharger->gun_info[gun_index].setLedBrightness.sector_9) ||
  1997. (previousCharger.gun_info[gun_index].setLedBrightness.sector_10 != ShmCharger->gun_info[gun_index].setLedBrightness.sector_10) ||
  1998. (previousCharger.gun_info[gun_index].setLedBrightness.sector_11 != ShmCharger->gun_info[gun_index].setLedBrightness.sector_11) ||
  1999. (previousCharger.gun_info[gun_index].setLedBrightness.sector_12 != ShmCharger->gun_info[gun_index].setLedBrightness.sector_12))
  2000. {
  2001. if(ShmCharger->gun_info[gun_index].isSetLedBrightness == ON)
  2002. {
  2003. DEBUG_INFO("===========================================\n");
  2004. DEBUG_INFO("==== Normal priority polling : Case 17 ====\n");
  2005. DEBUG_INFO("===========================================\n");
  2006. DEBUG_INFO("MCU-%d set led brightness Sector 01 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_1);
  2007. DEBUG_INFO("MCU-%d set led brightness Sector 02 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_2);
  2008. DEBUG_INFO("MCU-%d set led brightness Sector 03 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_3);
  2009. DEBUG_INFO("MCU-%d set led brightness Sector 04 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_4);
  2010. DEBUG_INFO("MCU-%d set led brightness Sector 05 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_5);
  2011. DEBUG_INFO("MCU-%d set led brightness Sector 06 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_6);
  2012. DEBUG_INFO("MCU-%d set led brightness Sector 07 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_7);
  2013. DEBUG_INFO("MCU-%d set led brightness Sector 08 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_8);
  2014. DEBUG_INFO("MCU-%d set led brightness Sector 09 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_9);
  2015. DEBUG_INFO("MCU-%d set led brightness Sector 10 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_10);
  2016. DEBUG_INFO("MCU-%d set led brightness Sector 11 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_11);
  2017. DEBUG_INFO("MCU-%d set led brightness Sector 12 : [%x].\n", gun_index, ShmCharger->gun_info[gun_index].setLedBrightness.sector_12);
  2018. }
  2019. previousCharger.gun_info[gun_index].setLedBrightness.sector_1 = ShmCharger->gun_info[gun_index].setLedBrightness.sector_1;
  2020. previousCharger.gun_info[gun_index].setLedBrightness.sector_2 = ShmCharger->gun_info[gun_index].setLedBrightness.sector_2;
  2021. previousCharger.gun_info[gun_index].setLedBrightness.sector_3 = ShmCharger->gun_info[gun_index].setLedBrightness.sector_3;
  2022. previousCharger.gun_info[gun_index].setLedBrightness.sector_4 = ShmCharger->gun_info[gun_index].setLedBrightness.sector_4;
  2023. previousCharger.gun_info[gun_index].setLedBrightness.sector_5 = ShmCharger->gun_info[gun_index].setLedBrightness.sector_5;
  2024. previousCharger.gun_info[gun_index].setLedBrightness.sector_6 = ShmCharger->gun_info[gun_index].setLedBrightness.sector_6;
  2025. previousCharger.gun_info[gun_index].setLedBrightness.sector_7 = ShmCharger->gun_info[gun_index].setLedBrightness.sector_7;
  2026. previousCharger.gun_info[gun_index].setLedBrightness.sector_8 = ShmCharger->gun_info[gun_index].setLedBrightness.sector_8;
  2027. previousCharger.gun_info[gun_index].setLedBrightness.sector_9 = ShmCharger->gun_info[gun_index].setLedBrightness.sector_9;
  2028. previousCharger.gun_info[gun_index].setLedBrightness.sector_10 = ShmCharger->gun_info[gun_index].setLedBrightness.sector_10;
  2029. previousCharger.gun_info[gun_index].setLedBrightness.sector_11 = ShmCharger->gun_info[gun_index].setLedBrightness.sector_11;
  2030. previousCharger.gun_info[gun_index].setLedBrightness.sector_12 = ShmCharger->gun_info[gun_index].setLedBrightness.sector_12;
  2031. }
  2032. }
  2033. usleep(100000);
  2034. }
  2035. exit(0);
  2036. }
  2037. for(;;)
  2038. {
  2039. for(int gun_index=0;gun_index<AC_QUANTITY;gun_index++)
  2040. {
  2041. /*
  2042. * Polling loop
  2043. */
  2044. if((stepIndex%2)==0)
  2045. {
  2046. /*
  2047. * High priority communication
  2048. */
  2049. //===============================
  2050. // Case 1 : Config primary MCU LED
  2051. //===============================
  2052. ShmCharger->gun_info[gun_index].primaryMcuLed.alarm_code = ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode;
  2053. if(Config_AC_MCU_LED(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].primaryMcuLed))
  2054. {
  2055. failCount[gun_index] = 0;
  2056. }
  2057. else
  2058. {
  2059. DEBUG_WARN("MCU-%d set led fail...%d\n", gun_index, failCount[gun_index]);
  2060. if(failCount[gun_index]<USHRT_MAX)
  2061. failCount[gun_index]++;
  2062. else
  2063. failCount[gun_index] = FAIL_SPEC_COMM;
  2064. }
  2065. //===============================
  2066. // Case 2 : Config primary Legacy request
  2067. //===============================
  2068. if(Config_AC_MCU_LEGACY_REQUEST(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1),&ShmCharger->gun_info[gun_index].legacyRequest))
  2069. {
  2070. failCount[gun_index] = 0;
  2071. }
  2072. else
  2073. {
  2074. DEBUG_WARN("MCU-%d set PWN request fail...%d\n", gun_index, failCount[gun_index]);
  2075. if(failCount[gun_index]<USHRT_MAX)
  2076. failCount[gun_index]++;
  2077. else
  2078. failCount[gun_index] = FAIL_SPEC_COMM;
  2079. }
  2080. //===============================
  2081. // Case 2-X : Config primary Relay
  2082. //===============================
  2083. if(Config_Relay_Output(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1),&ShmCharger->gun_info[gun_index].primaryMcuState.relayState))
  2084. {
  2085. failCount[gun_index] = 0;
  2086. }
  2087. else
  2088. {
  2089. DEBUG_WARN("MCU-%d set relay fail...%d\n", gun_index, failCount[gun_index]);
  2090. if(failCount[gun_index]<USHRT_MAX)
  2091. failCount[gun_index]++;
  2092. else
  2093. failCount[gun_index] = FAIL_SPEC_COMM;
  2094. }
  2095. //===============================
  2096. // Case 3 : Query primary MCU status
  2097. //===============================
  2098. if(Query_AC_MCU_Status(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].primaryMcuState))
  2099. {
  2100. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState = ShmCharger->gun_info[gun_index].primaryMcuState.cp_state;
  2101. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotDuty = (ShmCharger->gun_info[gun_index].primaryMcuState.current_limit>51?(unsigned char)((ShmCharger->gun_info[gun_index].primaryMcuState.current_limit/2.5)+64):(unsigned char)(ShmCharger->gun_info[gun_index].primaryMcuState.current_limit/0.6));
  2102. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotVoltage = ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_positive;
  2103. ShmCharger->gun_info[gun_index].PilotVoltage.PilotVoltagePositive = ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_positive;
  2104. ShmCharger->gun_info[gun_index].PilotVoltage.PilotVoltageNegative = ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_negtive;
  2105. //pass info 2 CCS task
  2106. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_UNKNOWN)
  2107. {
  2108. if(ShmCharger->gun_info[gun_index].PilotVoltage.PilotVoltageNegative >= -12)
  2109. {
  2110. ShmCharger->gun_info[gun_index].acCcsInfo.CpPresentState = CCS_CP_STATE_G;
  2111. }
  2112. else
  2113. {
  2114. ShmCharger->gun_info[gun_index].acCcsInfo.CpPresentState = CCS_CP_STATE_H;
  2115. }
  2116. }
  2117. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  2118. {
  2119. ShmCharger->gun_info[gun_index].acCcsInfo.CpPresentState = CCS_CP_STATE_A;
  2120. }
  2121. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)
  2122. {
  2123. if(ShmCharger->gun_info[gun_index].PilotVoltage.PilotVoltageNegative == 0.0)
  2124. {
  2125. ShmCharger->gun_info[gun_index].acCcsInfo.CpPresentState = CCS_CP_STATE_B1;
  2126. }
  2127. else
  2128. {
  2129. ShmCharger->gun_info[gun_index].acCcsInfo.CpPresentState = CCS_CP_STATE_B2;
  2130. }
  2131. }
  2132. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  2133. {
  2134. ShmCharger->gun_info[gun_index].acCcsInfo.CpPresentState = CCS_CP_STATE_C;
  2135. }
  2136. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_D)
  2137. {
  2138. ShmCharger->gun_info[gun_index].acCcsInfo.CpPresentState = CCS_CP_STATE_D;
  2139. }
  2140. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_E)
  2141. {
  2142. ShmCharger->gun_info[gun_index].acCcsInfo.CpPresentState = CCS_CP_STATE_E;
  2143. }
  2144. else //CP_STATE_F
  2145. {
  2146. ShmCharger->gun_info[gun_index].acCcsInfo.CpPresentState = CCS_CP_STATE_F;
  2147. }
  2148. ShmCharger->gun_info[gun_index].acCcsInfo.CpPositiveVoltage = ShmCharger->gun_info[gun_index].PilotVoltage.PilotVoltagePositive;
  2149. ShmCharger->gun_info[gun_index].acCcsInfo.CpNegativeVoltage = ShmCharger->gun_info[gun_index].PilotVoltage.PilotVoltageNegative;
  2150. ShmCharger->gun_info[gun_index].acCcsInfo.CpPresentPWMDuty = ShmCharger->gun_info[gun_index].primaryMcuState.current_limit;
  2151. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  2152. {
  2153. ShmCharger->gun_info[gun_index].acCcsInfo.OutputRelayStatus = ON;
  2154. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].RelayK1K2Status = ON;
  2155. }
  2156. else
  2157. {
  2158. ShmCharger->gun_info[gun_index].acCcsInfo.OutputRelayStatus = OFF;
  2159. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].RelayK1K2Status = OFF;
  2160. }
  2161. failCount[gun_index] = 0;
  2162. }
  2163. else
  2164. {
  2165. DEBUG_WARN("MCU-%d get status fail...%d\n", gun_index, failCount[gun_index]);
  2166. if(failCount[gun_index]<USHRT_MAX)
  2167. failCount[gun_index]++;
  2168. else
  2169. failCount[gun_index] = FAIL_SPEC_COMM;
  2170. }
  2171. //===============================
  2172. // Case 4 : Query primary MCU Alarm code
  2173. //===============================
  2174. if(Query_AC_MCU_Alarm(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].primaryMcuAlarm))
  2175. {
  2176. //DEBUG_INFO("MCU-%d get alarm code success.\n",gun_index);
  2177. failCount[gun_index] = 0;
  2178. ShmCharger->gun_info[gun_index].acCcsInfo.CSUAlarmStatusCode = ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode;
  2179. }
  2180. else
  2181. {
  2182. DEBUG_WARN("MCU-%d get alarm fail...%d\n", gun_index, failCount[gun_index]);
  2183. if(failCount[gun_index]<USHRT_MAX)
  2184. failCount[gun_index]++;
  2185. else
  2186. failCount[gun_index] = FAIL_SPEC_COMM;
  2187. }
  2188. /*
  2189. //===============================
  2190. // Case 5 : Query primary MCU BLE config
  2191. //===============================
  2192. if(Query_Ble_Config(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].bleConfigData))
  2193. {
  2194. ShmSysConfigAndInfo->SysConfig.Bluetooth.isLogin = ShmCharger->gun_info[gun_index].bleConfigData.isLogin;
  2195. ShmSysConfigAndInfo->SysConfig.Bluetooth.isRequestStart = ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart;
  2196. ShmSysConfigAndInfo->SysConfig.Bluetooth.isRequestStop = ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop;
  2197. failCount[gun_index] = 0;
  2198. }
  2199. else
  2200. {
  2201. DEBUG_WARN("MCU-%d get ble config fail...%d\n", gun_index, failCount[gun_index]);
  2202. if(failCount[gun_index]<USHRT_MAX)
  2203. failCount[gun_index]++;
  2204. else
  2205. failCount[gun_index] = FAIL_SPEC_COMM;
  2206. }
  2207. //===============================
  2208. // Case 6 : Query primary MCU ble login id
  2209. //===============================
  2210. if(Query_Ble_Central_ID(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].bleLoginCentralId))
  2211. {
  2212. memcpy(ShmSysConfigAndInfo->SysConfig.Bluetooth.LoginCentralID, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, sizeof ShmSysConfigAndInfo->SysConfig.Bluetooth.LoginCentralID);
  2213. failCount[gun_index] = 0;
  2214. }
  2215. else
  2216. {
  2217. DEBUG_WARN("MCU-%d get ble login central id fail...%d\n", gun_index, failCount[gun_index]);
  2218. if(failCount[gun_index]<USHRT_MAX)
  2219. failCount[gun_index]++;
  2220. else
  2221. failCount[gun_index] = FAIL_SPEC_COMM;
  2222. }*/
  2223. //===============================
  2224. // Case 7 : Config primary MCU reset request
  2225. //===============================
  2226. if((access("/sys/class/gpio/gpio116/value", F_OK)) != -1)
  2227. {
  2228. if(ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest == ON)
  2229. {
  2230. if(Config_AC_MCU_RESET_REQUEST(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].mcuResetRequest) == PASS)
  2231. {
  2232. DEBUG_INFO("*******************************************\n");
  2233. DEBUG_INFO("**** High priority polling : Case 7 *******\n");
  2234. DEBUG_INFO("*******************************************\n");
  2235. DEBUG_INFO("MCU-%d set MCU reset Request : %d\n", gun_index, ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest);
  2236. ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest = OFF;
  2237. failCount[gun_index] = 0;
  2238. }
  2239. else
  2240. {
  2241. DEBUG_WARN("MCU-%d get MCU reset fail...%d\n", gun_index, failCount[gun_index]);
  2242. if(failCount[gun_index]<USHRT_MAX)
  2243. failCount[gun_index]++;
  2244. else
  2245. failCount[gun_index] = FAIL_SPEC_COMM;
  2246. }
  2247. }
  2248. }
  2249. //===============================
  2250. // Case 8 : Config primary set CP PWN duty
  2251. //===============================
  2252. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == ON)
  2253. {
  2254. //DEBUG_WARN("Case 8 : Config primary set CP PWN duty...%d\n", ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty);
  2255. if(Config_AC_MaxCurrent_And_CpPwmDuty(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty))
  2256. {
  2257. failCount[gun_index] = 0;
  2258. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = OFF;
  2259. }
  2260. else
  2261. {
  2262. DEBUG_WARN("MCU-%d set cp pwn duty fail...%d\n", gun_index, failCount[gun_index]);
  2263. if(failCount[gun_index]<USHRT_MAX)
  2264. failCount[gun_index]++;
  2265. else
  2266. failCount[gun_index] = FAIL_SPEC_COMM;
  2267. }
  2268. }
  2269. //===============================
  2270. // Case 9 : Query GPIO
  2271. //===============================
  2272. if(Query_Gpio_Input(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].GPIO_Input))
  2273. {
  2274. failCount[gun_index] = 0;
  2275. }
  2276. else
  2277. {
  2278. DEBUG_WARN("MCU-%d get GPIO input fail...%d\n", gun_index, failCount[gun_index]);
  2279. if(failCount[gun_index]<USHRT_MAX)
  2280. failCount[gun_index]++;
  2281. else
  2282. failCount[gun_index] = FAIL_SPEC_COMM;
  2283. }
  2284. //===============================
  2285. // Case 10 : Query primary MCU power consumption
  2286. //===============================
  2287. if(Query_Power_Consumption(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].powerConsumptionTotal, &ShmCharger->gun_info[gun_index].powerConsumption[0], &ShmCharger->gun_info[gun_index].powerConsumption[1], &ShmCharger->gun_info[gun_index].powerConsumption[2]))
  2288. {
  2289. failCount[gun_index] = 0;
  2290. }
  2291. else
  2292. {
  2293. DEBUG_WARN("MCU-%d get power consumption fail...%d\n", gun_index, failCount[gun_index]);
  2294. if(failCount[gun_index]<USHRT_MAX)
  2295. failCount[gun_index]++;
  2296. else
  2297. failCount[gun_index] = FAIL_SPEC_COMM;
  2298. }
  2299. //===============================
  2300. // Case 11 : Query present output current
  2301. //===============================
  2302. if(Query_Present_OutputCurrent(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].outputCurrent) == PASS)
  2303. {
  2304. #ifndef SIMULATION
  2305. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent = (float)ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0];
  2306. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL2 = (float)ShmCharger->gun_info[gun_index].outputCurrent.L2N_L23[0];
  2307. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL3 = (float)ShmCharger->gun_info[gun_index].outputCurrent.L3N_L31[0];
  2308. ShmCharger->gun_info[gun_index].acCcsInfo.EVSEPresentCurrent[0] = (float)ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0];
  2309. ShmCharger->gun_info[gun_index].acCcsInfo.EVSEPresentCurrent[1] = (float)ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0];
  2310. ShmCharger->gun_info[gun_index].acCcsInfo.EVSEPresentCurrent[2] = (float)ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0];
  2311. #else //SIMULATION
  2312. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent = (float)(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus==SYS_MODE_CHARGING?(((rand()%10)+((ShmCharger->gun_info[gun_index].targetCurrent*10)-5))/10.0):0);
  2313. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  2314. {
  2315. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL2 = (float)(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus==SYS_MODE_CHARGING?(((rand()%10)+((ShmCharger->gun_info[gun_index].targetCurrent*10)-5))/10.0):0);
  2316. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL3 = (float)(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus==SYS_MODE_CHARGING?(((rand()%10)+((ShmCharger->gun_info[gun_index].targetCurrent*10)-5))/10.0):0);
  2317. }
  2318. #endif //SIMULATION
  2319. failCount[gun_index] = 0;
  2320. }
  2321. else
  2322. {
  2323. DEBUG_WARN("MCU-%d get output current fail...%d\n", gun_index, failCount[gun_index]);
  2324. if(failCount[gun_index]<USHRT_MAX)
  2325. failCount[gun_index]++;
  2326. else
  2327. failCount[gun_index] = FAIL_SPEC_COMM;
  2328. }
  2329. }
  2330. else
  2331. {
  2332. /*
  2333. * Normal priority communication
  2334. */
  2335. switch(stepIndex)
  2336. {
  2337. case 1:
  2338. //===============================
  2339. // Query present input voltage
  2340. //===============================
  2341. if(Query_Present_InputVoltage(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].inputVoltage) == PASS)
  2342. {
  2343. ShmSysConfigAndInfo->SysInfo.InputVoltageR = ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12;
  2344. ShmSysConfigAndInfo->SysInfo.InputVoltageS = ShmCharger->gun_info[gun_index].inputVoltage.L2N_L23;
  2345. ShmSysConfigAndInfo->SysInfo.InputVoltageT = ShmCharger->gun_info[gun_index].inputVoltage.L3N_L31;
  2346. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltage = ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12;
  2347. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltageL2 = ShmCharger->gun_info[gun_index].inputVoltage.L2N_L23;
  2348. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingVoltageL3 = ShmCharger->gun_info[gun_index].inputVoltage.L3N_L31;
  2349. ShmCharger->gun_info[gun_index].acCcsInfo.GridVoltage[0] = ShmSysConfigAndInfo->SysInfo.InputVoltageR;
  2350. ShmCharger->gun_info[gun_index].acCcsInfo.GridVoltage[1] = ShmSysConfigAndInfo->SysInfo.InputVoltageS;
  2351. ShmCharger->gun_info[gun_index].acCcsInfo.GridVoltage[2] = ShmSysConfigAndInfo->SysInfo.InputVoltageT;
  2352. failCount[gun_index] = 0;
  2353. }
  2354. else
  2355. {
  2356. DEBUG_WARN("MCU-%d get input voltage fail...%d\n", gun_index, failCount[gun_index]);
  2357. if(failCount[gun_index]<USHRT_MAX)
  2358. failCount[gun_index]++;
  2359. else
  2360. failCount[gun_index] = FAIL_SPEC_COMM;
  2361. }
  2362. break;
  2363. case 3:
  2364. /*
  2365. //===============================
  2366. // Query present output current
  2367. //===============================
  2368. if(Query_Present_OutputCurrent(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].outputCurrent) == PASS)
  2369. {
  2370. #ifndef SIMULATION
  2371. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent = (float)ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0];
  2372. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL2 = (float)ShmCharger->gun_info[gun_index].outputCurrent.L2N_L23[0];
  2373. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL3 = (float)ShmCharger->gun_info[gun_index].outputCurrent.L3N_L31[0];
  2374. ShmCharger->gun_info[gun_index].acCcsInfo.EVSEPresentCurrent[0] = (float)ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0];
  2375. ShmCharger->gun_info[gun_index].acCcsInfo.EVSEPresentCurrent[1] = (float)ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0];
  2376. ShmCharger->gun_info[gun_index].acCcsInfo.EVSEPresentCurrent[2] = (float)ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0];
  2377. #else //SIMULATION
  2378. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent = (float)(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus==SYS_MODE_CHARGING?(((rand()%10)+((ShmCharger->gun_info[gun_index].targetCurrent*10)-5))/10.0):0);
  2379. if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
  2380. {
  2381. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL2 = (float)(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus==SYS_MODE_CHARGING?(((rand()%10)+((ShmCharger->gun_info[gun_index].targetCurrent*10)-5))/10.0):0);
  2382. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL3 = (float)(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus==SYS_MODE_CHARGING?(((rand()%10)+((ShmCharger->gun_info[gun_index].targetCurrent*10)-5))/10.0):0);
  2383. }
  2384. #endif //SIMULATION
  2385. failCount[gun_index] = 0;
  2386. }
  2387. else
  2388. {
  2389. DEBUG_WARN("MCU-%d get output current fail...%d\n", gun_index, failCount[gun_index]);
  2390. if(failCount[gun_index]<USHRT_MAX)
  2391. failCount[gun_index]++;
  2392. else
  2393. failCount[gun_index] = FAIL_SPEC_COMM;
  2394. }
  2395. */
  2396. break;
  2397. case 5:
  2398. //===============================
  2399. // Query gun plug-in times
  2400. //===============================
  2401. if(Query_AC_GUN_PLUGIN_TIMES(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].gunPluginTimes) == PASS)
  2402. {
  2403. ShmSysConfigAndInfo->SysConfig.AcPlugInTimes = ((long)ShmCharger->gun_info[gun_index].gunPluginTimes.GunPluginTimes & 0xFFFF);
  2404. failCount[gun_index] = 0;
  2405. }
  2406. else
  2407. {
  2408. DEBUG_WARN("MCU-%d get gun plugin times fail...%d\n", gun_index, failCount[gun_index]);
  2409. if(failCount[gun_index]<USHRT_MAX)
  2410. failCount[gun_index]++;
  2411. else
  2412. failCount[gun_index] = FAIL_SPEC_COMM;
  2413. }
  2414. break;
  2415. case 7:
  2416. //===============================
  2417. // Query temperature
  2418. //===============================
  2419. if(Query_Temperature(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].temperature) == PASS)
  2420. {
  2421. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = ShmCharger->gun_info[gun_index].temperature.point[0];
  2422. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ConnectorTemp = ShmCharger->gun_info[gun_index].temperature.point[0];
  2423. failCount[gun_index] = 0;
  2424. }
  2425. else
  2426. {
  2427. DEBUG_WARN("MCU-%d get temperature fail...%d\n", gun_index, failCount[gun_index]);
  2428. if(failCount[gun_index]<USHRT_MAX)
  2429. failCount[gun_index]++;
  2430. else
  2431. failCount[gun_index] = FAIL_SPEC_COMM;
  2432. }
  2433. break;
  2434. case 9:
  2435. //===============================
  2436. // Query RTC
  2437. //===============================
  2438. if(Query_RTC(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].rtc))
  2439. {
  2440. struct timeb csuTime, mcuTime;
  2441. struct tm *tmCSU;
  2442. struct tm tmMcu;
  2443. ftime(&csuTime);
  2444. tmCSU = localtime(&csuTime.time);
  2445. tmMcu.tm_year = ShmCharger->gun_info[gun_index].rtc.year-1900;
  2446. tmMcu.tm_mon = ShmCharger->gun_info[gun_index].rtc.month-1;
  2447. tmMcu.tm_mday = ShmCharger->gun_info[gun_index].rtc.day;
  2448. tmMcu.tm_hour = ShmCharger->gun_info[gun_index].rtc.hour;
  2449. tmMcu.tm_min = ShmCharger->gun_info[gun_index].rtc.min;
  2450. tmMcu.tm_sec = ShmCharger->gun_info[gun_index].rtc.sec;
  2451. mcuTime.time = mktime(&tmMcu);
  2452. if(ShmCharger->gun_info[gun_index].bleConfigData.isLogin && !ShmOCPP16Data->OcppConnStatus)
  2453. {
  2454. if(abs(DiffTimeb(csuTime, mcuTime)) > 10000)
  2455. {
  2456. char cmdBuf[128];
  2457. sprintf(cmdBuf, "date -u -s \"%04d-%02d-%02d %02d:%02d:%02d\"", ShmCharger->gun_info[gun_index].rtc.year,
  2458. ShmCharger->gun_info[gun_index].rtc.month,
  2459. ShmCharger->gun_info[gun_index].rtc.day,
  2460. ShmCharger->gun_info[gun_index].rtc.hour,
  2461. ShmCharger->gun_info[gun_index].rtc.min,
  2462. ShmCharger->gun_info[gun_index].rtc.sec);
  2463. system(cmdBuf);
  2464. system("hwclock -w -u");
  2465. system("hwclock -s");
  2466. }
  2467. }
  2468. else
  2469. {
  2470. if(abs(DiffTimeb(csuTime, mcuTime)) > 10000)
  2471. {
  2472. ShmCharger->gun_info[gun_index].rtc.year = tmCSU->tm_year+1900;
  2473. ShmCharger->gun_info[gun_index].rtc.month = tmCSU->tm_mon+1;
  2474. ShmCharger->gun_info[gun_index].rtc.day = tmCSU->tm_mday;
  2475. ShmCharger->gun_info[gun_index].rtc.hour = tmCSU->tm_hour;
  2476. ShmCharger->gun_info[gun_index].rtc.min = tmCSU->tm_min;
  2477. ShmCharger->gun_info[gun_index].rtc.sec = tmCSU->tm_sec;
  2478. Config_RTC(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].rtc);
  2479. }
  2480. }
  2481. failCount[gun_index] = 0;
  2482. }
  2483. else
  2484. {
  2485. DEBUG_WARN("MCU-%d get rtc fail...%d\n", gun_index, failCount[gun_index]);
  2486. if(failCount[gun_index]<USHRT_MAX)
  2487. failCount[gun_index]++;
  2488. else
  2489. failCount[gun_index] = FAIL_SPEC_COMM;
  2490. }
  2491. break;
  2492. case 11:
  2493. break;
  2494. case 13:
  2495. //===============================
  2496. // Upgrade MCU
  2497. //===============================
  2498. if(ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq)
  2499. {
  2500. DEBUG_INFO("===========================================\n");
  2501. DEBUG_INFO("==== Normal priority polling : Case 13 ====\n");
  2502. DEBUG_INFO("===========================================\n");
  2503. unsigned char cmd[512];
  2504. if(Upgrade_UART(Uart1Fd, AC_WALLMOUNT_CONTROLLER, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), ShmCharger->fwUpgradeInfo.location, ShmCharger->fwUpgradeInfo.modelName))
  2505. {
  2506. DEBUG_INFO("MCU-%d upgrade firmware OK...%s\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
  2507. sleep(20);
  2508. ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq = OFF;
  2509. failCount[gun_index] = 0;
  2510. }
  2511. else
  2512. {
  2513. DEBUG_WARN("MCU-%d upgrade firmware fail...%d\n", gun_index, failCount[gun_index]);
  2514. if(failCount[gun_index]<USHRT_MAX)
  2515. failCount[gun_index]++;
  2516. else
  2517. failCount[gun_index] = FAIL_SPEC_COMM;
  2518. }
  2519. sprintf((char*)cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  2520. system((char*)cmd);
  2521. }
  2522. break;
  2523. case 15:
  2524. //===============================
  2525. // Config set breathe led timing
  2526. //===============================
  2527. if(ShmCharger->gun_info[gun_index].isSetBreatheLedTiming == ON)
  2528. {
  2529. if(Config_AC_Set_Breathe_Led_Timing(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].setBreatheLedTiming))
  2530. {
  2531. failCount[gun_index] = 0;
  2532. ShmCharger->gun_info[gun_index].isSetBreatheLedTiming = OFF;
  2533. }
  2534. else
  2535. {
  2536. DEBUG_WARN("MCU-%d set breathe led timing fail...%d\n", gun_index, failCount[gun_index]);
  2537. if(failCount[gun_index]<USHRT_MAX)
  2538. failCount[gun_index]++;
  2539. else
  2540. failCount[gun_index] = FAIL_SPEC_COMM;
  2541. }
  2542. }
  2543. break;
  2544. case 17:
  2545. //===============================
  2546. // Config set led brightness
  2547. //===============================
  2548. if(ShmCharger->gun_info[gun_index].isSetLedBrightness == ON)
  2549. {
  2550. if(Config_AC_Set_Led_Brightness(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].setLedBrightness))
  2551. {
  2552. failCount[gun_index] = 0;
  2553. ShmCharger->gun_info[gun_index].isSetLedBrightness = OFF;
  2554. }
  2555. else
  2556. {
  2557. DEBUG_WARN("MCU-%d set led brightness fail...%d\n", gun_index, failCount[gun_index]);
  2558. if(failCount[gun_index]<USHRT_MAX)
  2559. failCount[gun_index]++;
  2560. else
  2561. failCount[gun_index] = FAIL_SPEC_COMM;
  2562. }
  2563. }
  2564. break;
  2565. case 19:
  2566. //===============================
  2567. // Query firmware version
  2568. //===============================
  2569. if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass != PASS)
  2570. {
  2571. DEBUG_INFO("===========================================\n");
  2572. DEBUG_INFO("==== Normal priority polling : Case 19-1===\n");
  2573. DEBUG_INFO("===========================================\n");
  2574. if(Query_FW_Ver(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].ver) == PASS)
  2575. {
  2576. DEBUG_INFO("MCU-%d get firmware version : %s\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
  2577. memcpy(ShmPrimaryMcuData->version, ShmCharger->gun_info[gun_index].ver.Version_FW, sizeof(ShmPrimaryMcuData->version));
  2578. ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass = PASS;
  2579. failCount[gun_index] = 0;
  2580. }
  2581. else
  2582. {
  2583. DEBUG_WARN("MCU-%d get firmware version fail...%d\n", gun_index, failCount[gun_index]);
  2584. if(failCount[gun_index]<USHRT_MAX)
  2585. failCount[gun_index]++;
  2586. else
  2587. failCount[gun_index] = FAIL_SPEC_COMM;
  2588. }
  2589. }
  2590. //===============================
  2591. // Query meter ic correction status
  2592. //===============================
  2593. if(ShmCharger->gun_info[gun_index].mcuFlag.isReadMeterIcCorrectionStatus != PASS)
  2594. {
  2595. DEBUG_INFO("===========================================\n");
  2596. DEBUG_INFO("==== Normal priority polling : Case 19-2===\n");
  2597. DEBUG_INFO("===========================================\n");
  2598. if(Query_MeterIc_CorrectionPara(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].meterIcCorrectionStatus) == PASS)
  2599. {
  2600. DEBUG_INFO("MCU-%d get meter ic correction status: 0x%08X\n", gun_index, ShmCharger->gun_info[gun_index].meterIcCorrectionStatus.value);
  2601. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedVaGain = ShmCharger->gun_info[gun_index].meterIcCorrectionStatus.bits.isCalibratedVaGain;
  2602. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedVbGain = ShmCharger->gun_info[gun_index].meterIcCorrectionStatus.bits.isCalibratedVbGain;
  2603. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedVcGain = ShmCharger->gun_info[gun_index].meterIcCorrectionStatus.bits.isCalibratedVcGain;
  2604. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedVaOffset = ShmCharger->gun_info[gun_index].meterIcCorrectionStatus.bits.isCalibratedVaOffset;
  2605. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedVbOffset = ShmCharger->gun_info[gun_index].meterIcCorrectionStatus.bits.isCalibratedVbOffset;
  2606. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedVcOffset = ShmCharger->gun_info[gun_index].meterIcCorrectionStatus.bits.isCalibratedVcOffset;
  2607. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedCaGain = ShmCharger->gun_info[gun_index].meterIcCorrectionStatus.bits.isCalibratedCaGain;
  2608. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedCbGain = ShmCharger->gun_info[gun_index].meterIcCorrectionStatus.bits.isCalibratedCbGain;
  2609. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedCcGain = ShmCharger->gun_info[gun_index].meterIcCorrectionStatus.bits.isCalibratedCcGain;
  2610. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedCaOffset = ShmCharger->gun_info[gun_index].meterIcCorrectionStatus.bits.isCalibratedCaOffset;
  2611. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedCbOffset = ShmCharger->gun_info[gun_index].meterIcCorrectionStatus.bits.isCalibratedCbOffset;
  2612. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedCcOffset = ShmCharger->gun_info[gun_index].meterIcCorrectionStatus.bits.isCalibratedCcOffset;
  2613. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedPa = ShmCharger->gun_info[gun_index].meterIcCorrectionStatus.bits.isCalibratedPa;
  2614. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedPb = ShmCharger->gun_info[gun_index].meterIcCorrectionStatus.bits.isCalibratedPb;
  2615. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].meterIcCalInfo.isCalibratedPc = ShmCharger->gun_info[gun_index].meterIcCorrectionStatus.bits.isCalibratedPc;
  2616. ShmCharger->gun_info[gun_index].mcuFlag.isReadMeterIcCorrectionStatus = PASS;
  2617. failCount[gun_index] = 0;
  2618. }
  2619. else
  2620. {
  2621. DEBUG_WARN("MCU-%d get meter ic correction status fail...%d\n", gun_index, failCount[gun_index]);
  2622. if(failCount[gun_index]<USHRT_MAX)
  2623. failCount[gun_index]++;
  2624. else
  2625. failCount[gun_index] = FAIL_SPEC_COMM;
  2626. }
  2627. }
  2628. //===============================
  2629. // Config primary MCU serial number
  2630. //===============================
  2631. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass != PASS)
  2632. {
  2633. DEBUG_INFO("===========================================\n");
  2634. DEBUG_INFO("==== Normal priority polling : Case 19-3===\n");
  2635. DEBUG_INFO("===========================================\n");
  2636. memcpy(ShmCharger->evseId.serial_number, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmCharger->evseId.serial_number));
  2637. if(Config_Serial_Number(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->evseId))
  2638. {
  2639. DEBUG_INFO("MCU-%d set serial number : %.12s\n",gun_index,ShmCharger->evseId.serial_number);
  2640. ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass = PASS;
  2641. failCount[gun_index] = 0;
  2642. }
  2643. else
  2644. {
  2645. DEBUG_WARN("MCU-%d set serial number fail...%d\n", gun_index, failCount[gun_index]);
  2646. if(failCount[gun_index]<USHRT_MAX)
  2647. failCount[gun_index]++;
  2648. else
  2649. failCount[gun_index] = FAIL_SPEC_COMM;
  2650. }
  2651. }
  2652. //===============================
  2653. // Config primary MCU model name
  2654. //===============================
  2655. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass != PASS)
  2656. {
  2657. DEBUG_INFO("===========================================\n");
  2658. DEBUG_INFO("==== Normal priority polling : Case 19-4===\n");
  2659. DEBUG_INFO("===========================================\n");
  2660. memcpy(ShmCharger->evseId.model_name, ShmSysConfigAndInfo->SysConfig.ModelName, ARRAY_SIZE(ShmCharger->evseId.model_name));
  2661. if(Config_Model_Name(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->evseId))
  2662. {
  2663. DEBUG_INFO("MCU-%d set model name : %.14s\n",gun_index,ShmCharger->evseId.model_name);
  2664. ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass = PASS;
  2665. failCount[gun_index] = 0;
  2666. }
  2667. else
  2668. {
  2669. DEBUG_WARN("MCU-%d set model name fail...%d\n", gun_index, failCount[gun_index]);
  2670. if(failCount[gun_index]<USHRT_MAX)
  2671. failCount[gun_index]++;
  2672. else
  2673. failCount[gun_index] = FAIL_SPEC_COMM;
  2674. }
  2675. }
  2676. break;
  2677. case 21:
  2678. //===============================
  2679. // Config aux power switch
  2680. //===============================
  2681. ShmCharger->gun_info[gun_index].setAuxPowerSwitch.power_switch = 0x01;
  2682. ShmCharger->gun_info[gun_index].setAuxPowerSwitch.power_switch = ShmCharger->gun_info[gun_index].isMeterOn;
  2683. if(Config_Aux_Power_Switch(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].setAuxPowerSwitch))
  2684. {
  2685. failCount[gun_index] = 0;
  2686. }
  2687. else
  2688. {
  2689. DEBUG_WARN("MCU-%d set aux power switch fail...%d\n", gun_index, failCount[gun_index]);
  2690. if(failCount[gun_index]<USHRT_MAX)
  2691. failCount[gun_index]++;
  2692. else
  2693. failCount[gun_index] = FAIL_SPEC_COMM;
  2694. }
  2695. break;
  2696. default:
  2697. stepIndex = 0;
  2698. break;
  2699. }
  2700. }
  2701. stepIndex++;
  2702. //===============================
  2703. // Communication fail check
  2704. //===============================
  2705. if(failCount[gun_index] >= 13)
  2706. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  2707. if(failCount[gun_index] >= FAIL_SPEC_COMM)
  2708. {
  2709. if((0 <= failCount[gun_index]%FAIL_SPEC_COMM) && (failCount[gun_index]%FAIL_SPEC_COMM < 10))
  2710. sleep(10);
  2711. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.comm_timeout == OFF)
  2712. {
  2713. DEBUG_ERROR("Primary MCU-%d communication fault. \n", gun_index);
  2714. ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.comm_timeout = ON;
  2715. }
  2716. }
  2717. else
  2718. {
  2719. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.comm_timeout == ON)
  2720. {
  2721. DEBUG_ERROR("Primary MCU-%d communication recover. \n", gun_index);
  2722. ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.comm_timeout = OFF;
  2723. }
  2724. }
  2725. usleep(100000);
  2726. }
  2727. }
  2728. return FAIL;
  2729. }