ReadCmdline.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. /*
  2. * Main.c
  3. *
  4. * Created on: 2019年8月6日
  5. * Author: 7564
  6. */
  7. #include <sys/time.h>
  8. #include <sys/timeb.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/types.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/socket.h>
  14. #include <sys/ipc.h>
  15. #include <sys/shm.h>
  16. #include <sys/shm.h>
  17. #include <sys/mman.h>
  18. #include <linux/wireless.h>
  19. #include <arpa/inet.h>
  20. #include <netinet/in.h>
  21. #include <unistd.h>
  22. #include <stdarg.h>
  23. #include <stdio.h> /*標準輸入輸出定義*/
  24. #include <stdlib.h> /*標準函數庫定義*/
  25. #include <unistd.h> /*Unix 標準函數定義*/
  26. #include <fcntl.h> /*檔控制定義*/
  27. #include <termios.h> /*PPSIX 終端控制定義*/
  28. #include <errno.h> /*錯誤號定義*/
  29. #include <errno.h>
  30. #include <string.h>
  31. #include <time.h>
  32. #include <ctype.h>
  33. #include <ifaddrs.h>
  34. #include <math.h>
  35. #include <stdbool.h>
  36. #include "../../define.h"
  37. typedef unsigned char byte;
  38. #define PASS 1
  39. #define FAIL -1
  40. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  41. #define TTY_PATH "/dev/tty"
  42. #define STTY_US "stty raw -echo -F "
  43. #define STTY_DEF "stty -raw echo -F "
  44. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  45. struct StatusCodeData *ShmStatusCodeData;
  46. struct PrimaryMcuData *ShmPrimaryMcuData;
  47. struct CHAdeMOData *ShmCHAdeMOData;
  48. struct CcsData *ShmCcsData;
  49. struct FanModuleData *ShmFanModuleData;
  50. struct RelayModuleData *ShmRelayModuleData;
  51. struct PsuData *ShmPsuData;
  52. struct ChargingInfoData *_chargingData[CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY];
  53. int gunCount = CHAdeMO_QUANTITY + CCS_QUANTITY + GB_QUANTITY;
  54. char *msg = "state : get gun state (index) \n"
  55. "card : scanning card (x) : \n"
  56. "gun : get gun plugit state (index) \n"
  57. "lock : get gun locked state (index) \n"
  58. "self : self test state (x) \n"
  59. "ver : ver of board (407 or index or rb or fan) \n"
  60. "ac : get ac relay state (x) \n";
  61. bool FindChargingInfoData(byte target, struct ChargingInfoData **chargingData)
  62. {
  63. for (byte index = 0; index < CHAdeMO_QUANTITY; index++)
  64. {
  65. if (ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index].Index == target)
  66. {
  67. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.ChademoChargingData[index];
  68. return true;
  69. }
  70. }
  71. for (byte index = 0; index < CCS_QUANTITY; index++)
  72. {
  73. if (ShmSysConfigAndInfo->SysInfo.CcsChargingData[index].Index == target)
  74. {
  75. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[index];
  76. return true;
  77. }
  78. }
  79. for (byte index = 0; index < GB_QUANTITY; index++)
  80. {
  81. if (ShmSysConfigAndInfo->SysInfo.GbChargingData[index].Index == target)
  82. {
  83. chargingData[target] = &ShmSysConfigAndInfo->SysInfo.GbChargingData[index];
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. int InitShareMemory()
  90. {
  91. int result = PASS;
  92. int MeterSMId;
  93. //initial ShmSysConfigAndInfo
  94. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  95. {
  96. result = FAIL;
  97. }
  98. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  99. {
  100. result = FAIL;
  101. }
  102. else
  103. {}
  104. //initial ShmStatusCodeData
  105. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  106. {
  107. result = FAIL;
  108. }
  109. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  110. {
  111. result = FAIL;
  112. }
  113. else
  114. {}
  115. if(CHAdeMO_QUANTITY > 0)
  116. {
  117. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData),
  118. IPC_CREAT | 0777)) < 0) {
  119. result = FAIL;
  120. } else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0))
  121. == (void *) -1) {
  122. result = FAIL;
  123. } else {
  124. }
  125. }
  126. if(CCS_QUANTITY > 0)
  127. {
  128. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData),
  129. IPC_CREAT | 0777)) < 0) {
  130. result = FAIL;
  131. } else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1) {
  132. result = FAIL;
  133. } else {
  134. }
  135. }
  136. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  137. {
  138. result = FAIL;
  139. }
  140. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  141. {
  142. result = FAIL;
  143. }
  144. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  145. {
  146. result = FAIL;
  147. }
  148. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  149. {
  150. result = FAIL;
  151. }
  152. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  153. {
  154. result = FAIL;
  155. }
  156. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  157. {
  158. result = FAIL;
  159. }
  160. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  161. {
  162. result = FAIL;
  163. }
  164. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  165. {
  166. result = FAIL;
  167. }
  168. return result;
  169. }
  170. void RunStatusProc(char *v1, char *v2)
  171. {
  172. int _index = atoi(v1);
  173. if (!FindChargingInfoData(_index, &_chargingData[0]))
  174. {
  175. printf ("FindChargingInfoData error\n");
  176. return;
  177. }
  178. if (strcmp(v2, "-1") == 0 || strcmp(v2, "") == 0)
  179. {
  180. // get
  181. printf ("index = %x, status = %x (%d)\n", _index, _chargingData[_index]->SystemStatus, _chargingData[_index]->IsAvailable);
  182. }
  183. else
  184. {
  185. // set
  186. _chargingData[_index]->SystemStatus = atoi(v2);
  187. }
  188. }
  189. void RunCardProc(char *v1, char *v2)
  190. {
  191. if (ShmSysConfigAndInfo->SysInfo.WaitForPlugit)
  192. {
  193. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = 0x00;
  194. printf ("SysInfo.WaitForPlugit = %x \n", ShmSysConfigAndInfo->SysInfo.WaitForPlugit);
  195. }
  196. else
  197. {
  198. ShmSysConfigAndInfo->SysInfo.WaitForPlugit = 0x01;
  199. printf ("SysInfo.WaitForPlugit = %x \n", ShmSysConfigAndInfo->SysInfo.WaitForPlugit);
  200. }
  201. }
  202. void RunGunPlugitProc(char *v1, char *v2)
  203. {
  204. int _index = atoi(v1);
  205. if (!FindChargingInfoData(_index, &_chargingData[0]))
  206. {
  207. printf("FindChargingInfoData error\n");
  208. return;
  209. }
  210. if (strcmp(v2, "-1") == 0 || strcmp(v2, "") == 0)
  211. {
  212. // get
  213. printf("index = %x, plug it = %x\n", _index, _chargingData[_index]->ConnectorPlugIn);
  214. }
  215. else
  216. {
  217. // set
  218. _chargingData[_index]->ConnectorPlugIn = atoi(v2);
  219. }
  220. }
  221. void GetGunLockStatusProc(char *v1, char *v2)
  222. {
  223. if (strcmp(v1, "0") == 0)
  224. {
  225. printf("Gun Locked Status = %x \n", _chargingData[0]->GunLocked);
  226. }
  227. if (strcmp(v1, "1") == 0)
  228. {
  229. printf("Gun Locked Status = %x \n", _chargingData[1]->GunLocked);
  230. }
  231. }
  232. void SetSystemIDProc()
  233. {
  234. char *systemId = "Alston_Test";
  235. memcpy(&ShmSysConfigAndInfo->SysConfig.SystemId, systemId, strlen(systemId));
  236. }
  237. void RunSelfProc()
  238. {
  239. printf("self test status = %x\n", ShmSysConfigAndInfo->SysInfo.SelfTestSeq);
  240. }
  241. void GetFwVerProc(char *v1)
  242. {
  243. if (strcmp(v1, "407") == 0)
  244. {
  245. printf("407 FW Version = %s \n", ShmPrimaryMcuData->version);
  246. }
  247. else if (strcmp(v1, "0") == 0)
  248. {
  249. printf("Ev board 1 FW Version = %s \n", ShmCHAdeMOData->evse[0].version);
  250. printf("Ev board 1 FW Version = %s \n", ShmCcsData->V2GMessage_DIN70121->version);
  251. }
  252. else if (strcmp(v1, "1") == 0)
  253. {
  254. printf("Ev board 2 FW Version = %s \n", ShmCHAdeMOData->evse[1].version);
  255. printf("Ev board 2 FW Version = %s \n", ShmCcsData->V2GMessage_DIN70121->version);
  256. }
  257. else if (strcmp(v1, "rb") == 0)
  258. {
  259. printf("RB Version = %s \n", ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev);
  260. }
  261. else if (strcmp(v1, "fan") == 0)
  262. {
  263. printf("FAN Version = %s \n", ShmSysConfigAndInfo->SysInfo.FanModuleFwRev);
  264. }
  265. else if (strcmp(v1, "dc") == 0)
  266. {
  267. printf("DC Main Version = %s \n", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  268. }
  269. }
  270. void FwUpdateFlagProc()
  271. {
  272. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = 0x01;
  273. }
  274. void CheckAcStatus(char *v1)
  275. {
  276. if (strcmp(v1, "-1") == 0|| strcmp(v1, "") == 0)
  277. {
  278. printf("AC Status = %d \n", ShmSysConfigAndInfo->SysInfo.AcContactorStatus);
  279. }
  280. }
  281. void SetCableChkStatus(char *v1, char *v2)
  282. {
  283. int _index = atoi(v1);
  284. if (!FindChargingInfoData(_index, &_chargingData[0]))
  285. {
  286. printf ("FindChargingInfoData error\n");
  287. return;
  288. }
  289. _chargingData[_index]->GroundFaultStatus = atoi(v2);
  290. }
  291. void SetPowerValue(char *v1, char *v2)
  292. {
  293. // int _index = atoi(v1);
  294. // if (!FindChargingInfoData(_index, &_chargingData[0])) {
  295. // printf("FindChargingInfoData error\n");
  296. // return;
  297. // }
  298. //
  299. // if (strcmp(v2, "-1") == 0 || strcmp(v2, "") == 0) {
  300. // // get
  301. // printf("index = %x, max cur = %f\n", _index,
  302. // _chargingData[_index]->MaxChargingCurrentForTest);
  303. // } else {
  304. // // set
  305. // _chargingData[_index]->MaxChargingCurrentForTest = atoi(v2);
  306. // }
  307. }
  308. void GetGunSelectedNum(char *v1)
  309. {
  310. if (strcmp(v1, "-1") == 0 || strcmp(v1, "") == 0)
  311. printf("connector selected = %d \n", ShmSysConfigAndInfo->SysInfo.CurGunSelected);
  312. else
  313. {
  314. int _index = atoi(v1);
  315. ShmSysConfigAndInfo->SysInfo.CurGunSelected = _index;
  316. printf("connector select changed = %d \n", _index);
  317. }
  318. }
  319. void SetFanSpeed(char *v1)
  320. {
  321. int speed = atoi(v1);
  322. ShmFanModuleData->TestFanSpeed = speed;
  323. }
  324. void GetPsuInformation(char *v1)
  325. {
  326. printf("*************************************************\n");
  327. if(strcmp(v1, "count") == 0)
  328. {
  329. for (int i = 0; i < 4; i++)
  330. {
  331. printf("Group Index = %d, Module Count = %d \n", i, ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity);
  332. }
  333. }
  334. else if(strcmp(v1, "ver") == 0)
  335. {
  336. for (int i = 0; i < ShmPsuData->GroupCount; i++)
  337. {
  338. for (int j = 0; j < ShmPsuData->PsuGroup[i].GroupPresentPsuQuantity; j++)
  339. {
  340. printf("Group Index = %d, Psu Index = %d, Version = %s \n",
  341. i, j, ShmPsuData->PsuGroup[i].PsuModule[j].FwVersion);
  342. }
  343. }
  344. }
  345. else if(strcmp(v1, "cap") == 0)
  346. {
  347. for (int i = 0; i < ShmPsuData->GroupCount; i++)
  348. {
  349. printf("Group Index = %d, MaxCur = %d, Power = %d \n",
  350. i, ShmPsuData->PsuGroup[i].GroupAvailableCurrent, ShmPsuData->PsuGroup[i].GroupAvailablePower);
  351. }
  352. }
  353. else if (strcmp(v1, "output") == 0)
  354. {
  355. for (int i = 0; i < ShmPsuData->GroupCount; i++)
  356. {
  357. printf("Group Index = %d, OutputV = %d, OutputC = %d \n",
  358. i, ShmPsuData->PsuGroup[i].GroupPresentOutputVoltage, ShmPsuData->PsuGroup[i].GroupPresentOutputCurrent);
  359. }
  360. }
  361. printf("*************************************************\n");
  362. }
  363. static int get_char()
  364. {
  365. fd_set rfds;
  366. struct timeval tv;
  367. int ch = 0;
  368. FD_ZERO(&rfds);
  369. FD_SET(0, &rfds);
  370. tv.tv_sec = 0;
  371. tv.tv_usec = 10; //wait input timout time
  372. //if input
  373. if (select(1, &rfds, NULL, NULL, &tv) > 0)
  374. {
  375. ch = getchar();
  376. }
  377. return ch;
  378. }
  379. void RunUnconditionalChargeIndex1(char *v1, char *v2, char *v3)
  380. {
  381. int _GunIndex = atoi(v1);
  382. int _Voltage = atoi(v2);
  383. int _Current = atoi(v3);
  384. unsigned char PreviousSystemStatus = 0xff;
  385. int ch = 0;
  386. if (!FindChargingInfoData(_GunIndex, &_chargingData[0]))
  387. {
  388. printf ("FindChargingInfoData error\n");
  389. return;
  390. }
  391. printf ("ReqVoltage = %d, ReqCurrent = %d\n", _Voltage,_Current);
  392. if(_Voltage > 1000 || _Voltage < 150){
  393. printf ("Input Voltage over range\n");
  394. return;
  395. }
  396. // if(_Current > 100 || _Current < 2){
  397. //
  398. // printf ("Input Current over range\n");
  399. // return;
  400. // }
  401. //測試期間先跳過自我測試 _STEST_COMPLETE = 0xfe
  402. //ShmSysConfigAndInfo->SysInfo.SelfTestSeq = 0xfe;
  403. //kill ev task
  404. system("killall Module_EvComm");
  405. _Voltage = (_Voltage * 10);
  406. _Current = (_Current * 10);
  407. //system(STTY_US TTY_PATH);
  408. while(true)
  409. {
  410. //fix gun 1
  411. ShmSysConfigAndInfo->SysInfo.CurGunSelected = _GunIndex;
  412. switch(_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  413. {
  414. case S_IDLE:
  415. {
  416. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  417. {
  418. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  419. printf ("[UnconditionalCharge - S_IDLE]\n");
  420. }
  421. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_PREPARNING;
  422. }
  423. break;
  424. case S_PREPARNING:
  425. {
  426. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  427. {
  428. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  429. printf ("[UnconditionalCharge - S_PREPARNIN]\n");
  430. //等待 AC Relay 搭上且找到模組 (main 在此 statue 其它 task 會去做完)
  431. printf ("wait find module\n");
  432. }
  433. //main 會在此階段判斷以下資料跳到下一個 state
  434. //用來得知 AC 是否有搭上 (搭上模組的資訊才會出來) 因為每次 AC_Contactor
  435. //ShmPsuData->SystemPresentPsuQuantity;
  436. //ShmPsuData->PsuGroup[gun_index].GroupPresentPsuQuantity;
  437. //ShmPsuData->PsuGroup[gun_index].GroupAvailablePower;
  438. //_chargingData[gun_index]->AvailableChargingPower;
  439. //等待 AC Relay 搭上且找到模組 (main 在此 statue 其它 task 會去做完)
  440. //sleep(10);
  441. //清除 main timeout 機制
  442. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->TimeoutFlag = 0;
  443. //不論是什麼 type 的槍都固意設成 Chademo 不跑 Prechage step
  444. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->Type = 9;
  445. }
  446. break;
  447. case S_PREPARING_FOR_EV:
  448. {
  449. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  450. {
  451. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  452. printf ("[UnconditionalCharge - S_PREPARING_FOR_EV]\n");
  453. printf ("ReqVoltage = %d, ReqCurrent = %d\n", _Voltage,_Current);
  454. }
  455. //清除 main timeout 機制
  456. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->TimeoutFlag = 0;
  457. //不論是什麼 type 的槍都固意設成 Chademo 不跑 Prechage step
  458. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->Type = 9;
  459. //充電電壓電流
  460. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterySoc = 50;
  461. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetVoltage = 5000;
  462. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetCurrent = 20;
  463. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->AvailableChargingCurrent = 1000;
  464. //****** 注意~此行為是防止 K1K2 先開導到無法升壓 ( Relay Board 在此 state 還未搭上 K1K2 )
  465. //確定模組己升壓完成
  466. //if(_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage <= (3000+500) &&
  467. // _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage >= (3000-500) )
  468. {
  469. printf ("Precharge Done = %f \n", _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage);
  470. //EV done
  471. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_PREPARING_FOR_EVSE;
  472. }
  473. }
  474. break;
  475. case S_PREPARING_FOR_EVSE:
  476. {
  477. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  478. {
  479. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  480. printf ("[UnconditionalCharge - S_PREPARING_FOR_EVSE]\n");
  481. }
  482. //printf ("tar vol = %d \n", _Voltage);
  483. //printf ("tar cur = %d \n", _Current);
  484. //清除 main timeout 機制
  485. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->TimeoutFlag = 0;
  486. //不論是什麼 type 的槍都固意設成 Chademo 不跑 Prechage step
  487. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->Type = 9;
  488. //充電電壓電流
  489. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterySoc = 50;
  490. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetVoltage = 5000;
  491. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetCurrent = 20;
  492. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->AvailableChargingCurrent = 1000;
  493. //printf ("tar vol_ = %d \n", _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetVoltage);
  494. // printf ("tar cur_ = %d \n", _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetCurrent);
  495. //****** 注意~此行為是防止 K1K2 先開導到無法升壓 ( Relay Board 在此 state 還未搭上 K1K2 )
  496. //確定模組己升壓完成
  497. if(_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus == 0x01 ||
  498. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus == 0x03)
  499. {
  500. printf ("First Ground Fault State (%d)\n",_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus);
  501. printf ("Wait K1K2 = %f \n", _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage);
  502. sleep(5);
  503. //EV done
  504. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_CHARGING;
  505. }
  506. else if (_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus > 0x02)
  507. {
  508. printf ("First Ground Fault check Fail (%d)\n",_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus);
  509. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_TERMINATING;
  510. }
  511. }
  512. break;
  513. case S_CHARGING:
  514. {
  515. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  516. {
  517. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  518. printf ("[UnconditionalCharge - S_CHARGING]\n");
  519. }
  520. //充電電壓電流
  521. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterySoc = 50;
  522. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetVoltage = _Voltage;
  523. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->EvBatterytargetCurrent = _Current;
  524. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->AvailableChargingCurrent = 1000;
  525. //ev task do this
  526. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingPower = ((float)((_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingVoltage / 10) * (_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->PresentChargingCurrent / 10)) / 1000);
  527. if (_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus == 0x02){
  528. printf ("Charging Ground Fault check Fail (%d)\n",_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus);
  529. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_TERMINATING;
  530. }
  531. if (_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus == 0x03){
  532. printf ("Charging Ground Fault Warning (%d)\n",_chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->GroundFaultStatus);
  533. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_TERMINATING;
  534. }
  535. }
  536. break;
  537. case S_TERMINATING:
  538. {
  539. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  540. {
  541. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  542. system("/root/Module_EvComm &");
  543. printf ("[UnconditionalCharge - S_TERMINATING]\n");
  544. //無阻塞偵測 keybaord 結束
  545. system(STTY_DEF TTY_PATH);
  546. }
  547. sleep(3);
  548. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_COMPLETE;
  549. return;
  550. }
  551. break;
  552. case S_COMPLETE:
  553. {
  554. if(PreviousSystemStatus != _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus)
  555. {
  556. PreviousSystemStatus = _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus;
  557. printf ("[UnconditionalCharge - S_COMPLETE]\n");
  558. }
  559. sleep(3);
  560. return;
  561. }
  562. break;
  563. }
  564. /*
  565. //使用 Keyboard input 阻塞方法
  566. fgets(InputChar, sizeof(InputChar), stdin);
  567. if (InputChar[0] == 's' && InputChar[1] == 't' && InputChar[2] == 'o' && InputChar[3] == 'p')
  568. {
  569. printf ("Precharge Done\n");
  570. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_TERMINATING;
  571. }
  572. */
  573. //使用 keybaord input 非阻塞方法
  574. ch = get_char();
  575. if (ch)
  576. {
  577. printf("%c \n\r", ch);
  578. switch (ch)
  579. {
  580. //Ctrl + C
  581. case 3:
  582. //system(STTY_DEF TTY_PATH);
  583. return;
  584. //input c or C
  585. case 'c':
  586. case 'C':
  587. printf("stop \n\r");
  588. //system(STTY_DEF TTY_PATH);
  589. _chargingData[ShmSysConfigAndInfo->SysInfo.CurGunSelected]->SystemStatus = S_TERMINATING;
  590. break;
  591. }
  592. }
  593. }
  594. }
  595. int main(void)
  596. {
  597. if(InitShareMemory() == FAIL)
  598. {
  599. printf ("InitShareMemory = FAIL \n");
  600. if(ShmStatusCodeData != NULL)
  601. {
  602. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  603. }
  604. sleep(5);
  605. return 0;
  606. }
  607. for(;;)
  608. {
  609. char word[128];
  610. char newString[7][10];
  611. int i,j,ctr;
  612. fgets(word, sizeof(word), stdin);
  613. j=0; ctr=0;
  614. strcpy(newString[1], "-1");
  615. strcpy(newString[2], "-1");
  616. for (i = 0; i <= (strlen(word)); i++)
  617. {
  618. if (word[i] == ' ' || word[i] == '\0' || word[i] == 10)
  619. {
  620. newString[ctr][j] = '\0';
  621. ctr++;
  622. j = 0;
  623. }
  624. else
  625. {
  626. newString[ctr][j] = word[i];
  627. j++;
  628. }
  629. }
  630. if(strcmp(newString[0], "state") == 0)
  631. {
  632. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  633. continue;
  634. // 槍狀態
  635. RunStatusProc(newString[1], newString[2]);
  636. }
  637. else if(strcmp(newString[0], "card") == 0)
  638. {
  639. // 刷卡狀態
  640. RunCardProc(newString[1], newString[2]);
  641. }
  642. else if(strcmp(newString[0], "gun") == 0)
  643. {
  644. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  645. continue;
  646. // 插槍狀態
  647. RunGunPlugitProc(newString[1], newString[2]);
  648. }
  649. else if(strcmp(newString[0], "lock") == 0)
  650. {
  651. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  652. continue;
  653. // 插槍狀態
  654. GetGunLockStatusProc(newString[1], newString[2]);
  655. }
  656. else if(strcmp(newString[0], "sysid") == 0)
  657. {
  658. // 測試 sys id
  659. SetSystemIDProc();
  660. }
  661. else if(strcmp(newString[0], "self") == 0)
  662. {
  663. // CSU 自我檢測狀態
  664. RunSelfProc(newString[1]);
  665. }
  666. else if(strcmp(newString[0], "ver") == 0)
  667. {
  668. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  669. continue;
  670. // 取 FW 版號
  671. GetFwVerProc(newString[1]);
  672. }
  673. else if (strcmp(newString[0], "update") == 0)
  674. {
  675. // 更新
  676. FwUpdateFlagProc(newString[1]);
  677. }
  678. else if (strcmp(newString[0], "ac") == 0)
  679. {
  680. // AC contactor 狀態
  681. CheckAcStatus(newString[1]);
  682. }
  683. else if (strcmp(newString[0], "cable") == 0)
  684. {
  685. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  686. continue;
  687. // cable check pass
  688. SetCableChkStatus(newString[1], newString[2]);
  689. }
  690. else if (strcmp(newString[0], "pow") == 0)
  691. {
  692. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  693. continue;
  694. // cable check pass
  695. SetPowerValue(newString[1], newString[2]);
  696. }
  697. else if(strcmp(newString[0], "select") == 0)
  698. {
  699. // 取得當前選的槍號
  700. GetGunSelectedNum(newString[1]);
  701. }
  702. else if(strcmp(newString[0], "fan") == 0)
  703. {
  704. // 設定風扇速度
  705. SetFanSpeed(newString[1]);
  706. }
  707. else if(strcmp(newString[0], "psu") == 0)
  708. {
  709. //如果連一個參數都沒有 (此命令不理會) 加上判斷第二參數
  710. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0)
  711. {
  712. printf ("PSU : Param fail..Please retry again......\n");
  713. continue;
  714. }
  715. // 取得 PSU 資訊
  716. GetPsuInformation(newString[1]);
  717. }
  718. else if(strcmp(newString[0], "strchg") == 0)
  719. {
  720. //如果連一個參數都沒有 (此命令不理會) 加上判斷第二參數
  721. if (strcmp(newString[1], "-1") == 0 || strcmp(newString[1], "") == 0 ||
  722. strcmp(newString[2], "-1") == 0 || strcmp(newString[2], "") == 0)
  723. {
  724. printf ("Input cmd fail ------ strchg [vol 150-1000] [cru 2-100]\n");
  725. continue;
  726. }
  727. // 槍狀態
  728. RunUnconditionalChargeIndex1(newString[1], newString[2], newString[3]);
  729. }
  730. printf ("%s\n", msg);
  731. usleep(100000);
  732. }
  733. return 0;
  734. }