ReadCmdline.c 24 KB

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