Module_InternalComm.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254
  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> /*Unix 標準函數定義*/
  20. #include <fcntl.h> /*檔控制定義*/
  21. #include <termios.h> /*PPSIX 終端控制定義*/
  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 "define.h"
  30. #include "main.h"
  31. #include "Module_InternalComm.h"
  32. #define FAIL_SPEC_COMM 1000
  33. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  34. #define PASS 1
  35. #define FAIL 0
  36. #define ON 1
  37. #define OFF 0
  38. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  39. struct StatusCodeData *ShmStatusCodeData;
  40. struct PrimaryMcuData *ShmPrimaryMcuData;
  41. struct OCPP16Data *ShmOCPP16Data;
  42. struct Charger *ShmCharger;
  43. uint16_t stepIndex;
  44. void trim(char *s);
  45. int mystrcmp(char *p1,char *p2);
  46. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  47. void split(char **arr, char *str, const char *del);
  48. int StoreLogMsg(const char *fmt, ...)
  49. {
  50. char Buf[4096+256];
  51. char buffer[4096];
  52. time_t CurrentTime;
  53. struct tm *tm;
  54. va_list args;
  55. va_start(args, fmt);
  56. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  57. va_end(args);
  58. memset(Buf,0,sizeof(Buf));
  59. CurrentTime = time(NULL);
  60. tm=localtime(&CurrentTime);
  61. sprintf(Buf,"echo \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  62. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  63. buffer,
  64. tm->tm_year+1900,tm->tm_mon+1);
  65. #ifdef SystemLogMessage
  66. system(Buf);
  67. #endif
  68. printf("[%04d.%02d.%02d %02d:%02d:%02d] - %s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec, buffer);
  69. return rc;
  70. }
  71. int DiffTimeb(struct timeb ST, struct timeb ET)
  72. {
  73. //return milli-second
  74. unsigned int StartTime,StopTime;
  75. StartTime=(unsigned int)ST.time;
  76. StopTime=(unsigned int)ET.time;
  77. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  78. }
  79. //=================================
  80. // Common routine
  81. //=================================
  82. void trim(char *s)
  83. {
  84. int i=0, j, k, l=0;
  85. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  86. i++;
  87. j = strlen(s)-1;
  88. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  89. j--;
  90. if(i==0 && j==strlen(s)-1) { }
  91. else if(i==0) s[j+1] = '\0';
  92. else {
  93. for(k=i; k<=j; k++) s[l++] = s[k];
  94. s[l] = '\0';
  95. }
  96. }
  97. int mystrcmp(char *p1,char *p2)
  98. {
  99. while(*p1==*p2)
  100. {
  101. if(*p1=='\0' || *p2=='\0')
  102. break;
  103. p1++;
  104. p2++;
  105. }
  106. if(*p1=='\0' && *p2=='\0')
  107. return(PASS);
  108. else
  109. return(FAIL);
  110. }
  111. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  112. {
  113. strncpy(dest, src + start, cnt);
  114. dest[cnt] = 0;
  115. }
  116. void split(char **arr, char *str, const char *del)
  117. {
  118. char *s = strtok(str, del);
  119. while(s != NULL)
  120. {
  121. *arr++ = s;
  122. s = strtok(NULL, del);
  123. }
  124. }
  125. //==========================================
  126. // Init all share memory
  127. //==========================================
  128. int InitShareMemory()
  129. {
  130. int result = PASS;
  131. int MeterSMId;
  132. //Initial ShmSysConfigAndInfo
  133. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  134. {
  135. #ifdef SystemLogMessage
  136. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  137. #endif
  138. result = FAIL;
  139. }
  140. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  141. {
  142. #ifdef SystemLogMessage
  143. DEBUG_ERROR("[shmat ShmSysConfigAndInfo NG\n");
  144. #endif
  145. result = FAIL;
  146. }
  147. //Initial ShmStatusCodeData
  148. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  149. {
  150. #ifdef SystemLogMessage
  151. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  152. #endif
  153. result = FAIL;
  154. }
  155. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  156. {
  157. #ifdef SystemLogMessage
  158. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  159. #endif
  160. result = FAIL;
  161. }
  162. //Initial ShmPrimaryMcuKey
  163. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  164. {
  165. #ifdef SystemLogMessage
  166. DEBUG_ERROR("shmget ShmPrimaryMcuData NG\r\n");
  167. #endif
  168. result = FAIL;
  169. }
  170. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  171. {
  172. #ifdef SystemLogMessage
  173. DEBUG_ERROR("shmat ShmPrimaryMcuData NG\r\n");
  174. #endif
  175. result = FAIL;
  176. }
  177. //creat ShmOCPP16Data
  178. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
  179. {
  180. #ifdef SystemLogMessage
  181. DEBUG_ERROR("shmget ShmOCPP16Data NG");
  182. #endif
  183. result = FAIL;
  184. }
  185. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  186. {
  187. #ifdef SystemLogMessage
  188. DEBUG_ERROR("shmat ShmOCPP16Data NG");
  189. #endif
  190. result = FAIL;
  191. }
  192. else
  193. {}
  194. //Initial ShmCharger
  195. if ((MeterSMId = shmget(ShmChargerKey, sizeof(struct Charger), IPC_CREAT | 0777)) < 0)
  196. {
  197. #ifdef SystemLogMessage
  198. DEBUG_ERROR("shmget ShmChargerKey NG\r\n");
  199. #endif
  200. result = FAIL;
  201. }
  202. else if ((ShmCharger = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  203. {
  204. #ifdef SystemLogMessage
  205. DEBUG_ERROR("shmat ShmChargerKey NG\r\n");
  206. #endif
  207. result = FAIL;
  208. }
  209. return result;
  210. }
  211. int InitComPort()
  212. {
  213. int fd;
  214. struct termios tios;
  215. fd = open("/dev/ttyS1", O_RDWR);
  216. if(fd<=0)
  217. {
  218. #ifdef SystemLogMessage
  219. DEBUG_ERROR("open /dev/ttyS1 NG\n");
  220. #endif
  221. return -1;
  222. }
  223. ioctl (fd, TCGETS, &tios);
  224. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  225. tios.c_lflag = 0;
  226. tios.c_iflag = 0;
  227. tios.c_oflag = 0;
  228. tios.c_cc[VMIN]=0;
  229. tios.c_cc[VTIME]=(unsigned char)5; // timeout 0.5 secod
  230. tios.c_lflag=0;
  231. tcflush(fd, TCIFLUSH);
  232. ioctl (fd, TCSETS, &tios);
  233. return fd;
  234. }
  235. void ack_delay(unsigned char cmd)
  236. {
  237. switch(cmd)
  238. {
  239. case CMD_UPDATE_START:
  240. case CMD_UPDATE_END:
  241. usleep(300000);
  242. break;
  243. default:
  244. usleep(100000);
  245. break;
  246. }
  247. }
  248. int tranceive(int fd, unsigned char* cmd, unsigned char cmd_len, unsigned char* rx)
  249. {
  250. int len;
  251. tcflush(fd,TCIOFLUSH);
  252. if(write(fd, cmd, cmd_len) >= cmd_len)
  253. {
  254. ack_delay(cmd[3]);
  255. len = read(fd, rx, 512);
  256. }
  257. else
  258. {
  259. #ifdef SystemLogMessage
  260. DEBUG_ERROR("Serial command %s response fail.\n", cmd);
  261. #endif
  262. }
  263. return len;
  264. }
  265. unsigned char Query_FW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
  266. {
  267. unsigned char result = FAIL;
  268. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_FW_VER, 0x00, 0x00, 0x00};
  269. unsigned char rx[512];
  270. unsigned char chksum = 0x00;
  271. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  272. if(len > 6)
  273. {
  274. if (len < 6+(rx[4] | rx[5]<<8))
  275. return result;
  276. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  277. {
  278. chksum ^= rx[6+idx];
  279. }
  280. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  281. (rx[2] == tx[1]) &&
  282. (rx[1] == tx[2]) &&
  283. (rx[3] == tx[3]))
  284. {
  285. strncpy(Ret_Buf->Version_FW, (char *)rx+6, (rx[4] | rx[5]<<8));
  286. result = PASS;
  287. }
  288. }
  289. return result;
  290. }
  291. unsigned char Query_HW_Ver(unsigned char fd, unsigned char targetAddr, Ver *Ret_Buf)
  292. {
  293. unsigned char result = FAIL;
  294. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_HW_VER, 0x00, 0x00, 0x00};
  295. unsigned char rx[512];
  296. unsigned char chksum = 0x00;
  297. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  298. if(len > 6)
  299. {
  300. if (len < 6+(rx[4] | rx[5]<<8))
  301. return result;
  302. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  303. {
  304. chksum ^= rx[6+idx];
  305. }
  306. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  307. (rx[2] == tx[1]) &&
  308. (rx[1] == tx[2]) &&
  309. (rx[3] == tx[3]))
  310. {
  311. strncpy(Ret_Buf->Version_HW, (char *)rx+6, (rx[4] | rx[5]<<8));
  312. result = PASS;
  313. }
  314. }
  315. return result;
  316. }
  317. unsigned char Query_Present_InputVoltage(unsigned char fd, unsigned char targetAddr, PresentInputVoltage *Ret_Buf)
  318. {
  319. unsigned char result = FAIL;
  320. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_PRESENT_INPUTVOLTAGE, 0x00, 0x00, 0x00};
  321. unsigned char rx[512];
  322. unsigned char chksum = 0x00;
  323. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  324. if(len > 6)
  325. {
  326. if (len < 6+(rx[4] | rx[5]<<8))
  327. return result;
  328. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  329. {
  330. chksum ^= rx[6+idx];
  331. }
  332. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  333. (rx[2] == tx[1]) &&
  334. (rx[1] == tx[2]) &&
  335. (rx[3] == tx[3]))
  336. {
  337. Ret_Buf->inputType = rx[6];
  338. Ret_Buf->L1N_L12 =( rx[7] | (rx[8]<<8))/10.0;
  339. Ret_Buf->L2N_L23 =( rx[9] | (rx[10]<<8))/10.0;
  340. Ret_Buf->L3N_L31 =( rx[11] | (rx[12]<<8))/10.0;
  341. result = PASS;
  342. }
  343. }
  344. return result;
  345. }
  346. unsigned char Query_Present_OutputVoltage(unsigned char fd, unsigned char targetAddr, PresentOutputVoltage *Ret_Buf)
  347. {
  348. unsigned char result = FAIL;
  349. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_PRESENT_OUTPUTVOLTAGE, 0x00, 0x00, 0x00};
  350. unsigned char rx[512];
  351. unsigned char chksum = 0x00;
  352. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  353. if(len > 6)
  354. {
  355. if (len < 6+(rx[4] | rx[5]<<8))
  356. return result;
  357. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  358. {
  359. chksum ^= rx[6+idx];
  360. }
  361. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  362. (rx[2] == tx[1]) &&
  363. (rx[1] == tx[2]) &&
  364. (rx[3] == tx[3]))
  365. {
  366. Ret_Buf->behindFuse_Voltage_C1 =(rx[6] | (rx[7]<<8))/10.0;
  367. Ret_Buf->behindRelay_Voltage_C1 =(rx[8] | (rx[9]<<8))/10.0;
  368. if((rx[4] | rx[5]<<8) > 4)
  369. {
  370. Ret_Buf->behindFuse_Voltage_C2 =(rx[10] | (rx[11]<<8))/10.0;
  371. Ret_Buf->behindRelay_Voltage_C2 =(rx[12] | (rx[13]<<8))/10.0;
  372. }
  373. result = PASS;
  374. }
  375. }
  376. return result;
  377. }
  378. unsigned char Query_Fan_Speed(unsigned char fd, unsigned char targetAddr, FanSpeed *Ret_Buf)
  379. {
  380. unsigned char result = FAIL;
  381. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_FAN_SPEED, 0x00, 0x00, 0x00};
  382. unsigned char rx[512];
  383. unsigned char chksum = 0x00;
  384. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  385. if(len > 6)
  386. {
  387. if (len < 6+(rx[4] | rx[5]<<8))
  388. return result;
  389. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  390. {
  391. chksum ^= rx[6+idx];
  392. }
  393. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  394. (rx[2] == tx[1]) &&
  395. (rx[1] == tx[2]) &&
  396. (rx[3] == tx[3]))
  397. {
  398. for(int idx=0;idx<((rx[4] | rx[5]<<8)>>1);idx++)
  399. Ret_Buf->speed[idx] =(rx[6+(2*idx)] | (rx[6+(2*idx)+1]<<8));
  400. result = PASS;
  401. }
  402. }
  403. return result;
  404. }
  405. unsigned char Query_Temperature(unsigned char fd, unsigned char targetAddr, Temperature *Ret_Buf)
  406. {
  407. unsigned char result = FAIL;
  408. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_TEMPERATURE, 0x00, 0x00, 0x00};
  409. unsigned char rx[512];
  410. unsigned char chksum = 0x00;
  411. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  412. if(len > 6)
  413. {
  414. if (len < 6+(rx[4] | rx[5]<<8))
  415. return result;
  416. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  417. {
  418. chksum ^= rx[6+idx];
  419. }
  420. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  421. (rx[2] == tx[1]) &&
  422. (rx[1] == tx[2]) &&
  423. (rx[3] == tx[3]))
  424. {
  425. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  426. Ret_Buf->point[idx] = rx[6+idx] - 60;
  427. result = PASS;
  428. }
  429. }
  430. return result;
  431. }
  432. unsigned char Query_Aux_PowerVoltage(unsigned char fd, unsigned char targetAddr, AuxPower *Ret_Buf)
  433. {
  434. unsigned char result = FAIL;
  435. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AUX_POWERVOLTAGE, 0x00, 0x00, 0x00};
  436. unsigned char rx[512];
  437. unsigned char chksum = 0x00;
  438. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  439. if(len > 6)
  440. {
  441. if (len < 6+(rx[4] | rx[5]<<8))
  442. return result;
  443. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  444. {
  445. chksum ^= rx[6+idx];
  446. }
  447. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  448. (rx[2] == tx[1]) &&
  449. (rx[1] == tx[2]) &&
  450. (rx[3] == tx[3]))
  451. {
  452. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  453. Ret_Buf->voltage[idx] = rx[6+idx];
  454. result = PASS;
  455. }
  456. }
  457. return result;
  458. }
  459. unsigned char Query_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Ret_Buf)
  460. {
  461. unsigned char result = FAIL;
  462. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_RELAY_OUTPUT, 0x00, 0x00, 0x00};
  463. unsigned char rx[512];
  464. unsigned char chksum = 0x00;
  465. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  466. if(len > 6)
  467. {
  468. if (len < 6+(rx[4] | rx[5]<<8))
  469. return result;
  470. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  471. {
  472. chksum ^= rx[6+idx];
  473. }
  474. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  475. (rx[2] == tx[1]) &&
  476. (rx[1] == tx[2]) &&
  477. (rx[3] == tx[3]))
  478. {
  479. for(int idx_connector=0;idx_connector<(rx[4] | rx[5]<<8);idx_connector++)
  480. {
  481. for(int idx=0;idx<8;idx++)
  482. Ret_Buf->relay_status[idx_connector][idx] = (rx[6+idx_connector]>>idx) & 0x01;
  483. }
  484. result = PASS;
  485. }
  486. }
  487. return result;
  488. }
  489. unsigned char Query_Gfd_Adc(unsigned char fd, unsigned char targetAddr, Gfd *Ret_Buf)
  490. {
  491. unsigned char result = FAIL;
  492. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_GFD_ADC, 0x00, 0x00, 0x00};
  493. unsigned char rx[512];
  494. unsigned char chksum = 0x00;
  495. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  496. if(len > 6)
  497. {
  498. if (len < 6+(rx[4] | rx[5]<<8))
  499. return result;
  500. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  501. {
  502. chksum ^= rx[6+idx];
  503. }
  504. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  505. (rx[2] == tx[1]) &&
  506. (rx[1] == tx[2]) &&
  507. (rx[3] == tx[3]))
  508. {
  509. for(int idx_connector=0;idx_connector<((rx[4] | rx[5]<<8)>>2);idx_connector++)
  510. {
  511. Ret_Buf->adc_value_positive[idx_connector] = (rx[6+(4*idx_connector)] | rx[6+(4*idx_connector)+1]<<8);
  512. Ret_Buf->adc_value_negative[idx_connector] = (rx[6+(4*idx_connector)+2] | rx[6+(4*idx_connector)+3]<<8);;
  513. }
  514. result = PASS;
  515. }
  516. }
  517. return result;
  518. }
  519. unsigned char Query_Gpio_Input(unsigned char fd, unsigned char targetAddr, Gpio_in *Ret_Buf)
  520. {
  521. unsigned char result = FAIL;
  522. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_GPIO_INPUT, 0x00, 0x00, 0x00};
  523. unsigned char rx[512];
  524. unsigned char chksum = 0x00;
  525. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  526. if(len > 6)
  527. {
  528. if (len < 6+(rx[4] | rx[5]<<8))
  529. return result;
  530. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  531. {
  532. chksum ^= rx[6+idx];
  533. }
  534. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  535. (rx[2] == tx[1]) &&
  536. (rx[1] == tx[2]) &&
  537. (rx[3] == tx[3]))
  538. {
  539. Ret_Buf->AC_Connector = (rx[6] >> 0) & 0x01;
  540. Ret_Buf->AC_MainBreaker = (rx[6] >> 1) & 0x01;
  541. Ret_Buf->SPD = (rx[6] >> 2) & 0x01;
  542. Ret_Buf->Door_Open = (rx[6] >> 3) & 0x01;
  543. Ret_Buf->GFD[0] = (rx[6] >> 4) & 0x01;
  544. Ret_Buf->GFD[1] = (rx[6] >> 5) & 0x01;
  545. Ret_Buf->Button[0] = (rx[6] >> 6) & 0x01;
  546. Ret_Buf->Button[1] = (rx[6] >> 7) & 0x01;
  547. Ret_Buf->Button_Emergency = (rx[7] >> 0) & 0x01;
  548. result = PASS;
  549. }
  550. }
  551. return result;
  552. }
  553. unsigned char Query_Alarm_Log(unsigned char fd, unsigned char targetAddr, Alarm_Log *Ret_Buf)
  554. {
  555. unsigned char result = FAIL;
  556. 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};
  557. unsigned char rx[512];
  558. unsigned char chksum = Ret_Buf->logArea ^ (Ret_Buf->alarmIndex&0xff) ^ ((Ret_Buf->alarmIndex>>8)&0xff);
  559. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  560. if(len > 6)
  561. {
  562. if (len < 6+(rx[4] | rx[5]<<8))
  563. return result;
  564. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  565. {
  566. chksum ^= rx[6+idx];
  567. }
  568. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  569. (rx[2] == tx[1]) &&
  570. (rx[1] == tx[2]) &&
  571. (rx[3] == tx[3]))
  572. {
  573. memcpy(&Ret_Buf->log[0], &rx[8], 8);
  574. result = PASS;
  575. }
  576. }
  577. return result;
  578. }
  579. unsigned char Query_RTC(unsigned char fd, unsigned char targetAddr, Rtc *Ret_Buf)
  580. {
  581. unsigned char result = FAIL;
  582. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_RTC, 0x00, 0x00, 0x00};
  583. unsigned char rx[512];
  584. unsigned char chksum = 0x00;
  585. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  586. if(len > 6)
  587. {
  588. if (len < 6+(rx[4] | rx[5]<<8))
  589. return result;
  590. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  591. {
  592. chksum ^= rx[6+idx];
  593. }
  594. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  595. (rx[2] == tx[1]) &&
  596. (rx[1] == tx[2]) &&
  597. (rx[3] == tx[3]))
  598. {
  599. Ret_Buf->year = ((rx[6]-'0')*1000) + ((rx[7]-'0')*100) + ((rx[8]-'0')*10) + ((rx[9]-'0')*1);
  600. Ret_Buf->month = ((rx[10]-'0')*10) + ((rx[11]-'0')*1);
  601. Ret_Buf->day = ((rx[12]-'0')*10) + ((rx[13]-'0')*1);
  602. Ret_Buf->hour = ((rx[14]-'0')*10) + ((rx[15]-'0')*1);
  603. Ret_Buf->min = ((rx[16]-'0')*10) + ((rx[17]-'0')*1);
  604. Ret_Buf->sec = ((rx[18]-'0')*10) + ((rx[19]-'0')*1);
  605. result = PASS;
  606. }
  607. }
  608. return result;
  609. }
  610. unsigned char Query_AC_MCU_Status(unsigned char fd, unsigned char targetAddr, Ac_Primary_Mcu *Ret_Buf)
  611. {
  612. unsigned char result = FAIL;
  613. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AC_STATUS, 0x00, 0x00, 0x00};
  614. unsigned char rx[512];
  615. unsigned char chksum = 0x00;
  616. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  617. if(len > 6)
  618. {
  619. if (len < 6+(rx[4] | rx[5]<<8))
  620. return result;
  621. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  622. {
  623. chksum ^= rx[6+idx];
  624. }
  625. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  626. (rx[2] == tx[1]) &&
  627. (rx[1] == tx[2]) &&
  628. (rx[3] == tx[3]))
  629. {
  630. Ret_Buf->cp_state = rx[6];
  631. Ret_Buf->current_limit = rx[7] | (rx[8]<<0x08);
  632. Ret_Buf->cp_voltage_positive = (rx[9] | (rx[10]<<0x08))/100.0;
  633. Ret_Buf->cp_voltage_negtive = (rx[11] | (rx[12]<<0x08))/100.0;
  634. Ret_Buf->locker_state = rx[13];
  635. Ret_Buf->relay_state = rx[14];
  636. Ret_Buf->shutter_state = rx[15];
  637. Ret_Buf->meter_state = rx[16];
  638. Ret_Buf->pp_state = rx[17];
  639. Ret_Buf->rating_current = rx[18];
  640. Ret_Buf->rotatory_switch = (rx[19] & 0x0F);
  641. result = PASS;
  642. }
  643. }
  644. return result;
  645. }
  646. unsigned char Query_AC_MCU_Alarm(unsigned char fd, unsigned char targetAddr, Ac_Primary_Mcu_Alarm *Ret_Buf)
  647. {
  648. unsigned char result = FAIL;
  649. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AC_ALARM, 0x00, 0x00, 0x00};
  650. unsigned char rx[512];
  651. unsigned char chksum = 0x00;
  652. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  653. if(len > 6)
  654. {
  655. if (len < 6+(rx[4] | rx[5]<<8))
  656. return result;
  657. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  658. {
  659. chksum ^= rx[6+idx];
  660. }
  661. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  662. (rx[2] == tx[1]) &&
  663. (rx[1] == tx[2]) &&
  664. (rx[3] == tx[3]))
  665. {
  666. //rx[6]
  667. Ret_Buf->bits.OVP = (((rx[6]>>0)&0x01)?1:0);
  668. Ret_Buf->bits.UVP = (((rx[6]>>1)&0x01)?1:0);
  669. Ret_Buf->bits.OCP = (((rx[6]>>2)&0x01)?1:0);
  670. Ret_Buf->bits.OTP = (((rx[6]>>3)&0x01)?1:0);
  671. Ret_Buf->bits.gmi_fault = (((rx[6]>>4)&0x01)?1:0);
  672. Ret_Buf->bits.cp_fault = (((rx[6]>>5)&0x01)?1:0);
  673. Ret_Buf->bits.ac_leak = (((rx[6]>>6)&0x01)?1:0);
  674. Ret_Buf->bits.dc_leak = (((rx[6]>>7)&0x01)?1:0);
  675. //rx[7]
  676. Ret_Buf->bits.mcu_selftest_fail = (((rx[7]>>0)&0x01)?1:0);
  677. Ret_Buf->bits.handshaking_timeout = (((rx[7]>>1)&0x01)?1:0);
  678. Ret_Buf->bits.emergency_stop = (((rx[7]>>2)&0x01)?1:0);
  679. Ret_Buf->bits.relay_welding = (((rx[7]>>3)&0x01)?1:0);
  680. Ret_Buf->bits.leak_module_fail = (((rx[7]>>4)&0x01)?1:0);
  681. Ret_Buf->bits.shutter_fault = (((rx[7]>>5)&0x01)?1:0);
  682. Ret_Buf->bits.locker_fault = (((rx[7]>>6)&0x01)?1:0);
  683. Ret_Buf->bits.power_drop = (((rx[7]>>7)&0x01)?1:0);
  684. //rx[8] 3~7bits reserved
  685. Ret_Buf->bits.circuit_short = (((rx[8]>>0)&0x01)?1:0);
  686. Ret_Buf->bits.set_circuit = (((rx[8]>>1)&0x01)?1:0);
  687. Ret_Buf->bits.relay_drive_fault = (((rx[8]>>2)&0x01)?1:0);
  688. //rx[9] 0~7bits reserved
  689. result = PASS;
  690. }
  691. }
  692. return result;
  693. }
  694. unsigned char Query_Present_OutputCurrent(unsigned char fd, unsigned char targetAddr, Presentoutputcurrent *Ret_Buf)
  695. {
  696. unsigned char result = FAIL;
  697. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_PRESENT_OUTPUTCURRENT, 0x00, 0x00, 0x00};
  698. unsigned char rx[512];
  699. unsigned char chksum = 0x00;
  700. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  701. if(len > 0)
  702. {
  703. if(len < 6+(rx[4] | rx[5]<<8))
  704. return result;
  705. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  706. {
  707. chksum ^= rx[6+idx];
  708. }
  709. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  710. (rx[2] == tx[1]) &&
  711. (rx[1] == tx[2]) &&
  712. (rx[3] == tx[3]))
  713. {
  714. Ret_Buf->L1N_L12[0] = (rx[6] | (rx[7]<<8))/10.0;
  715. Ret_Buf->L2N_L23[0] = (rx[8] | (rx[9]<<8))/10.0;
  716. Ret_Buf->L3N_L31[0] = (rx[10] | (rx[11]<<8))/10.0;
  717. Ret_Buf->L1N_L12[1] = (rx[12] | (rx[13]<<8))/10.0;
  718. Ret_Buf->L2N_L23[1] = (rx[14] | (rx[15]<<8))/10.0;
  719. Ret_Buf->L3N_L31[1] = (rx[16] | (rx[17]<<8))/10.0;
  720. result = PASS;
  721. }
  722. }
  723. return result;
  724. }
  725. unsigned char Query_Ble_Config(unsigned char fd, unsigned char targetAddr, Ble_Config_Data *Ret_Buf)
  726. {
  727. unsigned char result = FAIL;
  728. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_BLE_CONFIG_DATA, 0x00, 0x00, 0x00};
  729. unsigned char rx[512];
  730. unsigned char chksum = 0x00;
  731. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  732. if(len > 0)
  733. {
  734. if(len < 6+(rx[4] | rx[5]<<8))
  735. return result;
  736. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  737. {
  738. chksum ^= rx[6+idx];
  739. }
  740. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  741. (rx[2] == tx[1]) &&
  742. (rx[1] == tx[2]) &&
  743. (rx[3] == tx[3]))
  744. {
  745. Ret_Buf->isLogin = (rx[6]?0x01:0x00);
  746. Ret_Buf->isRequestStart = (rx[7]?0x01:0x00);
  747. Ret_Buf->isRequestStop = (rx[8]?0x01:0x00);
  748. result = PASS;
  749. }
  750. }
  751. return result;
  752. }
  753. unsigned char Query_Ble_Central_ID(unsigned char fd, unsigned char targetAddr, Ble_Login_Central_Id *Ret_Buf)
  754. {
  755. unsigned char result = FAIL;
  756. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_BLE_CENTRAL_ID, 0x00, 0x00, 0x00};
  757. unsigned char rx[512];
  758. unsigned char chksum = 0x00;
  759. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  760. if(len > 0)
  761. {
  762. if(len < 6+(rx[4] | rx[5]<<8))
  763. return result;
  764. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  765. {
  766. chksum ^= rx[6+idx];
  767. }
  768. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  769. (rx[2] == tx[1]) &&
  770. (rx[1] == tx[2]) &&
  771. (rx[3] == tx[3]))
  772. {
  773. memset(Ret_Buf->id, 0x00, ARRAY_SIZE(Ret_Buf->id));
  774. memcpy(&Ret_Buf->id[0], &rx[6], (rx[4] | (rx[5]<<8)));
  775. result = PASS;
  776. }
  777. }
  778. return result;
  779. }
  780. unsigned char Query_Power_Consumption(unsigned char fd, unsigned char targetAddr, Power_Consumption *Ret_Buf)
  781. {
  782. unsigned char result = FAIL;
  783. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_POWER_CONSUMPTION, 0x00, 0x00, 0x00};
  784. unsigned char rx[512];
  785. unsigned char chksum = 0x00;
  786. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  787. if(len > 0)
  788. {
  789. if(len < 6+(rx[4] | rx[5]<<8))
  790. return result;
  791. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  792. {
  793. chksum ^= rx[6+idx];
  794. }
  795. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  796. (rx[2] == tx[1]) &&
  797. (rx[1] == tx[2]) &&
  798. (rx[3] == tx[3]))
  799. {
  800. Ret_Buf-> power_consumption = ((uint32_t)rx[6] | (((uint32_t)rx[7])<<8) | (((uint32_t)rx[8])<<16) | (((uint32_t)rx[9])<<24));
  801. result = PASS;
  802. }
  803. }
  804. return result;
  805. }
  806. unsigned char Config_Fan_Speed(unsigned char fd, unsigned char targetAddr, FanSpeed *Set_Buf)
  807. {
  808. unsigned char result = FAIL;
  809. 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};
  810. unsigned char rx[512];
  811. unsigned char chksum = 0x00;
  812. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  813. chksum ^= tx[6+idx];
  814. tx[14] = chksum;
  815. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  816. if(len > 6)
  817. {
  818. if (len < 6+(rx[4] | rx[5]<<8))
  819. return result;
  820. chksum = 0x00;
  821. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  822. {
  823. chksum ^= rx[6+idx];
  824. }
  825. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  826. (rx[2] == tx[1]) &&
  827. (rx[1] == tx[2]) &&
  828. (rx[3] == tx[3]) &&
  829. (rx[6] == 0x01))
  830. {
  831. result = PASS;
  832. }
  833. }
  834. return result;
  835. }
  836. unsigned char Config_Serial_Number(unsigned char fd, unsigned char targetAddr, Evse_Id *Set_Buf)
  837. {
  838. unsigned char result = FAIL;
  839. 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};
  840. unsigned char rx[512];
  841. unsigned char chksum = 0x00;
  842. memcpy(&tx[14], &Set_Buf->serial_number[0], ARRAY_SIZE(Set_Buf->serial_number));
  843. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  844. chksum ^= tx[6+idx];
  845. tx[26] = chksum;
  846. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  847. if(len > 6)
  848. {
  849. chksum = 0x00;
  850. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  851. {
  852. chksum ^= rx[6+idx];
  853. }
  854. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  855. (rx[2] == tx[1]) &&
  856. (rx[1] == tx[2]) &&
  857. (rx[3] == tx[3]) &&
  858. (rx[6] == 0x01))
  859. {
  860. result = PASS;
  861. }
  862. }
  863. return result;
  864. }
  865. unsigned char Config_Model_Name(unsigned char fd, unsigned char targetAddr, Evse_Id *Set_Buf)
  866. {
  867. unsigned char result = FAIL;
  868. 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};
  869. unsigned char rx[512];
  870. unsigned char chksum = 0x00;
  871. memcpy(&tx[6], &Set_Buf->model_name[0], ARRAY_SIZE(Set_Buf->model_name));
  872. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  873. chksum ^= tx[6+idx];
  874. tx[20] = chksum;
  875. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  876. if(len > 6)
  877. {
  878. chksum = 0x00;
  879. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  880. {
  881. chksum ^= rx[6+idx];
  882. }
  883. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  884. (rx[2] == tx[1]) &&
  885. (rx[1] == tx[2]) &&
  886. (rx[3] == tx[3]) &&
  887. (rx[6] == 0x01))
  888. {
  889. result = PASS;
  890. }
  891. }
  892. return result;
  893. }
  894. unsigned char Config_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Set_Buf)
  895. {
  896. unsigned char result = FAIL;
  897. unsigned char tx[9] = {0xaa, 0x00, targetAddr, CMD_CONFIG_RELAY_OUTPUT, 0x02, 0x00, 0x00, 0x00, 0x00};
  898. unsigned char rx[512];
  899. unsigned char chksum = 0x00;
  900. for(int idx_connector=0;idx_connector<2;idx_connector++)
  901. for(int idx = 0;idx<8;idx++)
  902. tx[6+idx_connector] |= ((Set_Buf->relay_status[idx_connector][idx]?0x01:0x00)<<idx);
  903. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  904. chksum ^= tx[6+idx];
  905. tx[8] = chksum;
  906. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  907. if(len > 6)
  908. {
  909. if (len < 6+(rx[4] | rx[5]<<8))
  910. return result;
  911. chksum = 0x00;
  912. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  913. {
  914. chksum ^= rx[6+idx];
  915. }
  916. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  917. (rx[2] == tx[1]) &&
  918. (rx[1] == tx[2]) &&
  919. (rx[3] == tx[3]) &&
  920. (rx[6] == 0x01))
  921. {
  922. result = PASS;
  923. }
  924. }
  925. return result;
  926. }
  927. unsigned char Config_Gpio_Output(unsigned char fd, unsigned char targetAddr, Gpio_out *Set_Buf)
  928. {
  929. unsigned char result = FAIL;
  930. unsigned char tx[9] = {0xaa, 0x00, targetAddr, CMD_CONFIG_GPIO_OUTPUT, 0x01, 0x00, 0x00, 0x00};
  931. unsigned char rx[512];
  932. unsigned char chksum = 0x00;
  933. tx[6] |= (Set_Buf->AC_Connector?0x01:0x00);
  934. for(int idx=0;idx<2;idx++)
  935. tx[6] |= (Set_Buf->Button_LED[idx]?0x01:0x00)<<(1+idx);
  936. for(int idx=0;idx<4;idx++)
  937. tx[6] |= (Set_Buf->System_LED[idx]?0x01:0x00)<<(3+idx);
  938. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  939. chksum ^= tx[6+idx];
  940. tx[14] = chksum;
  941. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  942. if(len > 6)
  943. {
  944. if (len < 6+(rx[4] | rx[5]<<8))
  945. return result;
  946. chksum = 0x00;
  947. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  948. {
  949. chksum ^= rx[6+idx];
  950. }
  951. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  952. (rx[2] == tx[1]) &&
  953. (rx[1] == tx[2]) &&
  954. (rx[3] == tx[3]) &&
  955. (rx[6] == tx[6]))
  956. {
  957. result = PASS;
  958. }
  959. }
  960. return result;
  961. }
  962. unsigned char Config_RTC(unsigned char fd, unsigned char targetAddr, Rtc *Set_Buf)
  963. {
  964. unsigned char result = FAIL;
  965. unsigned char tx[21];
  966. unsigned char rx[512];
  967. unsigned char chksum = 0x00;
  968. tx[0] = 0xaa;
  969. tx[1] = 0x00;
  970. tx[2] = targetAddr;
  971. tx[3] = CMD_CONFIG_RTC;
  972. tx[4] = 0x0e;
  973. tx[5] = 0x00;
  974. tx[6] = ((Set_Buf->year)/1000)+'0';
  975. tx[7] = ((Set_Buf->year)/100%10)+'0';
  976. tx[8] = ((Set_Buf->year)/10%10)+'0';
  977. tx[9] = ((Set_Buf->year)%10)+'0';
  978. tx[10] = ((Set_Buf->month)/10)+'0';
  979. tx[11] = ((Set_Buf->month)%10)+'0';
  980. tx[12] = ((Set_Buf->day)/10)+'0';
  981. tx[13] = ((Set_Buf->day)%10)+'0';
  982. tx[14] = ((Set_Buf->hour)/10)+'0';
  983. tx[15] = ((Set_Buf->hour)%10)+'0';
  984. tx[16] = ((Set_Buf->min)/10)+'0';
  985. tx[17] = ((Set_Buf->min)%10)+'0';
  986. tx[18] = ((Set_Buf->sec)/10)+'0';
  987. tx[19] = ((Set_Buf->sec)%10)+'0';
  988. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  989. chksum ^= tx[6+idx];
  990. tx[20] = chksum;
  991. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  992. if(len > 6)
  993. {
  994. if (len < 6+(rx[4] | rx[5]<<8))
  995. return result;
  996. chksum = 0x00;
  997. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  998. {
  999. chksum ^= rx[6+idx];
  1000. }
  1001. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1002. (rx[2] == tx[1]) &&
  1003. (rx[1] == tx[2]) &&
  1004. (rx[3] == tx[3]) &&
  1005. (rx[6] == 0x01))
  1006. {
  1007. result = PASS;
  1008. }
  1009. }
  1010. return result;
  1011. }
  1012. unsigned char Config_AC_MCU_LED(unsigned char fd, unsigned char targetAddr, Ac_Primary_Mcu_Led *Set_Buf)
  1013. {
  1014. unsigned char result = FAIL;
  1015. unsigned char tx[12] ;
  1016. unsigned char rx[512];
  1017. unsigned char chksum = 0x00;
  1018. tx[0] = 0xaa;
  1019. tx[1] = 0x00;
  1020. tx[2] = targetAddr;
  1021. tx[3] = CMD_CONFIG_AC_LED;
  1022. tx[4] = 0x05;
  1023. tx[5] = 0x00;
  1024. tx[6] = Set_Buf->mode;
  1025. tx[7] = ((Set_Buf->alarm_code>>0)&0xff);
  1026. tx[8] = ((Set_Buf->alarm_code>>8)&0xff);
  1027. tx[9] = ((Set_Buf->alarm_code>>16)&0xff);
  1028. tx[10] = ((Set_Buf->alarm_code>>24)&0xff);
  1029. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1030. chksum ^= tx[6+idx];
  1031. tx[11] = chksum;
  1032. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1033. if(len > 6)
  1034. {
  1035. if (len < 6+(rx[4] | rx[5]<<8))
  1036. return result;
  1037. chksum = 0x00;
  1038. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1039. {
  1040. chksum ^= rx[6+idx];
  1041. }
  1042. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1043. (rx[2] == tx[1]) &&
  1044. (rx[1] == tx[2]) &&
  1045. (rx[3] == tx[3]) &&
  1046. (rx[6] == 0x01))
  1047. {
  1048. result = PASS;
  1049. }
  1050. }
  1051. return result;
  1052. }
  1053. unsigned char Config_AC_MCU_LEGACY_REQUEST(unsigned char fd, unsigned char targetAddr, Legacy_Request *Set_Buf)
  1054. {
  1055. unsigned char result = FAIL;
  1056. unsigned char tx[9];
  1057. unsigned char rx[512];
  1058. unsigned char chksum = 0x00;
  1059. tx[0] = 0xaa;
  1060. tx[1] = 0x00;
  1061. tx[2] = targetAddr;
  1062. tx[3] = CMD_CONFIG_MCU_MODE;
  1063. tx[4] = 0x02;
  1064. tx[5] = 0x00;
  1065. tx[6] = Set_Buf->isLegacyRequest;
  1066. tx[7] = 0x00;
  1067. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1068. chksum ^= tx[6+idx];
  1069. tx[8] = chksum;
  1070. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1071. if(len > 6)
  1072. {
  1073. if (len < 6+(rx[4] | rx[5]<<8))
  1074. return result;
  1075. chksum = 0x00;
  1076. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1077. {
  1078. chksum ^= rx[6+idx];
  1079. }
  1080. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1081. (rx[2] == tx[1]) &&
  1082. (rx[1] == tx[2]) &&
  1083. (rx[3] == tx[3]) &&
  1084. (rx[6] == 0x01))
  1085. {
  1086. result = PASS;
  1087. }
  1088. }
  1089. return result;
  1090. }
  1091. unsigned char Config_AC_MCU_RESET_REQUEST(unsigned char fd, unsigned char targetAddr, Mcu_Reset_Request *Set_Buf)
  1092. {
  1093. unsigned char result = FAIL;
  1094. unsigned char tx[9];
  1095. unsigned char rx[512];
  1096. unsigned char chksum = 0x00;
  1097. tx[0] = 0xaa;
  1098. tx[1] = 0x00;
  1099. tx[2] = targetAddr;
  1100. tx[3] = CMD_CONFIG_MCU_RESET_REQUEST;
  1101. tx[4] = 0x02;
  1102. tx[5] = 0x00;
  1103. tx[6] = Set_Buf->isMcuResetRequest;
  1104. tx[7] = 0x00;
  1105. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1106. chksum ^= tx[6+idx];
  1107. tx[8] = chksum;
  1108. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1109. if(len > 6)
  1110. {
  1111. if (len < 6+(rx[4] | rx[5]<<8))
  1112. return result;
  1113. chksum = 0x00;
  1114. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1115. {
  1116. chksum ^= rx[6+idx];
  1117. }
  1118. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1119. (rx[2] == tx[1]) &&
  1120. (rx[1] == tx[2]) &&
  1121. (rx[3] == tx[3]) &&
  1122. (rx[6] == 0x01))
  1123. {
  1124. result = PASS;
  1125. }
  1126. }
  1127. return result;
  1128. }
  1129. unsigned char Query_AC_GUN_PLUGIN_TIMES(unsigned char fd, unsigned char targetAddr, Gun_Plugin_Times *Ret_Buf)
  1130. {
  1131. unsigned char result = FAIL;
  1132. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_GUN_PLUGIN_TIMES, 0x00, 0x00, 0x00};
  1133. unsigned char rx[512];
  1134. unsigned char chksum = 0x00;
  1135. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1136. if(len > 6)
  1137. {
  1138. if(len < 6+(rx[4] | rx[5]<<8))
  1139. return result;
  1140. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1141. {
  1142. chksum ^= rx[6+idx];
  1143. }
  1144. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1145. (rx[2] == tx[1]) &&
  1146. (rx[1] == tx[2]) &&
  1147. (rx[3] == tx[3]))
  1148. {
  1149. Ret_Buf-> GunPluginTimes = ((uint32_t)rx[6] | (((uint32_t)rx[7])<<8) | (((uint32_t)rx[8])<<16) | (((uint32_t)rx[9])<<24));
  1150. result = PASS;
  1151. }
  1152. }
  1153. return result;
  1154. }
  1155. unsigned char Config_AC_MaxCurrent_And_CpPwmDuty(unsigned char fd, unsigned char targetAddr, Ac_Primary_Mcu_Cp_Pwm_Duty*Set_Buf)
  1156. {
  1157. unsigned char result = FAIL;
  1158. unsigned char tx[8];
  1159. unsigned char rx[512];
  1160. unsigned char chksum = 0x00;
  1161. tx[0] = 0xaa;
  1162. tx[1] = 0x00;
  1163. tx[2] = targetAddr;
  1164. tx[3] = CMD_CONFIG_CURRENT_LINIT;
  1165. tx[4] = 0x01;
  1166. tx[5] = 0x00;
  1167. tx[6] = Set_Buf->max_current;
  1168. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1169. chksum ^= tx[6+idx];
  1170. tx[7] = chksum;
  1171. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1172. if(len > 6)
  1173. {
  1174. chksum = 0x00;
  1175. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1176. {
  1177. chksum ^= rx[6+idx];
  1178. }
  1179. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1180. (rx[2] == tx[1]) &&
  1181. (rx[1] == tx[2]) &&
  1182. (rx[3] == tx[3]) &&
  1183. (rx[6] == 0x01))
  1184. {
  1185. result = PASS;
  1186. }
  1187. }
  1188. return result;
  1189. }
  1190. unsigned char Update_Start(unsigned char fd, unsigned char targetAddr, unsigned int crc32)
  1191. {
  1192. unsigned char result = FAIL;
  1193. 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};
  1194. unsigned char rx[512];
  1195. unsigned char chksum = 0x00;
  1196. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1197. chksum ^= tx[6+idx];
  1198. tx[10] = chksum;
  1199. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1200. if(len > 6)
  1201. {
  1202. if (len < 6+(rx[4] | rx[5]<<8))
  1203. return result;
  1204. chksum = 0x00;
  1205. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  1206. {
  1207. chksum ^= rx[6+idx];
  1208. }
  1209. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1210. (rx[2] == tx[1]) &&
  1211. (rx[1] == tx[2]) &&
  1212. (rx[3] == tx[3]) &&
  1213. (rx[6] == 0x00))
  1214. {
  1215. result = PASS;
  1216. }
  1217. }
  1218. return result;
  1219. }
  1220. unsigned char Update_Abord(unsigned char fd, unsigned char targetAddr)
  1221. {
  1222. unsigned char result = FAIL;
  1223. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_UPDATE_ABOARD, 0x04, 0x00, 0x00};
  1224. unsigned char rx[512];
  1225. unsigned char chksum = 0x00;
  1226. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1227. if(len > 6)
  1228. {
  1229. if (len < 6+(rx[4] | rx[5]<<8))
  1230. return result;
  1231. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  1232. {
  1233. chksum ^= rx[6+idx];
  1234. }
  1235. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1236. (rx[2] == tx[1]) &&
  1237. (rx[1] == tx[2]) &&
  1238. (rx[3] == tx[3]) &&
  1239. (rx[6] == 0x00))
  1240. {
  1241. result = PASS;
  1242. }
  1243. }
  1244. return result;
  1245. }
  1246. unsigned char Update_Transfer(unsigned char fd, unsigned char targetAddr, unsigned int startAddr, unsigned char *data, unsigned short int length)
  1247. {
  1248. unsigned char result = FAIL;
  1249. unsigned char tx[11 + length];
  1250. unsigned char rx[512];
  1251. unsigned char chksum = 0x00;
  1252. tx[0] = 0xaa;
  1253. tx[1] = 0x00;
  1254. tx[2] = targetAddr;
  1255. tx[3] = CMD_UPDATE_TRANSFER;
  1256. tx[4] = (4 + length) & 0xff;
  1257. tx[5] = ((4 + length)>>8) & 0xff;
  1258. tx[6] = (startAddr>>0) & 0xff;
  1259. tx[7] = (startAddr>>8) & 0xff;
  1260. tx[8] = (startAddr>>16) & 0xff;
  1261. tx[9] = (startAddr>>24) & 0xff;
  1262. memcpy(tx+10, data, length);
  1263. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1264. chksum ^= tx[6+idx];
  1265. tx[sizeof(tx)-1] = chksum;
  1266. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1267. if(len > 6)
  1268. {
  1269. if (len < 6+(rx[4] | rx[5]<<8))
  1270. return result;
  1271. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  1272. {
  1273. chksum ^= rx[6+idx];
  1274. }
  1275. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1276. (rx[2] == tx[1]) &&
  1277. (rx[1] == tx[2]) &&
  1278. (rx[3] == tx[3]) &&
  1279. (rx[6] == 0x00))
  1280. {
  1281. result = PASS;
  1282. }
  1283. }
  1284. return result;
  1285. }
  1286. unsigned char Update_Finish(unsigned char fd, unsigned char targetAddr)
  1287. {
  1288. unsigned char result = FAIL;
  1289. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_UPDATE_END, 0x04, 0x00, 0x00};
  1290. unsigned char rx[512];
  1291. unsigned char chksum = 0x00;
  1292. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1293. if(len > 6)
  1294. {
  1295. if (len < 6+(rx[4] | rx[5]<<8))
  1296. return result;
  1297. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1298. {
  1299. chksum ^= rx[6+idx];
  1300. }
  1301. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1302. (rx[2] == tx[1]) &&
  1303. (rx[1] == tx[2]) &&
  1304. (rx[3] == tx[3]) &&
  1305. (rx[6] == 0x01))
  1306. {
  1307. result = PASS;
  1308. }
  1309. }
  1310. return result;
  1311. }
  1312. //================================================
  1313. // Main process
  1314. //================================================
  1315. int main(void)
  1316. {
  1317. int Uart1Fd;
  1318. unsigned short int failCount[2] = {0,0};
  1319. if(InitShareMemory() == FAIL)
  1320. {
  1321. #ifdef SystemLogMessage
  1322. DEBUG_ERROR("InitShareMemory NG\n");
  1323. #endif
  1324. if(ShmStatusCodeData!=NULL)
  1325. {
  1326. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  1327. }
  1328. sleep(5);
  1329. return 0;
  1330. }
  1331. else
  1332. {
  1333. DEBUG_INFO("InitShareMemory OK.\r\n");
  1334. }
  1335. Uart1Fd=InitComPort();
  1336. if(Uart1Fd<0)
  1337. {
  1338. DEBUG_ERROR("InitComPort NG\n");
  1339. if(ShmStatusCodeData!=NULL)
  1340. {
  1341. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1342. }
  1343. sleep(5);
  1344. return 0;
  1345. }
  1346. else
  1347. {
  1348. DEBUG_INFO("ttyS1 port open success.\r\n");
  1349. }
  1350. for(;;)
  1351. {
  1352. for(int gun_index=0;gun_index<AC_QUANTITY;gun_index++)
  1353. {
  1354. /*
  1355. * Polling loop
  1356. */
  1357. if((stepIndex%2)==0)
  1358. {
  1359. /*
  1360. * High priority communication
  1361. */
  1362. DEBUG_INFO("========== High priority polling ==========\r\n");
  1363. //===============================
  1364. // Config primary MCU LED
  1365. //===============================
  1366. ShmCharger->gun_info[gun_index].primaryMcuLed.alarm_code = ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode;
  1367. if(Config_AC_MCU_LED(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].primaryMcuLed))
  1368. {
  1369. DEBUG_INFO("MCU-%d set Led mode : %d\r\n",gun_index, ShmCharger->gun_info[gun_index].primaryMcuLed.mode);
  1370. DEBUG_INFO("MCU-%d set Alarm code : %x\r\n",gun_index, ShmCharger->gun_info[gun_index].primaryMcuLed.alarm_code);
  1371. failCount[gun_index] = 0;
  1372. }
  1373. else
  1374. {
  1375. DEBUG_WARN("MCU-%d set led fail...%d\r\n", gun_index, failCount[gun_index]);
  1376. if(failCount[gun_index]<1000)
  1377. {
  1378. failCount[gun_index]++;
  1379. }
  1380. }
  1381. //===============================
  1382. // Config primary Legacy request
  1383. //===============================
  1384. if(Config_AC_MCU_LEGACY_REQUEST(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1),&ShmCharger->gun_info[gun_index].legacyRequest))
  1385. {
  1386. DEBUG_INFO("MCU-%d set relay request : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest);
  1387. failCount[gun_index] = 0;
  1388. }
  1389. else
  1390. {
  1391. DEBUG_WARN("MCU-%d set relay request fail...%d\r\n", gun_index, failCount[gun_index]);
  1392. if(failCount[gun_index]<1000)
  1393. {
  1394. failCount[gun_index]++;
  1395. }
  1396. }
  1397. //===============================
  1398. // Query primary MCU status
  1399. //===============================
  1400. if(Query_AC_MCU_Status(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].primaryMcuState))
  1401. {
  1402. DEBUG_INFO("MCU-%d get Pilot State : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.cp_state);
  1403. DEBUG_INFO("MCU-%d get Pilot Duty : %.2f\r\n", gun_index, (float)ShmCharger->gun_info[gun_index].primaryMcuState.current_limit);
  1404. DEBUG_INFO("MCU-%d get Pilot Voltage Positive : %.2f\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_positive);
  1405. DEBUG_INFO("MCU-%d get Pilot Voltage Negative : %.2f\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_negtive);
  1406. DEBUG_INFO("MCU-%d get Relay State : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.relay_state);
  1407. DEBUG_INFO("MCU-%d get Rating Current : %.2f\r\n", gun_index, (float)ShmCharger->gun_info[gun_index].primaryMcuState.rating_current);
  1408. DEBUG_INFO("MCU-%d get Rotary switch : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch);
  1409. DEBUG_INFO("MCU-%d get Locker State : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.locker_state);
  1410. DEBUG_INFO("MCU-%d get Shutter State : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.shutter_state);
  1411. DEBUG_INFO("MCU-%d get Meter State : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.meter_state);
  1412. DEBUG_INFO("MCU-%d get PP State : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.pp_state);
  1413. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState = ShmCharger->gun_info[gun_index].primaryMcuState.cp_state;
  1414. 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));
  1415. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotVoltage = ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_positive;
  1416. ShmCharger->gun_info[gun_index].PilotVoltage.PilotVoltagePositive = ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_positive;
  1417. ShmCharger->gun_info[gun_index].PilotVoltage.PilotVoltageNegative = ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_negtive;
  1418. failCount[gun_index] = 0;
  1419. }
  1420. else
  1421. {
  1422. DEBUG_WARN("MCU-%d get status fail...%d\r\n", gun_index, failCount[gun_index]);
  1423. if(failCount[gun_index]<1000)
  1424. {
  1425. failCount[gun_index]++;
  1426. }
  1427. }
  1428. //===============================
  1429. // Query primary MCU Alarm code
  1430. //===============================
  1431. if(Query_AC_MCU_Alarm(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].primaryMcuAlarm))
  1432. {
  1433. DEBUG_INFO("MCU-%d get alarm OK...\r\n");
  1434. /*
  1435. // Byte[6]
  1436. // Byte[7]
  1437. // Byte[8] Alarm code 3~7 bits Reserved
  1438. // Byte[9] Alarm code 0~7 bits Reserved
  1439. */
  1440. //================================================
  1441. // Byte[6]
  1442. //================================================
  1443. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OVP == 0x01)
  1444. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<0;
  1445. else
  1446. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<0);
  1447. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.UVP == 0x01)
  1448. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<1;
  1449. else
  1450. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<1);
  1451. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP == 0x01)
  1452. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<2;
  1453. else
  1454. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<2);
  1455. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OTP == 0x01)
  1456. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<3;
  1457. else
  1458. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<3);
  1459. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.gmi_fault == 0x01)
  1460. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<4;
  1461. else
  1462. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<4);
  1463. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.cp_fault == 0x01)
  1464. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<5;
  1465. else
  1466. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<5);
  1467. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.ac_leak == 0x01)
  1468. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<6;
  1469. else
  1470. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<6);
  1471. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.dc_leak == 0x01)
  1472. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<7;
  1473. else
  1474. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<7);
  1475. //================================================
  1476. // Byte[7]
  1477. //================================================
  1478. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.mcu_selftest_fail == 0x01)
  1479. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<8;
  1480. else
  1481. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<8);
  1482. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.handshaking_timeout == 0x01)
  1483. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<9;
  1484. else
  1485. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<9);
  1486. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.emergency_stop == 0x01)
  1487. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<10;
  1488. else
  1489. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<10);
  1490. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.relay_welding == 0x01)
  1491. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<11;
  1492. else
  1493. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<11);
  1494. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.leak_module_fail == 0x01)
  1495. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<12;
  1496. else
  1497. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<12);
  1498. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.shutter_fault == 0x01)
  1499. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<13;
  1500. else
  1501. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<13);
  1502. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.locker_fault == 0x01)
  1503. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<14;
  1504. else
  1505. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<14);
  1506. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.power_drop == 0x01)
  1507. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<15;
  1508. else
  1509. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<15);
  1510. //================================================
  1511. // Byte[8]
  1512. //================================================
  1513. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.circuit_short == 0x01)
  1514. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<16;
  1515. else
  1516. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<16);
  1517. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.set_circuit == 0x01)
  1518. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<17;
  1519. else
  1520. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<17);
  1521. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.relay_drive_fault == 0x01)
  1522. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<18;
  1523. else
  1524. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<18);
  1525. //================================================
  1526. // Byte[9]
  1527. //================================================
  1528. // Debug information print out
  1529. if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode>0))
  1530. {
  1531. DEBUG_INFO("MCU-%d get OVP : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OVP);
  1532. DEBUG_INFO("MCU-%d get UVP : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.UVP);
  1533. DEBUG_INFO("MCU-%d get OCP : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP);
  1534. DEBUG_INFO("MCU-%d get OTP : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OTP);
  1535. DEBUG_INFO("MCU-%d get gmi_fault : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.gmi_fault);
  1536. DEBUG_INFO("MCU-%d get cp_fault : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.cp_fault);
  1537. DEBUG_INFO("MCU-%d get ac_leak : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.ac_leak);
  1538. DEBUG_INFO("MCU-%d get dc_leak : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.dc_leak);
  1539. DEBUG_INFO("MCU-%d get mcu_selftest_fail : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.mcu_selftest_fail);
  1540. DEBUG_INFO("MCU-%d get handshaking_timeout : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.handshaking_timeout);
  1541. DEBUG_INFO("MCU-%d get emergency_stop : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.emergency_stop);
  1542. DEBUG_INFO("MCU-%d get relay_welding : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.relay_welding);
  1543. DEBUG_INFO("MCU-%d get leak_module_fail : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.leak_module_fail);
  1544. DEBUG_INFO("MCU-%d get shutter_fault : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.shutter_fault);
  1545. DEBUG_INFO("MCU-%d get locker_fault : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.locker_fault);
  1546. DEBUG_INFO("MCU-%d get power_drop : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.power_drop);
  1547. DEBUG_INFO("MCU-%d get circuit_short : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.circuit_short);
  1548. DEBUG_INFO("MCU-%d get set_circuit : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.set_circuit);
  1549. DEBUG_INFO("MCU-%d get relay_drive_fault : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.relay_drive_fault);
  1550. DEBUG_INFO("MCU-%d get InputAlarmCode : %x\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode);
  1551. }
  1552. failCount[gun_index] = 0;
  1553. }
  1554. else
  1555. {
  1556. DEBUG_WARN("MCU-%d get alarm fail...%d\r\n", gun_index, failCount[gun_index]);
  1557. if(failCount[gun_index]<1000)
  1558. {
  1559. failCount[gun_index]++;
  1560. }
  1561. }
  1562. //===============================
  1563. // Query primary MCU BLE config
  1564. //===============================
  1565. if(Query_Ble_Config(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].bleConfigData))
  1566. {
  1567. if(ShmCharger->gun_info[gun_index].bleConfigData.isLogin == ON)
  1568. {
  1569. DEBUG_INFO("MCU-%d get isUserLogin : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].bleConfigData.isLogin);
  1570. DEBUG_INFO("MCU-%d get isRequestStartCharger : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart);
  1571. DEBUG_INFO("MCU-%d get isRequestStopCharger : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop);
  1572. }
  1573. ShmSysConfigAndInfo->SysConfig.Bluetooth.isLogin = ShmCharger->gun_info[gun_index].bleConfigData.isLogin;
  1574. ShmSysConfigAndInfo->SysConfig.Bluetooth.isRequestStart = ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart;
  1575. ShmSysConfigAndInfo->SysConfig.Bluetooth.isRequestStop = ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop;
  1576. failCount[gun_index] = 0;
  1577. }
  1578. else
  1579. {
  1580. DEBUG_WARN("MCU-%d get ble config fail...%d\r\n", gun_index, failCount[gun_index]);
  1581. if(failCount[gun_index]<1000)
  1582. {
  1583. failCount[gun_index]++;
  1584. }
  1585. }
  1586. //===============================
  1587. // Query primary MCU ble login id
  1588. //===============================
  1589. if(Query_Ble_Central_ID(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].bleLoginCentralId))
  1590. {
  1591. DEBUG_INFO("MCU-%d get ble central id : %s\r\n", gun_index, ShmCharger->gun_info[gun_index].bleLoginCentralId.id);
  1592. memcpy(ShmSysConfigAndInfo->SysConfig.Bluetooth.LoginCentralID, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, sizeof ShmSysConfigAndInfo->SysConfig.Bluetooth.LoginCentralID);
  1593. failCount[gun_index] = 0;
  1594. }
  1595. else
  1596. {
  1597. DEBUG_WARN("MCU-%d get ble login central id fail...%d\r\n", gun_index, failCount[gun_index]);
  1598. if(failCount[gun_index]<1000)
  1599. {
  1600. failCount[gun_index]++;
  1601. }
  1602. }
  1603. //===============================
  1604. // Config primary MCU reset request
  1605. //===============================
  1606. if(ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest == ON)
  1607. {
  1608. 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)
  1609. {
  1610. DEBUG_INFO("MCU-%d set MCU reset Request : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest);
  1611. ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest = OFF;
  1612. failCount[gun_index] = 0;
  1613. }
  1614. else
  1615. {
  1616. DEBUG_WARN("MCU-%d get MCU reset fail...%d\r\n", gun_index, failCount[gun_index]);
  1617. if(failCount[gun_index]<1000)
  1618. {
  1619. failCount[gun_index]++;
  1620. }
  1621. }
  1622. }
  1623. }
  1624. else
  1625. {
  1626. /*
  1627. * Normal priority communication
  1628. */
  1629. DEBUG_INFO("========== Normal priority polling ==========\r\n");
  1630. switch(stepIndex)
  1631. {
  1632. case 1:
  1633. //===============================
  1634. // Query present input voltage
  1635. //===============================
  1636. if(Query_Present_InputVoltage(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].inputVoltage) == PASS)
  1637. {
  1638. DEBUG_INFO("MCU-%d get input voltage : %f\r\n", gun_index, (float)ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12);
  1639. ShmSysConfigAndInfo->SysInfo.InputVoltageR = ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12;
  1640. ShmSysConfigAndInfo->SysInfo.InputVoltageS = ShmCharger->gun_info[gun_index].inputVoltage.L2N_L23;
  1641. ShmSysConfigAndInfo->SysInfo.InputVoltageT = ShmCharger->gun_info[gun_index].inputVoltage.L3N_L31;
  1642. failCount[gun_index] = 0;
  1643. }
  1644. else
  1645. {
  1646. DEBUG_WARN("MCU-%d get input voltage fail...%d\r\n", gun_index, failCount[gun_index]);
  1647. if(failCount[gun_index]<1000)
  1648. {
  1649. failCount[gun_index]++;
  1650. }
  1651. }
  1652. break;
  1653. case 3:
  1654. //===============================
  1655. // Query present output current
  1656. //===============================
  1657. if(Query_Present_OutputCurrent(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].outputCurrent) == PASS)
  1658. {
  1659. DEBUG_INFO("MCU-%d get output current : %f\r\n", gun_index, (float)ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0]);
  1660. failCount[gun_index] = 0;
  1661. }
  1662. else
  1663. {
  1664. DEBUG_WARN("MCU-%d get output current fail...%d\r\n", gun_index, failCount[gun_index]);
  1665. if(failCount[gun_index]<1000)
  1666. {
  1667. failCount[gun_index]++;
  1668. }
  1669. }
  1670. break;
  1671. case 5:
  1672. //===============================
  1673. // Query gun plug-in times
  1674. //===============================
  1675. 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)
  1676. {
  1677. DEBUG_INFO("MCU-%d get gun plugin times : %ld\r\n", gun_index, (long)ShmCharger->gun_info[gun_index].gunPluginTimes.GunPluginTimes);
  1678. ShmSysConfigAndInfo->SysConfig.AcPlugInTimes = ((long)ShmCharger->gun_info[gun_index].gunPluginTimes.GunPluginTimes & 0xFFFF);
  1679. failCount[gun_index] = 0;
  1680. }
  1681. else
  1682. {
  1683. DEBUG_WARN("MCU-%d get gun plugin times fail...%d\r\n", gun_index, failCount[gun_index]);
  1684. if(failCount[gun_index]<1000)
  1685. {
  1686. failCount[gun_index]++;
  1687. }
  1688. }
  1689. break;
  1690. case 7:
  1691. //===============================
  1692. // Query temperature
  1693. //===============================
  1694. if(Query_Temperature(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].temperature) == PASS)
  1695. {
  1696. DEBUG_INFO("MCU-%d get temperature : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].temperature.point[0]);
  1697. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = ShmCharger->gun_info[gun_index].temperature.point[0];
  1698. failCount[gun_index] = 0;
  1699. }
  1700. else
  1701. {
  1702. DEBUG_WARN("MCU-%d get temperature fail...%d\r\n", gun_index, failCount[gun_index]);
  1703. if(failCount[gun_index]<1000)
  1704. {
  1705. failCount[gun_index]++;
  1706. }
  1707. }
  1708. break;
  1709. case 9:
  1710. //===============================
  1711. // Config primary set CP PWN duty
  1712. //===============================
  1713. 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))
  1714. {
  1715. DEBUG_INFO("MCU-%d cp pwn duty : %d\r\n",gun_index, ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
  1716. failCount[gun_index] = 0;
  1717. }
  1718. else
  1719. {
  1720. DEBUG_WARN("MCU-%d set cp pwn duty fail...%d\r\n", gun_index, failCount[gun_index]);
  1721. if(failCount[gun_index]<1000)
  1722. {
  1723. failCount[gun_index]++;
  1724. }
  1725. }
  1726. break;
  1727. case 11:
  1728. //===============================
  1729. // Query RTC
  1730. //===============================
  1731. if(Query_RTC(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].rtc))
  1732. {
  1733. struct timeb csuTime, mcuTime;
  1734. struct tm *tmCSU;
  1735. struct tm tmMcu;
  1736. ftime(&csuTime);
  1737. tmCSU = localtime(&csuTime.time);
  1738. tmMcu.tm_year = ShmCharger->gun_info[gun_index].rtc.year-1900;
  1739. tmMcu.tm_mon = ShmCharger->gun_info[gun_index].rtc.month-1;
  1740. tmMcu.tm_mday = ShmCharger->gun_info[gun_index].rtc.day;
  1741. tmMcu.tm_hour = ShmCharger->gun_info[gun_index].rtc.hour;
  1742. tmMcu.tm_min = ShmCharger->gun_info[gun_index].rtc.min;
  1743. tmMcu.tm_sec = ShmCharger->gun_info[gun_index].rtc.sec;
  1744. mcuTime.time = mktime(&tmMcu);
  1745. if(ShmCharger->gun_info[gun_index].bleConfigData.isLogin && !ShmOCPP16Data->OcppConnStatus)
  1746. {
  1747. if(abs(DiffTimeb(csuTime, mcuTime)) > 10000)
  1748. {
  1749. char cmdBuf[128];
  1750. sprintf(cmdBuf, "date -u -s \"%04d-%02d-%02d %02d:%02d:%02d\"", ShmCharger->gun_info[gun_index].rtc.year,
  1751. ShmCharger->gun_info[gun_index].rtc.month,
  1752. ShmCharger->gun_info[gun_index].rtc.day,
  1753. ShmCharger->gun_info[gun_index].rtc.hour,
  1754. ShmCharger->gun_info[gun_index].rtc.min,
  1755. ShmCharger->gun_info[gun_index].rtc.sec);
  1756. system(cmdBuf);
  1757. system("hwclock -w -u");
  1758. system("hwclock -s");
  1759. DEBUG_INFO("Sync from MCU-%d rtc OK...%04d-%02d-%02d %02d:%02d:%02d\r\n", gun_index,
  1760. ShmCharger->gun_info[gun_index].rtc.year,
  1761. ShmCharger->gun_info[gun_index].rtc.month,
  1762. ShmCharger->gun_info[gun_index].rtc.day,
  1763. ShmCharger->gun_info[gun_index].rtc.hour,
  1764. ShmCharger->gun_info[gun_index].rtc.min,
  1765. ShmCharger->gun_info[gun_index].rtc.sec);
  1766. }
  1767. }
  1768. else
  1769. {
  1770. if(abs(DiffTimeb(csuTime, mcuTime)) > 10000)
  1771. {
  1772. ShmCharger->gun_info[gun_index].rtc.year = tmCSU->tm_year+1900;
  1773. ShmCharger->gun_info[gun_index].rtc.month = tmCSU->tm_mon+1;
  1774. ShmCharger->gun_info[gun_index].rtc.day = tmCSU->tm_mday;
  1775. ShmCharger->gun_info[gun_index].rtc.hour = tmCSU->tm_hour;
  1776. ShmCharger->gun_info[gun_index].rtc.min = tmCSU->tm_min;
  1777. ShmCharger->gun_info[gun_index].rtc.sec = tmCSU->tm_sec;
  1778. Config_RTC(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].rtc);
  1779. DEBUG_INFO("MCU-%d set rtc OK...%04d-%02d-%02d %02d:%02d:%02d\r\n", gun_index,
  1780. ShmCharger->gun_info[gun_index].rtc.year,
  1781. ShmCharger->gun_info[gun_index].rtc.month,
  1782. ShmCharger->gun_info[gun_index].rtc.day,
  1783. ShmCharger->gun_info[gun_index].rtc.hour,
  1784. ShmCharger->gun_info[gun_index].rtc.min,
  1785. ShmCharger->gun_info[gun_index].rtc.sec);
  1786. }
  1787. }
  1788. failCount[gun_index] = 0;
  1789. }
  1790. else
  1791. {
  1792. DEBUG_WARN("MCU-%d get rtc fail...%d\r\n", gun_index, failCount[gun_index]);
  1793. if(failCount[gun_index]<1000)
  1794. {
  1795. failCount[gun_index]++;
  1796. }
  1797. }
  1798. break;
  1799. case 13:
  1800. //===============================
  1801. // Query primary MCU power consumption
  1802. //===============================
  1803. if(Query_Power_Consumption(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].powerConsumption))
  1804. {
  1805. DEBUG_INFO("MCU-%d get power consumption : %f\r\n",gun_index, ((float)ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0));
  1806. failCount[gun_index] = 0;
  1807. }
  1808. else
  1809. {
  1810. DEBUG_WARN("MCU-%d get power consumption fail...%d\r\n", gun_index, failCount[gun_index]);
  1811. if(failCount[gun_index]<1000)
  1812. {
  1813. failCount[gun_index]++;
  1814. }
  1815. }
  1816. break;
  1817. case 15:
  1818. //===============================
  1819. // Upgrade MCU
  1820. //===============================
  1821. if(ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq)
  1822. {
  1823. unsigned char cmd[512];
  1824. if(Upgrade_UART(Uart1Fd, AC_WALLMOUNT_CONTROLLER, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), ShmCharger->fwUpgradeInfo.location, ShmCharger->fwUpgradeInfo.modelName))
  1825. {
  1826. DEBUG_INFO("MCU-%d upgrade firmware OK...%s\r\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
  1827. sleep(20);
  1828. ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq = OFF;
  1829. failCount[gun_index] = 0;
  1830. }
  1831. else
  1832. {
  1833. DEBUG_WARN("MCU-%d upgrade firmware fail...%d\r\n", gun_index, failCount[gun_index]);
  1834. if(failCount[gun_index]<1000)
  1835. {
  1836. failCount[gun_index]++;
  1837. }
  1838. }
  1839. sprintf((char*)cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  1840. system((char*)cmd);
  1841. }
  1842. break;
  1843. case 17:
  1844. break;
  1845. case 19:
  1846. break;
  1847. case 21:
  1848. //===============================
  1849. // Query firmware version
  1850. //===============================
  1851. if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass != PASS)
  1852. {
  1853. if(Query_FW_Ver(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].ver) == PASS)
  1854. {
  1855. DEBUG_INFO("MCU-%d get firmware version : %s\r\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
  1856. memcpy(ShmPrimaryMcuData->version, ShmCharger->gun_info[gun_index].ver.Version_FW, sizeof(ShmPrimaryMcuData->version));
  1857. ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass = PASS;
  1858. failCount[gun_index] = 0;
  1859. }
  1860. else
  1861. {
  1862. DEBUG_WARN("MCU-%d get firmware version fail...%d\r\n", gun_index, failCount[gun_index]);
  1863. if(failCount[gun_index]<1000)
  1864. {
  1865. failCount[gun_index]++;
  1866. }
  1867. }
  1868. }
  1869. //===============================
  1870. // Config primary MCU serial number
  1871. //===============================
  1872. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass != PASS)
  1873. {
  1874. memcpy(ShmCharger->evseId.serial_number, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmCharger->evseId.serial_number));
  1875. if(Config_Serial_Number(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->evseId))
  1876. {
  1877. DEBUG_INFO("MCU-%d set serial number : %.12s\r\n",gun_index,ShmCharger->evseId.serial_number);
  1878. ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass = PASS;
  1879. failCount[gun_index] = 0;
  1880. }
  1881. else
  1882. {
  1883. DEBUG_WARN("MCU-%d set serial number fail...%d\r\n", gun_index, failCount[gun_index]);
  1884. if(failCount[gun_index]<1000)
  1885. {
  1886. failCount[gun_index]++;
  1887. }
  1888. }
  1889. }
  1890. //===============================
  1891. // Config primary MCU model name
  1892. //===============================
  1893. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass != PASS)
  1894. {
  1895. memcpy(ShmCharger->evseId.model_name, ShmSysConfigAndInfo->SysConfig.ModelName, ARRAY_SIZE(ShmCharger->evseId.model_name));
  1896. if(Config_Model_Name(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->evseId))
  1897. {
  1898. DEBUG_INFO("MCU-%d set model name : %.14s\r\n",gun_index,ShmCharger->evseId.model_name);
  1899. ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass = PASS;
  1900. failCount[gun_index] = 0;
  1901. }
  1902. else
  1903. {
  1904. DEBUG_WARN("MCU-%d set model name fail...%d\r\n", gun_index, failCount[gun_index]);
  1905. if(failCount[gun_index]<1000)
  1906. {
  1907. failCount[gun_index]++;
  1908. }
  1909. }
  1910. }
  1911. break;
  1912. default:
  1913. stepIndex = 0;
  1914. break;
  1915. }
  1916. }
  1917. stepIndex++;
  1918. //===============================
  1919. // Communication fail check
  1920. //===============================
  1921. if(failCount[gun_index] >= FAIL_SPEC_COMM)
  1922. {
  1923. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.comm_timeout == OFF)
  1924. {
  1925. DEBUG_ERROR("Primary MCU-%d communication fault", gun_index);
  1926. ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.comm_timeout = ON;
  1927. }
  1928. }
  1929. else
  1930. {
  1931. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.comm_timeout == ON)
  1932. {
  1933. DEBUG_ERROR("Primary MCU-%d communication recover", gun_index);
  1934. ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.comm_timeout = OFF;
  1935. }
  1936. }
  1937. }
  1938. }
  1939. return FAIL;
  1940. }