Module_InternalComm.c 61 KB

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