Module_InternalComm.c 109 KB

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