Module_InternalComm.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101
  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 ShmCharger
  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. Ret_Buf->rotatory_switch = rx[19];
  611. result = PASS;
  612. }
  613. }
  614. return result;
  615. }
  616. unsigned char Query_AC_MCU_Alarm(unsigned char fd, unsigned char targetAddr, Ac_Primary_Mcu_Alarm *Ret_Buf)
  617. {
  618. unsigned char result = FAIL;
  619. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AC_ALARM, 0x00, 0x00, 0x00};
  620. unsigned char rx[512];
  621. unsigned char chksum = 0x00;
  622. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  623. if(len > 6)
  624. {
  625. if (len < 6+(rx[4] | rx[5]<<8))
  626. return result;
  627. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  628. {
  629. chksum ^= rx[6+idx];
  630. }
  631. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  632. (rx[2] == tx[1]) &&
  633. (rx[1] == tx[2]) &&
  634. (rx[3] == tx[3]))
  635. {
  636. //rx[6]
  637. Ret_Buf->bits.OVP = (((rx[6]>>0)&0x01)?1:0);
  638. Ret_Buf->bits.UVP = (((rx[6]>>1)&0x01)?1:0);
  639. Ret_Buf->bits.OCP = (((rx[6]>>2)&0x01)?1:0);
  640. Ret_Buf->bits.OTP = (((rx[6]>>3)&0x01)?1:0);
  641. Ret_Buf->bits.gmi_fault = (((rx[6]>>4)&0x01)?1:0);
  642. Ret_Buf->bits.cp_fault = (((rx[6]>>5)&0x01)?1:0);
  643. Ret_Buf->bits.ac_leak = (((rx[6]>>6)&0x01)?1:0);
  644. Ret_Buf->bits.dc_leak = (((rx[6]>>7)&0x01)?1:0);
  645. //rx[7]
  646. Ret_Buf->bits.mcu_selftest_fail = (((rx[7]>>0)&0x01)?1:0);
  647. Ret_Buf->bits.handshaking_timeout = (((rx[7]>>1)&0x01)?1:0);
  648. Ret_Buf->bits.emergency_stop = (((rx[7]>>2)&0x01)?1:0);
  649. Ret_Buf->bits.relay_welding = (((rx[7]>>3)&0x01)?1:0);
  650. Ret_Buf->bits.leak_module_fail = (((rx[7]>>4)&0x01)?1:0);
  651. Ret_Buf->bits.shutter_fault = (((rx[7]>>5)&0x01)?1:0);
  652. Ret_Buf->bits.locker_fault = (((rx[7]>>6)&0x01)?1:0);
  653. Ret_Buf->bits.power_drop = (((rx[7]>>7)&0x01)?1:0);
  654. //rx[8] 3~7bits reserved
  655. Ret_Buf->bits.circuit_short = (((rx[8]>>0)&0x01)?1:0);
  656. Ret_Buf->bits.set_circuit = (((rx[8]>>1)&0x01)?1:0);
  657. Ret_Buf->bits.relay_drive_fault = (((rx[8]>>2)&0x01)?1:0);
  658. //rx[9] 0~7bits reserved
  659. result = PASS;
  660. }
  661. }
  662. return result;
  663. }
  664. unsigned char Query_Present_OutputCurrent(unsigned char fd, unsigned char targetAddr, Presentoutputcurrent *Ret_Buf)
  665. {
  666. unsigned char result = FAIL;
  667. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_PRESENT_OUTPUTCURRENT, 0x00, 0x00, 0x00};
  668. unsigned char rx[512];
  669. unsigned char chksum = 0x00;
  670. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  671. if(len > 0)
  672. {
  673. if(len < 6+(rx[4] | rx[5]<<8))
  674. return result;
  675. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  676. {
  677. chksum ^= rx[6+idx];
  678. }
  679. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  680. (rx[2] == tx[1]) &&
  681. (rx[1] == tx[2]) &&
  682. (rx[3] == tx[3]))
  683. {
  684. Ret_Buf->L1N_L12[0] = (rx[6] | (rx[7]<<8))/10.0;
  685. Ret_Buf->L2N_L23[0] = (rx[8] | (rx[9]<<8))/10.0;
  686. Ret_Buf->L3N_L31[0] = (rx[10] | (rx[11]<<8))/10.0;
  687. Ret_Buf->L1N_L12[1] = (rx[12] | (rx[13]<<8))/10.0;
  688. Ret_Buf->L2N_L23[1] = (rx[14] | (rx[15]<<8))/10.0;
  689. Ret_Buf->L3N_L31[1] = (rx[16] | (rx[17]<<8))/10.0;
  690. result = PASS;
  691. }
  692. }
  693. return result;
  694. }
  695. unsigned char Query_Ble_Config(unsigned char fd, unsigned char targetAddr, Ble_Config_Data *Ret_Buf)
  696. {
  697. unsigned char result = FAIL;
  698. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_BLE_CONFIG_DATA, 0x00, 0x00, 0x00};
  699. unsigned char rx[512];
  700. unsigned char chksum = 0x00;
  701. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  702. if(len > 0)
  703. {
  704. if(len < 6+(rx[4] | rx[5]<<8))
  705. return result;
  706. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  707. {
  708. chksum ^= rx[6+idx];
  709. }
  710. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  711. (rx[2] == tx[1]) &&
  712. (rx[1] == tx[2]) &&
  713. (rx[3] == tx[3]))
  714. {
  715. Ret_Buf->isLogin = (rx[6]?0x01:0x00);
  716. Ret_Buf->isRequestStart = (rx[7]?0x01:0x00);
  717. Ret_Buf->isRequestStop = (rx[8]?0x01:0x00);
  718. result = PASS;
  719. }
  720. }
  721. return result;
  722. }
  723. unsigned char Query_Ble_Central_ID(unsigned char fd, unsigned char targetAddr, Ble_Login_Central_Id *Ret_Buf)
  724. {
  725. unsigned char result = FAIL;
  726. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_BLE_CENTRAL_ID, 0x00, 0x00, 0x00};
  727. unsigned char rx[512];
  728. unsigned char chksum = 0x00;
  729. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  730. if(len > 0)
  731. {
  732. if(len < 6+(rx[4] | rx[5]<<8))
  733. return result;
  734. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  735. {
  736. chksum ^= rx[6+idx];
  737. }
  738. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  739. (rx[2] == tx[1]) &&
  740. (rx[1] == tx[2]) &&
  741. (rx[3] == tx[3]))
  742. {
  743. memset(Ret_Buf->id, 0x00, ARRAY_SIZE(Ret_Buf->id));
  744. memcpy(&Ret_Buf->id[0], &rx[6], (rx[4] | (rx[5]<<8)));
  745. result = PASS;
  746. }
  747. }
  748. return result;
  749. }
  750. unsigned char Query_Power_Consumption(unsigned char fd, unsigned char targetAddr, Power_Consumption *Ret_Buf)
  751. {
  752. unsigned char result = FAIL;
  753. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_POWER_CONSUMPTION, 0x00, 0x00, 0x00};
  754. unsigned char rx[512];
  755. unsigned char chksum = 0x00;
  756. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  757. if(len > 0)
  758. {
  759. if(len < 6+(rx[4] | rx[5]<<8))
  760. return result;
  761. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  762. {
  763. chksum ^= rx[6+idx];
  764. }
  765. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  766. (rx[2] == tx[1]) &&
  767. (rx[1] == tx[2]) &&
  768. (rx[3] == tx[3]))
  769. {
  770. Ret_Buf-> power_consumption = ((uint32_t)rx[6] | (((uint32_t)rx[7])<<8) | (((uint32_t)rx[8])<<16) | (((uint32_t)rx[9])<<24));
  771. result = PASS;
  772. }
  773. }
  774. return result;
  775. }
  776. unsigned char Config_Fan_Speed(unsigned char fd, unsigned char targetAddr, FanSpeed *Set_Buf)
  777. {
  778. unsigned char result = FAIL;
  779. 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};
  780. unsigned char rx[512];
  781. unsigned char chksum = 0x00;
  782. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  783. chksum ^= tx[6+idx];
  784. tx[14] = chksum;
  785. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  786. if(len > 6)
  787. {
  788. if (len < 6+(rx[4] | rx[5]<<8))
  789. return result;
  790. chksum = 0x00;
  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. (rx[6] == 0x01))
  800. {
  801. result = PASS;
  802. }
  803. }
  804. return result;
  805. }
  806. unsigned char Config_Serial_Number(unsigned char fd, unsigned char targetAddr, Evse_Id *Set_Buf)
  807. {
  808. unsigned char result = FAIL;
  809. 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};
  810. unsigned char rx[512];
  811. unsigned char chksum = 0x00;
  812. memcpy(&tx[14], &Set_Buf->serial_number[0], ARRAY_SIZE(Set_Buf->serial_number));
  813. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  814. chksum ^= tx[6+idx];
  815. tx[26] = chksum;
  816. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  817. if(len > 6)
  818. {
  819. chksum = 0x00;
  820. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  821. {
  822. chksum ^= rx[6+idx];
  823. }
  824. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  825. (rx[2] == tx[1]) &&
  826. (rx[1] == tx[2]) &&
  827. (rx[3] == tx[3]) &&
  828. (rx[6] == 0x01))
  829. {
  830. result = PASS;
  831. }
  832. }
  833. return result;
  834. }
  835. unsigned char Config_Model_Name(unsigned char fd, unsigned char targetAddr, Evse_Id *Set_Buf)
  836. {
  837. unsigned char result = FAIL;
  838. 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};
  839. unsigned char rx[512];
  840. unsigned char chksum = 0x00;
  841. memcpy(&tx[6], &Set_Buf->model_name[0], ARRAY_SIZE(Set_Buf->model_name));
  842. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  843. chksum ^= tx[6+idx];
  844. tx[20] = chksum;
  845. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  846. if(len > 6)
  847. {
  848. chksum = 0x00;
  849. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  850. {
  851. chksum ^= rx[6+idx];
  852. }
  853. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  854. (rx[2] == tx[1]) &&
  855. (rx[1] == tx[2]) &&
  856. (rx[3] == tx[3]) &&
  857. (rx[6] == 0x01))
  858. {
  859. result = PASS;
  860. }
  861. }
  862. return result;
  863. }
  864. unsigned char Config_Relay_Output(unsigned char fd, unsigned char targetAddr, Relay *Set_Buf)
  865. {
  866. unsigned char result = FAIL;
  867. unsigned char tx[9] = {0xaa, 0x00, targetAddr, CMD_CONFIG_RELAY_OUTPUT, 0x02, 0x00, 0x00, 0x00, 0x00};
  868. unsigned char rx[512];
  869. unsigned char chksum = 0x00;
  870. for(int idx_connector=0;idx_connector<2;idx_connector++)
  871. for(int idx = 0;idx<8;idx++)
  872. tx[6+idx_connector] |= ((Set_Buf->relay_status[idx_connector][idx]?0x01:0x00)<<idx);
  873. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  874. chksum ^= tx[6+idx];
  875. tx[8] = chksum;
  876. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  877. if(len > 6)
  878. {
  879. if (len < 6+(rx[4] | rx[5]<<8))
  880. return result;
  881. chksum = 0x00;
  882. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  883. {
  884. chksum ^= rx[6+idx];
  885. }
  886. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  887. (rx[2] == tx[1]) &&
  888. (rx[1] == tx[2]) &&
  889. (rx[3] == tx[3]) &&
  890. (rx[6] == 0x01))
  891. {
  892. result = PASS;
  893. }
  894. }
  895. return result;
  896. }
  897. unsigned char Config_Gpio_Output(unsigned char fd, unsigned char targetAddr, Gpio_out *Set_Buf)
  898. {
  899. unsigned char result = FAIL;
  900. unsigned char tx[9] = {0xaa, 0x00, targetAddr, CMD_CONFIG_GPIO_OUTPUT, 0x01, 0x00, 0x00, 0x00};
  901. unsigned char rx[512];
  902. unsigned char chksum = 0x00;
  903. tx[6] |= (Set_Buf->AC_Connector?0x01:0x00);
  904. for(int idx=0;idx<2;idx++)
  905. tx[6] |= (Set_Buf->Button_LED[idx]?0x01:0x00)<<(1+idx);
  906. for(int idx=0;idx<4;idx++)
  907. tx[6] |= (Set_Buf->System_LED[idx]?0x01:0x00)<<(3+idx);
  908. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  909. chksum ^= tx[6+idx];
  910. tx[14] = chksum;
  911. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  912. if(len > 6)
  913. {
  914. if (len < 6+(rx[4] | rx[5]<<8))
  915. return result;
  916. chksum = 0x00;
  917. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  918. {
  919. chksum ^= rx[6+idx];
  920. }
  921. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  922. (rx[2] == tx[1]) &&
  923. (rx[1] == tx[2]) &&
  924. (rx[3] == tx[3]) &&
  925. (rx[6] == tx[6]))
  926. {
  927. result = PASS;
  928. }
  929. }
  930. return result;
  931. }
  932. unsigned char Config_RTC(unsigned char fd, unsigned char targetAddr, Rtc *Set_Buf)
  933. {
  934. unsigned char result = FAIL;
  935. unsigned char tx[21];
  936. unsigned char rx[512];
  937. unsigned char chksum = 0x00;
  938. tx[0] = 0xaa;
  939. tx[1] = 0x00;
  940. tx[2] = targetAddr;
  941. tx[3] = CMD_CONFIG_RTC;
  942. tx[4] = 0x0e;
  943. tx[5] = 0x00;
  944. tx[6] = ((Set_Buf->year)/1000)+'0';
  945. tx[7] = ((Set_Buf->year)/100%10)+'0';
  946. tx[8] = ((Set_Buf->year)/10%10)+'0';
  947. tx[9] = (Set_Buf->year)%1000+'0';
  948. tx[10] = ((Set_Buf->month)/10)+'0';
  949. tx[11] = ((Set_Buf->month)%10)+'0';
  950. tx[12] = ((Set_Buf->day)/10)+'0';
  951. tx[13] = ((Set_Buf->day)%10)+'0';
  952. tx[14] = ((Set_Buf->hour)/10)+'0';
  953. tx[15] = ((Set_Buf->hour)%10)+'0';
  954. tx[16] = ((Set_Buf->min)/10)+'0';
  955. tx[17] = ((Set_Buf->min)%10)+'0';
  956. tx[18] = ((Set_Buf->sec)/10)+'0';
  957. tx[19] = ((Set_Buf->sec)%10)+'0';
  958. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  959. chksum ^= tx[6+idx];
  960. tx[20] = chksum;
  961. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  962. if(len > 6)
  963. {
  964. if (len < 6+(rx[4] | rx[5]<<8))
  965. return result;
  966. chksum = 0x00;
  967. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  968. {
  969. chksum ^= rx[6+idx];
  970. }
  971. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  972. (rx[2] == tx[1]) &&
  973. (rx[1] == tx[2]) &&
  974. (rx[3] == tx[3]) &&
  975. (rx[6] == 0x01))
  976. {
  977. result = PASS;
  978. }
  979. }
  980. return result;
  981. }
  982. unsigned char Config_AC_MCU_LED(unsigned char fd, unsigned char targetAddr, Ac_Primary_Mcu_Led *Set_Buf)
  983. {
  984. unsigned char result = FAIL;
  985. unsigned char tx[12] ;
  986. unsigned char rx[512];
  987. unsigned char chksum = 0x00;
  988. tx[0] = 0xaa;
  989. tx[1] = 0x00;
  990. tx[2] = targetAddr;
  991. tx[3] = CMD_CONFIG_AC_LED;
  992. tx[4] = 0x05;
  993. tx[5] = 0x00;
  994. tx[6] = Set_Buf->mode;
  995. tx[7] = ((Set_Buf->alarm_code>>0)&0xff);
  996. tx[8] = ((Set_Buf->alarm_code>>8)&0xff);
  997. tx[9] = ((Set_Buf->alarm_code>>16)&0xff);
  998. tx[10] = ((Set_Buf->alarm_code>>24)&0xff);
  999. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1000. chksum ^= tx[6+idx];
  1001. tx[11] = chksum;
  1002. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1003. if(len > 6)
  1004. {
  1005. if (len < 6+(rx[4] | rx[5]<<8))
  1006. return result;
  1007. chksum = 0x00;
  1008. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1009. {
  1010. chksum ^= rx[6+idx];
  1011. }
  1012. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1013. (rx[2] == tx[1]) &&
  1014. (rx[1] == tx[2]) &&
  1015. (rx[3] == tx[3]) &&
  1016. (rx[6] == 0x01))
  1017. {
  1018. result = PASS;
  1019. }
  1020. }
  1021. return result;
  1022. }
  1023. unsigned char Config_AC_MCU_LEGACY_REQUEST(unsigned char fd, unsigned char targetAddr, Legacy_Request *Set_Buf)
  1024. {
  1025. unsigned char result = FAIL;
  1026. unsigned char tx[9];
  1027. unsigned char rx[512];
  1028. unsigned char chksum = 0x00;
  1029. tx[0] = 0xaa;
  1030. tx[1] = 0x00;
  1031. tx[2] = targetAddr;
  1032. tx[3] = CMD_CONFIG_MCU_MODE;
  1033. tx[4] = 0x02;
  1034. tx[5] = 0x00;
  1035. tx[6] = Set_Buf->isLegacyRequest;
  1036. tx[7] = 0x00;
  1037. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1038. chksum ^= tx[6+idx];
  1039. tx[8] = chksum;
  1040. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1041. if(len > 6)
  1042. {
  1043. if (len < 6+(rx[4] | rx[5]<<8))
  1044. return result;
  1045. chksum = 0x00;
  1046. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1047. {
  1048. chksum ^= rx[6+idx];
  1049. }
  1050. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1051. (rx[2] == tx[1]) &&
  1052. (rx[1] == tx[2]) &&
  1053. (rx[3] == tx[3]) &&
  1054. (rx[6] == 0x01))
  1055. {
  1056. result = PASS;
  1057. }
  1058. }
  1059. return result;
  1060. }
  1061. unsigned char Query_AC_GUN_PLUGIN_TIMES(unsigned char fd, unsigned char targetAddr, Gun_Plugin_Times *Ret_Buf)
  1062. {
  1063. unsigned char result = FAIL;
  1064. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_CONFIG_GUN_PLUGIN_TIMES, 0x00, 0x00, 0x00};
  1065. unsigned char rx[512];
  1066. unsigned char chksum = 0x00;
  1067. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1068. if(len > 0)
  1069. {
  1070. if(len < 6+(rx[4] | rx[5]<<8))
  1071. return result;
  1072. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1073. {
  1074. chksum ^= rx[6+idx];
  1075. }
  1076. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1077. (rx[2] == tx[1]) &&
  1078. (rx[1] == tx[2]) &&
  1079. (rx[3] == tx[3]))
  1080. {
  1081. Ret_Buf-> GunPluginTimes = ((uint32_t)rx[6] | (((uint32_t)rx[7])<<8) | (((uint32_t)rx[8])<<16) | (((uint32_t)rx[9])<<24));
  1082. result = PASS;
  1083. }
  1084. }
  1085. return result;
  1086. }
  1087. unsigned char Config_AC_MaxCurrent_And_CpPwmDuty(unsigned char fd, unsigned char targetAddr, Ac_Primary_Mcu_Cp_Pwm_Duty*Set_Buf)
  1088. {
  1089. unsigned char result = FAIL;
  1090. unsigned char tx[9];
  1091. unsigned char rx[512];
  1092. unsigned char chksum = 0x00;
  1093. tx[0] = 0xaa;
  1094. tx[1] = 0x00;
  1095. tx[2] = targetAddr;
  1096. tx[3] = CMD_CONFIG_CURRENT_LINIT;
  1097. tx[4] = 0x05;
  1098. tx[5] = 0x00;
  1099. tx[6] = Set_Buf->max_current;
  1100. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1101. chksum ^= tx[6+idx];
  1102. tx[8] = chksum;
  1103. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1104. if(len > 6)
  1105. {
  1106. chksum = 0x00;
  1107. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1108. {
  1109. chksum ^= rx[6+idx];
  1110. }
  1111. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1112. (rx[2] == tx[1]) &&
  1113. (rx[1] == tx[2]) &&
  1114. (rx[3] == tx[3]) &&
  1115. (rx[6] == 0x01))
  1116. {
  1117. result = PASS;
  1118. }
  1119. }
  1120. return result;
  1121. }
  1122. unsigned char Update_Start(unsigned char fd, unsigned char targetAddr, unsigned int crc32)
  1123. {
  1124. unsigned char result = FAIL;
  1125. 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};
  1126. unsigned char rx[512];
  1127. unsigned char chksum = 0x00;
  1128. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1129. chksum ^= tx[6+idx];
  1130. tx[10] = chksum;
  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. chksum = 0x00;
  1137. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  1138. {
  1139. chksum ^= rx[6+idx];
  1140. }
  1141. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1142. (rx[2] == tx[1]) &&
  1143. (rx[1] == tx[2]) &&
  1144. (rx[3] == tx[3]) &&
  1145. (rx[6] == 0x00))
  1146. {
  1147. result = PASS;
  1148. }
  1149. }
  1150. return result;
  1151. }
  1152. unsigned char Update_Abord(unsigned char fd, unsigned char targetAddr)
  1153. {
  1154. unsigned char result = FAIL;
  1155. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_UPDATE_ABOARD, 0x04, 0x00, 0x00};
  1156. unsigned char rx[512];
  1157. unsigned char chksum = 0x00;
  1158. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1159. if(len > 6)
  1160. {
  1161. if (len < 6+(rx[4] | rx[5]<<8))
  1162. return result;
  1163. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  1164. {
  1165. chksum ^= rx[6+idx];
  1166. }
  1167. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1168. (rx[2] == tx[1]) &&
  1169. (rx[1] == tx[2]) &&
  1170. (rx[3] == tx[3]) &&
  1171. (rx[6] == 0x00))
  1172. {
  1173. result = PASS;
  1174. }
  1175. }
  1176. return result;
  1177. }
  1178. unsigned char Update_Transfer(unsigned char fd, unsigned char targetAddr, unsigned int startAddr, unsigned char *data, unsigned short int length)
  1179. {
  1180. unsigned char result = FAIL;
  1181. unsigned char tx[11 + length];
  1182. unsigned char rx[512];
  1183. unsigned char chksum = 0x00;
  1184. tx[0] = 0xaa;
  1185. tx[1] = 0x00;
  1186. tx[2] = targetAddr;
  1187. tx[3] = CMD_UPDATE_TRANSFER;
  1188. tx[4] = (4 + length) & 0xff;
  1189. tx[5] = ((4 + length)>>8) & 0xff;
  1190. tx[6] = (startAddr>>0) & 0xff;
  1191. tx[7] = (startAddr>>8) & 0xff;
  1192. tx[8] = (startAddr>>16) & 0xff;
  1193. tx[9] = (startAddr>>24) & 0xff;
  1194. memcpy(tx+10, data, length);
  1195. for(int idx=0;idx<(tx[4] | tx[5]<<8);idx++)
  1196. chksum ^= tx[6+idx];
  1197. tx[sizeof(tx)-1] = chksum;
  1198. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1199. if(len > 6)
  1200. {
  1201. if (len < 6+(rx[4] | rx[5]<<8))
  1202. return result;
  1203. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  1204. {
  1205. chksum ^= rx[6+idx];
  1206. }
  1207. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1208. (rx[2] == tx[1]) &&
  1209. (rx[1] == tx[2]) &&
  1210. (rx[3] == tx[3]) &&
  1211. (rx[6] == 0x00))
  1212. {
  1213. result = PASS;
  1214. }
  1215. }
  1216. return result;
  1217. }
  1218. unsigned char Update_Finish(unsigned char fd, unsigned char targetAddr)
  1219. {
  1220. unsigned char result = FAIL;
  1221. unsigned char tx[7] = {0xaa, 0x00, targetAddr, CMD_UPDATE_END, 0x04, 0x00, 0x00};
  1222. unsigned char rx[512];
  1223. unsigned char chksum = 0x00;
  1224. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  1225. if(len > 6)
  1226. {
  1227. if (len < 6+(rx[4] | rx[5]<<8))
  1228. return result;
  1229. for(int idx=0;idx<(rx[4] | rx[5]<<8);idx++)
  1230. {
  1231. chksum ^= rx[6+idx];
  1232. }
  1233. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  1234. (rx[2] == tx[1]) &&
  1235. (rx[1] == tx[2]) &&
  1236. (rx[3] == tx[3]) &&
  1237. (rx[6] == 0x01))
  1238. {
  1239. result = PASS;
  1240. }
  1241. }
  1242. return result;
  1243. }
  1244. //================================================
  1245. // Main process
  1246. //================================================
  1247. int main(void)
  1248. {
  1249. int Uart1Fd;
  1250. unsigned short int failCount[2] = {0,0};
  1251. if(InitShareMemory() == FAIL)
  1252. {
  1253. #ifdef SystemLogMessage
  1254. DEBUG_ERROR("InitShareMemory NG\n");
  1255. #endif
  1256. if(ShmStatusCodeData!=NULL)
  1257. {
  1258. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  1259. }
  1260. sleep(5);
  1261. return 0;
  1262. }
  1263. else
  1264. {
  1265. DEBUG_INFO("InitShareMemory OK.\r\n");
  1266. }
  1267. Uart1Fd=InitComPort();
  1268. if(Uart1Fd<0)
  1269. {
  1270. DEBUG_ERROR("InitComPort NG\n");
  1271. if(ShmStatusCodeData!=NULL)
  1272. {
  1273. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  1274. }
  1275. sleep(5);
  1276. return 0;
  1277. }
  1278. else
  1279. {
  1280. DEBUG_INFO("ttyS1 port open success.\r\n");
  1281. }
  1282. for(;;)
  1283. {
  1284. for(int gun_index=0;gun_index<AC_QUANTITY;gun_index++)
  1285. {
  1286. //===============================
  1287. // Query firmware version
  1288. //===============================
  1289. if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass != PASS)
  1290. {
  1291. if(Query_FW_Ver(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].ver) == PASS)
  1292. {
  1293. DEBUG_INFO("MCU-%d get firmware version : %s\r\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
  1294. memcpy(ShmPrimaryMcuData->version, ShmCharger->gun_info[gun_index].ver.Version_FW, sizeof(ShmPrimaryMcuData->version));
  1295. ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass = PASS;
  1296. failCount[gun_index] = 0;
  1297. }
  1298. else
  1299. {
  1300. DEBUG_WARN("MCU-%d get firmware version fail...%d\r\n", gun_index, failCount[gun_index]);
  1301. if(failCount[gun_index]<1000)
  1302. {
  1303. failCount[gun_index]++;
  1304. }
  1305. }
  1306. }
  1307. //===============================
  1308. // Config primary MCU serial number
  1309. //===============================
  1310. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass != PASS)
  1311. {
  1312. memcpy(ShmCharger->evseId.serial_number, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmCharger->evseId.serial_number));
  1313. if(Config_Serial_Number(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->evseId))
  1314. {
  1315. DEBUG_INFO("MCU-%d set serial number : %.12s\r\n",gun_index,ShmCharger->evseId.serial_number);
  1316. ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass = PASS;
  1317. failCount[gun_index] = 0;
  1318. }
  1319. else
  1320. {
  1321. DEBUG_WARN("MCU-%d set serial number fail...%d\r\n", gun_index, failCount[gun_index]);
  1322. if(failCount[gun_index]<1000)
  1323. {
  1324. failCount[gun_index]++;
  1325. }
  1326. }
  1327. }
  1328. //===============================
  1329. // Config primary MCU model name
  1330. //===============================
  1331. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass != PASS)
  1332. {
  1333. memcpy(ShmCharger->evseId.model_name, ShmSysConfigAndInfo->SysConfig.ModelName, ARRAY_SIZE(ShmCharger->evseId.model_name));
  1334. if(Config_Model_Name(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->evseId))
  1335. {
  1336. DEBUG_INFO("MCU-%d set model name : %.14s\r\n",gun_index,ShmCharger->evseId.model_name);
  1337. ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass = PASS;
  1338. failCount[gun_index] = 0;
  1339. }
  1340. else
  1341. {
  1342. DEBUG_WARN("MCU-%d set model name fail...%d\r\n", gun_index, failCount[gun_index]);
  1343. if(failCount[gun_index]<1000)
  1344. {
  1345. failCount[gun_index]++;
  1346. }
  1347. }
  1348. }
  1349. //===============================
  1350. // Query present input voltage
  1351. //===============================
  1352. if(Query_Present_InputVoltage(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].inputVoltage) == PASS)
  1353. {
  1354. DEBUG_INFO("MCU-%d get input voltage : %f\r\n", gun_index, (float)ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12);
  1355. ShmSysConfigAndInfo->SysInfo.InputVoltageR = ShmCharger->gun_info[gun_index].inputVoltage.L1N_L12;
  1356. ShmSysConfigAndInfo->SysInfo.InputVoltageS = ShmCharger->gun_info[gun_index].inputVoltage.L2N_L23;
  1357. ShmSysConfigAndInfo->SysInfo.InputVoltageT = ShmCharger->gun_info[gun_index].inputVoltage.L3N_L31;
  1358. failCount[gun_index] = 0;
  1359. }
  1360. else
  1361. {
  1362. DEBUG_WARN("MCU-%d get input voltage fail...%d\r\n", gun_index, failCount[gun_index]);
  1363. if(failCount[gun_index]<1000)
  1364. {
  1365. failCount[gun_index]++;
  1366. }
  1367. }
  1368. //===============================
  1369. // Query present output current
  1370. //===============================
  1371. if(Query_Present_OutputCurrent(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].outputCurrent) == PASS)
  1372. {
  1373. DEBUG_INFO("MCU-%d get output current : %f\r\n", gun_index, (float)ShmCharger->gun_info[gun_index].outputCurrent.L1N_L12[0]);
  1374. failCount[gun_index] = 0;
  1375. }
  1376. else
  1377. {
  1378. DEBUG_WARN("MCU-%d get output current fail...%d\r\n", gun_index, failCount[gun_index]);
  1379. if(failCount[gun_index]<1000)
  1380. failCount[gun_index]++;
  1381. }
  1382. //===============================
  1383. // Query gun plug-in times
  1384. //===============================
  1385. 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)
  1386. {
  1387. DEBUG_INFO("MCU-%d get gun plugin times : %.2f\r\n", gun_index, (float)ShmCharger->gun_info[gun_index].gunPluginTimes.GunPluginTimes);
  1388. ShmSysConfigAndInfo->SysConfig.AcPlugInTimes = (float)ShmCharger->gun_info[gun_index].gunPluginTimes.GunPluginTimes;
  1389. failCount[gun_index] = 0;
  1390. }
  1391. else
  1392. {
  1393. DEBUG_WARN("MCU-%d get gun plugin times fail...%d\r\n", gun_index, failCount[gun_index]);
  1394. if(failCount[gun_index]<1000)
  1395. failCount[gun_index]++;
  1396. }
  1397. //===============================
  1398. // Query temperature
  1399. //===============================
  1400. if(Query_Temperature(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].temperature) == PASS)
  1401. {
  1402. DEBUG_INFO("MCU-%d get temperature : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].temperature.point[0]);
  1403. ShmSysConfigAndInfo->SysInfo.SystemAmbientTemp = ShmCharger->gun_info[gun_index].temperature.point[0];
  1404. failCount[gun_index] = 0;
  1405. }
  1406. else
  1407. {
  1408. DEBUG_WARN("MCU-%d get temperature fail...%d\r\n", gun_index, failCount[gun_index]);
  1409. if(failCount[gun_index]<1000)
  1410. {
  1411. failCount[gun_index]++;
  1412. }
  1413. }
  1414. //===============================
  1415. // Query primary MCU status
  1416. //===============================
  1417. if(Query_AC_MCU_Status(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].primaryMcuState))
  1418. {
  1419. DEBUG_INFO("MCU-%d get Pilot State : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.cp_state);
  1420. DEBUG_INFO("MCU-%d get Pilot Duty : %.2f\r\n", gun_index, (float)ShmCharger->gun_info[gun_index].primaryMcuState.current_limit);
  1421. DEBUG_INFO("MCU-%d get Pilot Voltage Positive : %.2f\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_positive);
  1422. DEBUG_INFO("MCU-%d get Pilot Voltage Negative : %.2f\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_negtive);
  1423. DEBUG_INFO("MCU-%d get Locker State : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.locker_state);
  1424. DEBUG_INFO("MCU-%d get Relay State : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.relay_state);
  1425. DEBUG_INFO("MCU-%d get Shutter State : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.shutter_state);
  1426. DEBUG_INFO("MCU-%d get Meter State : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.meter_state);
  1427. DEBUG_INFO("MCU-%d get PP State : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.pp_state);
  1428. DEBUG_INFO("MCU-%d get Rating Current : %.2f\r\n", gun_index, (float)ShmCharger->gun_info[gun_index].primaryMcuState.rating_current);
  1429. DEBUG_INFO("MCU-%d get Rotatory switch : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch);
  1430. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState = ShmCharger->gun_info[gun_index].primaryMcuState.cp_state;
  1431. 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));
  1432. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotVoltage = ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_positive;
  1433. ShmCharger->gun_info[gun_index].PilotVoltage.PilotVoltagePositive = ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_positive;
  1434. ShmCharger->gun_info[gun_index].PilotVoltage.PilotVoltageNegative = ShmCharger->gun_info[gun_index].primaryMcuState.cp_voltage_negtive;
  1435. failCount[gun_index] = 0;
  1436. }
  1437. else
  1438. {
  1439. DEBUG_WARN("MCU-%d get status fail...%d\r\n", gun_index, failCount[gun_index]);
  1440. if(failCount[gun_index]<1000)
  1441. {
  1442. failCount[gun_index]++;
  1443. }
  1444. }
  1445. //===============================
  1446. // Query primary MCU Alarm code
  1447. //===============================
  1448. if(Query_AC_MCU_Alarm(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].primaryMcuAlarm))
  1449. {
  1450. DEBUG_INFO("MCU-%d get alarm OK...\r\n");
  1451. /*
  1452. // Byte[6]
  1453. // Byte[7]
  1454. // Byte[8] Alarm code 3~7 bits Reserved
  1455. // Byte[9] Alarm code 0~7 bits Reserved
  1456. */
  1457. //================================================
  1458. // Byte[6]
  1459. //================================================
  1460. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OVP == 0x01)
  1461. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<0;
  1462. else
  1463. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<0);
  1464. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.UVP == 0x01)
  1465. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<1;
  1466. else
  1467. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<1);
  1468. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP == 0x01)
  1469. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<2;
  1470. else
  1471. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<2);
  1472. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OTP == 0x01)
  1473. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<3;
  1474. else
  1475. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<3);
  1476. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.gmi_fault == 0x01)
  1477. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<4;
  1478. else
  1479. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<4);
  1480. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.cp_fault == 0x01)
  1481. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<5;
  1482. else
  1483. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<5);
  1484. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.ac_leak == 0x01)
  1485. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<6;
  1486. else
  1487. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<6);
  1488. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.dc_leak == 0x01)
  1489. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<7;
  1490. else
  1491. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<7);
  1492. //================================================
  1493. // Byte[7]
  1494. //================================================
  1495. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.mcu_selftest_fail == 0x01)
  1496. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<8;
  1497. else
  1498. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<8);
  1499. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.handshaking_timeout == 0x01)
  1500. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<9;
  1501. else
  1502. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<9);
  1503. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.emergency_stop == 0x01)
  1504. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<10;
  1505. else
  1506. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<10);
  1507. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.relay_welding == 0x01)
  1508. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<11;
  1509. else
  1510. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<11);
  1511. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.leak_module_fail == 0x01)
  1512. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<12;
  1513. else
  1514. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<12);
  1515. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.shutter_fault == 0x01)
  1516. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<13;
  1517. else
  1518. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<13);
  1519. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.locker_fault == 0x01)
  1520. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<14;
  1521. else
  1522. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<14);
  1523. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.power_drop == 0x01)
  1524. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<15;
  1525. else
  1526. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<15);
  1527. //================================================
  1528. // Byte[8]
  1529. //================================================
  1530. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.circuit_short == 0x01)
  1531. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<16;
  1532. else
  1533. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<16);
  1534. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.set_circuit == 0x01)
  1535. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<17;
  1536. else
  1537. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<17);
  1538. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.relay_drive_fault == 0x01)
  1539. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode |= 1<<18;
  1540. else
  1541. ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode &= ~(1<<18);
  1542. //================================================
  1543. // Byte[9]
  1544. //================================================
  1545. // Debug print out
  1546. DEBUG_INFO("MCU-%d get OVP : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OVP);
  1547. DEBUG_INFO("MCU-%d get UVP : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.UVP);
  1548. DEBUG_INFO("MCU-%d get OCP : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP);
  1549. DEBUG_INFO("MCU-%d get OTP : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OTP);
  1550. DEBUG_INFO("MCU-%d get gmi_fault : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.gmi_fault);
  1551. DEBUG_INFO("MCU-%d get cp_fault : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.cp_fault);
  1552. DEBUG_INFO("MCU-%d get ac_leak : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.ac_leak);
  1553. DEBUG_INFO("MCU-%d get dc_leak : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.dc_leak);
  1554. DEBUG_INFO("MCU-%d get mcu_selftest_fail : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.mcu_selftest_fail);
  1555. DEBUG_INFO("MCU-%d get handshaking_timeout : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.handshaking_timeout);
  1556. DEBUG_INFO("MCU-%d get emergency_stop : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.emergency_stop);
  1557. DEBUG_INFO("MCU-%d get relay_welding : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.relay_welding);
  1558. DEBUG_INFO("MCU-%d get leak_module_fail : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.leak_module_fail);
  1559. DEBUG_INFO("MCU-%d get shutter_fault : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.shutter_fault);
  1560. DEBUG_INFO("MCU-%d get locker_fault : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.locker_fault);
  1561. DEBUG_INFO("MCU-%d get power_drop : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.power_drop);
  1562. DEBUG_INFO("MCU-%d get circuit_short : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.circuit_short);
  1563. DEBUG_INFO("MCU-%d get set_circuit : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.set_circuit);
  1564. DEBUG_INFO("MCU-%d get relay_drive_fault : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.relay_drive_fault);
  1565. DEBUG_INFO("MCU-%d get InputAlarmCode : %x\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode);
  1566. failCount[gun_index] = 0;
  1567. }
  1568. else
  1569. {
  1570. DEBUG_WARN("MCU-%d get alarm fail...%d\r\n", gun_index, failCount[gun_index]);
  1571. if(failCount[gun_index]<1000)
  1572. {
  1573. failCount[gun_index]++;
  1574. }
  1575. }
  1576. //===============================
  1577. // Query primary MCU BLE config
  1578. //===============================
  1579. if(Query_Ble_Config(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].bleConfigData))
  1580. {
  1581. DEBUG_INFO("MCU-%d get isUserLogin : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].bleConfigData.isLogin);
  1582. DEBUG_INFO("MCU-%d get isRequestStartCharger : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart);
  1583. DEBUG_INFO("MCU-%d get isRequestStopCharger : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop);
  1584. ShmSysConfigAndInfo->SysConfig.Bluetooth.isLogin = ShmCharger->gun_info[gun_index].bleConfigData.isLogin;
  1585. ShmSysConfigAndInfo->SysConfig.Bluetooth.isRequestStart = ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart;
  1586. ShmSysConfigAndInfo->SysConfig.Bluetooth.isRequestStop = ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop;
  1587. failCount[gun_index] = 0;
  1588. }
  1589. else
  1590. {
  1591. DEBUG_WARN("MCU-%d get ble config fail...%d\r\n", gun_index, failCount[gun_index]);
  1592. if(failCount[gun_index]<1000)
  1593. {
  1594. failCount[gun_index]++;
  1595. }
  1596. }
  1597. //===============================
  1598. // Query primary MCU ble login id
  1599. //===============================
  1600. if(Query_Ble_Central_ID(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].bleLoginCentralId))
  1601. {
  1602. DEBUG_INFO("MCU-%d get ble central id : %s\r\n", gun_index, ShmCharger->gun_info[gun_index].bleLoginCentralId.id);
  1603. memcpy(ShmSysConfigAndInfo->SysConfig.Bluetooth.LoginCentralID, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, sizeof ShmSysConfigAndInfo->SysConfig.Bluetooth.LoginCentralID);
  1604. failCount[gun_index] = 0;
  1605. }
  1606. else
  1607. {
  1608. DEBUG_WARN("MCU-%d get ble login central id fail...%d\r\n", gun_index, failCount[gun_index]);
  1609. if(failCount[gun_index]<1000)
  1610. {
  1611. failCount[gun_index]++;
  1612. }
  1613. }
  1614. //===============================
  1615. // Config primary MCU LED
  1616. //===============================
  1617. ShmCharger->gun_info[gun_index].primaryMcuLed.alarm_code = ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode;
  1618. if(Config_AC_MCU_LED(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].primaryMcuLed))
  1619. {
  1620. DEBUG_INFO("MCU-%d set Led mode : %d\r\n",gun_index, ShmCharger->gun_info[gun_index].primaryMcuLed.mode);
  1621. DEBUG_INFO("MCU-%d set Alarm code : %x\r\n",gun_index, ShmCharger->gun_info[gun_index].primaryMcuLed.alarm_code);
  1622. failCount[gun_index] = 0;
  1623. }
  1624. else
  1625. {
  1626. DEBUG_WARN("MCU-%d set led fail...%d\r\n", gun_index, failCount[gun_index]);
  1627. if(failCount[gun_index]<1000)
  1628. {
  1629. failCount[gun_index]++;
  1630. }
  1631. }
  1632. //===============================
  1633. // Query RTC
  1634. //===============================
  1635. if(Query_RTC(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].rtc))
  1636. {
  1637. struct timeb csuTime, mcuTime;
  1638. struct tm *tmCSU;
  1639. struct tm tmMcu;
  1640. ftime(&csuTime);
  1641. tmCSU = localtime(&csuTime.time);
  1642. tmMcu.tm_year = ShmCharger->gun_info[gun_index].rtc.year-1900;
  1643. tmMcu.tm_mon = ShmCharger->gun_info[gun_index].rtc.month-1;
  1644. tmMcu.tm_mday = ShmCharger->gun_info[gun_index].rtc.day;
  1645. tmMcu.tm_hour = ShmCharger->gun_info[gun_index].rtc.hour;
  1646. tmMcu.tm_min = ShmCharger->gun_info[gun_index].rtc.min;
  1647. tmMcu.tm_sec = ShmCharger->gun_info[gun_index].rtc.sec;
  1648. mcuTime.time = mktime(&tmMcu);
  1649. if(ShmCharger->gun_info[gun_index].bleConfigData.isLogin && !ShmOCPP16Data->OcppConnStatus)
  1650. {
  1651. if(abs(DiffTimeb(csuTime, mcuTime)) > 10000)
  1652. {
  1653. char cmdBuf[128];
  1654. sprintf(cmdBuf, "date -u -s \"%04d-%02d-%02d %02d:%02d:%02d\"", ShmCharger->gun_info[gun_index].rtc.year,
  1655. ShmCharger->gun_info[gun_index].rtc.month,
  1656. ShmCharger->gun_info[gun_index].rtc.day,
  1657. ShmCharger->gun_info[gun_index].rtc.hour,
  1658. ShmCharger->gun_info[gun_index].rtc.min,
  1659. ShmCharger->gun_info[gun_index].rtc.sec);
  1660. system(cmdBuf);
  1661. system("hwclock -w -u");
  1662. system("hwclock -s");
  1663. DEBUG_INFO("Sync from MCU-%d rtc OK...%04d-%02d-%02d %02d:%02d:%02d\r\n", gun_index,
  1664. ShmCharger->gun_info[gun_index].rtc.year,
  1665. ShmCharger->gun_info[gun_index].rtc.month,
  1666. ShmCharger->gun_info[gun_index].rtc.day,
  1667. ShmCharger->gun_info[gun_index].rtc.hour,
  1668. ShmCharger->gun_info[gun_index].rtc.min,
  1669. ShmCharger->gun_info[gun_index].rtc.sec);
  1670. }
  1671. }
  1672. else
  1673. {
  1674. if(abs(DiffTimeb(csuTime, mcuTime)) > 10000)
  1675. {
  1676. ShmCharger->gun_info[gun_index].rtc.year = tmCSU->tm_year+1900;
  1677. ShmCharger->gun_info[gun_index].rtc.month = tmCSU->tm_mon+1;
  1678. ShmCharger->gun_info[gun_index].rtc.day = tmCSU->tm_mday;
  1679. ShmCharger->gun_info[gun_index].rtc.hour = tmCSU->tm_hour;
  1680. ShmCharger->gun_info[gun_index].rtc.min = tmCSU->tm_hour;
  1681. ShmCharger->gun_info[gun_index].rtc.sec = tmCSU->tm_sec;
  1682. Config_RTC(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].rtc);
  1683. DEBUG_INFO("MCU-%d set rtc OK...%04d-%02d-%02d %02d:%02d:%02d\r\n", gun_index,
  1684. ShmCharger->gun_info[gun_index].rtc.year,
  1685. ShmCharger->gun_info[gun_index].rtc.month,
  1686. ShmCharger->gun_info[gun_index].rtc.day,
  1687. ShmCharger->gun_info[gun_index].rtc.hour,
  1688. ShmCharger->gun_info[gun_index].rtc.min,
  1689. ShmCharger->gun_info[gun_index].rtc.sec);
  1690. }
  1691. }
  1692. failCount[gun_index] = 0;
  1693. }
  1694. else
  1695. {
  1696. DEBUG_WARN("MCU-%d get rtc fail...%d\r\n", gun_index, failCount[gun_index]);
  1697. if(failCount[gun_index]<1000)
  1698. failCount[gun_index]++;
  1699. }
  1700. //===============================
  1701. // Query primary MCU power consumption
  1702. //===============================
  1703. if(Query_Power_Consumption(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), &ShmCharger->gun_info[gun_index].powerConsumption))
  1704. {
  1705. DEBUG_INFO("MCU-%d get power consumption : %f\r\n",gun_index, ((float)ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0));
  1706. failCount[gun_index] = 0;
  1707. }
  1708. else
  1709. {
  1710. DEBUG_WARN("MCU-%d get power consumption fail...%d\r\n", gun_index, failCount[gun_index]);
  1711. if(failCount[gun_index]<1000)
  1712. failCount[gun_index]++;
  1713. }
  1714. //===============================
  1715. // Config primary Legacy request
  1716. //===============================
  1717. if(Config_AC_MCU_LEGACY_REQUEST(Uart1Fd, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1),&ShmCharger->gun_info[gun_index].legacyRequest))
  1718. {
  1719. DEBUG_INFO("MCU-%d set relay request : %d\r\n", gun_index, ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest);
  1720. DEBUG_INFO("========================================\n");
  1721. failCount[gun_index] = 0;
  1722. }
  1723. else
  1724. {
  1725. DEBUG_WARN("MCU-%d set relay request fail...%d\r\n", gun_index, failCount[gun_index]);
  1726. if(failCount[gun_index]<1000)
  1727. failCount[gun_index]++;
  1728. }
  1729. //===============================
  1730. // Upgrade MCU
  1731. //===============================
  1732. if(ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq)
  1733. {
  1734. unsigned char cmd[512];
  1735. if(Upgrade_UART(Uart1Fd, AC_WALLMOUNT_CONTROLLER, (gun_index>0?ADDR_AC_PRIMARY_2:ADDR_AC_PRIMARY_1), ShmCharger->fwUpgradeInfo.location, ShmCharger->fwUpgradeInfo.modelName))
  1736. {
  1737. DEBUG_INFO("MCU-%d upgrade firmware OK...%s\r\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
  1738. sleep(20);
  1739. ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq = OFF;
  1740. failCount[gun_index] = 0;
  1741. }
  1742. else
  1743. {
  1744. DEBUG_WARN("MCU-%d upgrade firmware fail...%d\r\n", gun_index, failCount[gun_index]);
  1745. if(failCount[gun_index]<1000)
  1746. failCount[gun_index]++;
  1747. }
  1748. sprintf((char*)cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  1749. system((char*)cmd);
  1750. }
  1751. //===============================
  1752. // Config primary set CP PWN duty
  1753. //===============================
  1754. // ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = 32;
  1755. // 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))
  1756. // {
  1757. // DEBUG_INFO("MCU-%d cp pwn duty : %d\r\n",gun_index, &ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
  1758. // }
  1759. // else
  1760. // {
  1761. // DEBUG_WARN("MCU-%d set cp pwn duty fail...%d\r\n", gun_index, failCount[gun_index]);
  1762. // if(failCount[gun_index]<1000)
  1763. // {
  1764. // failCount[gun_index]++;
  1765. // }
  1766. // }
  1767. //
  1768. // if(failCount[gun_index]>=1000)
  1769. // {
  1770. // DEBUG_ERROR("Primary MCU-%d communication fault", gun_index);
  1771. // }
  1772. //===============================
  1773. // Communication fail check
  1774. //===============================
  1775. if(failCount[gun_index] >= FAIL_SPEC_COMM)
  1776. {
  1777. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.comm_timeout == OFF)
  1778. {
  1779. DEBUG_ERROR("Primary MCU-%d communication fault", gun_index);
  1780. ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.comm_timeout = ON;
  1781. }
  1782. }
  1783. else
  1784. {
  1785. if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.comm_timeout == ON)
  1786. {
  1787. DEBUG_ERROR("Primary MCU-%d communication recover", gun_index);
  1788. ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.comm_timeout = OFF;
  1789. }
  1790. }
  1791. }
  1792. usleep(100000);
  1793. }
  1794. return FAIL;
  1795. }