OutputTask.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * OutputTask.c
  3. *
  4. * Created on: 2020年2月25日
  5. * Author: 7564
  6. */
  7. #include "OutputTask.h"
  8. int Uart5Fd;
  9. int PresentRPM;
  10. char *relayRs485PortName = "/dev/ttyS5";
  11. int InitComPort5()
  12. {
  13. int fd;
  14. struct termios tios;
  15. fd = open(relayRs485PortName, O_RDWR);
  16. if(fd <= 0)
  17. {
  18. printf("InitComPort NG\n");
  19. sleep(5);
  20. return -1;
  21. }
  22. ioctl (fd, TCGETS, &tios);
  23. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  24. tios.c_lflag = 0;
  25. tios.c_iflag = 0;
  26. tios.c_oflag = 0;
  27. tios.c_cc[VMIN]=0;
  28. tios.c_cc[VTIME]=(byte)0; // timeout 0.5 second
  29. tios.c_lflag=0;
  30. tcflush(fd, TCIFLUSH);
  31. ioctl (fd, TCSETS, &tios);
  32. return fd;
  33. }
  34. unsigned char Set_Fan_Speed(unsigned char fd, int rpm)
  35. {
  36. unsigned char result = FAIL;
  37. PresentRPM = rpm;
  38. if(rpm >= 14000)
  39. {
  40. rpm = 14000;
  41. }
  42. if(rpm < 0)
  43. {
  44. rpm = 0;
  45. }
  46. unsigned char tx[15] = {0xaa, 0x00, 0x02, 0x81, 0x08, 0x00, (rpm & 0xFF), ((rpm >> 8) & 0xFF), (rpm & 0xFF), ((rpm >> 8) & 0xFF), (rpm & 0xFF), ((rpm >> 8) & 0xFF), (rpm & 0xFF), ((rpm >> 8) & 0xFF), 0x00};
  47. unsigned char rx[512];
  48. unsigned char chksum = 0x00;
  49. for(int idx = 0;idx<(tx[4] | tx[5]<<8);idx++)
  50. chksum ^= tx[6+idx];
  51. tx[14] = chksum;
  52. unsigned char len = tranceive(fd, tx, sizeof(tx), rx);
  53. if(len > 6)
  54. {
  55. if (len < 6+(rx[4] | rx[5]<<8))
  56. return result;
  57. chksum = 0x00;
  58. for(int idx = 0;idx<(rx[4] | rx[5]<<8);idx++)
  59. {
  60. chksum ^= rx[6+idx];
  61. }
  62. if((chksum == rx[6+(rx[4] | rx[5]<<8)]) &&
  63. (rx[2] == tx[1]) &&
  64. (rx[1] == tx[2]) &&
  65. (rx[3] == tx[3]) &&
  66. rx[6] == PASS)
  67. {
  68. result = PASS;
  69. }
  70. }
  71. return result;
  72. }
  73. bool isOpen;
  74. int InitComPort()
  75. {
  76. int fd;
  77. struct termios tios;
  78. fd = open(priPortName, O_RDWR);
  79. if(fd<=0)
  80. {
  81. #ifdef SystemLogMessage
  82. DEBUG_ERROR("open 407 Communication port NG \n");
  83. #endif
  84. return -1;
  85. }
  86. ioctl (fd, TCGETS, &tios);
  87. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  88. tios.c_lflag = 0;
  89. tios.c_iflag = 0;
  90. tios.c_oflag = 0;
  91. tios.c_cc[VMIN]=0;
  92. tios.c_cc[VTIME]=(unsigned char)1;
  93. tios.c_lflag=0;
  94. tcflush(fd, TCIFLUSH);
  95. ioctl (fd, TCSETS, &tios);
  96. return fd;
  97. }
  98. unsigned long GetTimeoutValue(struct timeval _sour_time)
  99. {
  100. struct timeval _end_time;
  101. gettimeofday(&_end_time, NULL);
  102. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  103. }
  104. void ShowMainMsg()
  105. {
  106. printf("Max Vol : %f, Max Cur : %d, POW : %d \n", UnSafeDataInfo->PSU_VOLTAGE,
  107. UnSafeDataInfo->PSU_CURRENT, UnSafeDataInfo->PSU_POWER);
  108. printf("=> ");
  109. }
  110. void ChkButtonStatus()
  111. {
  112. if (Button1 == PRESS && !leftBtnPush)
  113. {
  114. if(!leftBtnPush)
  115. {
  116. leftBtnPush = true;
  117. if (_charging_mode == CHARGING_MODE_STOP)
  118. {
  119. _charging_mode = CHARGING_MODE_START;
  120. printf("****************** Switch to Charging Mode ******************\n");
  121. }
  122. }
  123. else if (Button1 == RELEASE)
  124. {
  125. if(leftBtnPush)
  126. {
  127. leftBtnPush = false;
  128. }
  129. }
  130. }
  131. if (Button2 == PRESS && !rightBtnPush)
  132. {
  133. if(!rightBtnPush)
  134. {
  135. rightBtnPush = true;
  136. if (_charging_mode == CHARGING_MODE_START)
  137. {
  138. _charging_mode = CHARGING_MODE_TERMINATING;
  139. printf("****************** Switch to Stop Mode ******************\n");
  140. }
  141. }
  142. else if (Button2 == RELEASE)
  143. {
  144. if(rightBtnPush)
  145. {
  146. rightBtnPush = false;
  147. }
  148. }
  149. }
  150. }
  151. void GetModuleCountCallback(byte group, byte count)
  152. {
  153. printf("group = %d, count = %d \n", group, count);
  154. if (group == SYSTEM_CMD)
  155. UnSafeDataInfo->PSU_COUNT = count;
  156. }
  157. void GetAvailableCapCallback(byte address, short maxVol, short minVol, short maxCur, short totalPow)
  158. {
  159. int _groupPower = 0, _groupCurrent = 0;
  160. UnSafeDataInfo->PsuModule[address].PSU_VOLTAGE_INFO = maxVol;
  161. UnSafeDataInfo->PsuModule[address].PSU_CURRENT_INFO = maxCur;
  162. UnSafeDataInfo->PsuModule[address].PSU_POWER_INFO = totalPow;
  163. for (byte index = 0; index < UnSafeDataInfo->PSU_COUNT; index++)
  164. {
  165. _groupCurrent += UnSafeDataInfo->PsuModule[address].PSU_CURRENT_INFO;
  166. _groupPower += UnSafeDataInfo->PsuModule[address].PSU_POWER_INFO;
  167. }
  168. UnSafeDataInfo->PSU_VOLTAGE = maxVol;
  169. UnSafeDataInfo->PSU_CURRENT = _groupCurrent;
  170. UnSafeDataInfo->PSU_POWER = _groupPower;
  171. }
  172. void GetStatusCallback(byte group, byte address, byte temp, int alarm)
  173. {
  174. printf("alarm = %d \n", alarm);
  175. }
  176. void GetInputVoltageCallback(byte address, unsigned short vol1, unsigned short vol2, unsigned short vol3)
  177. {
  178. printf("vol1 = %d, vol2 = %d, vol3 = %d \n", vol1, vol2, vol3);
  179. }
  180. int CreateShareMemory()
  181. {
  182. int MeterSMId;
  183. if ((MeterSMId = shmget(ShmTestKey, sizeof(struct UnSafeData), IPC_CREAT | 0777)) < 0)
  184. {
  185. return 0;
  186. }
  187. else if ((UnSafeDataInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  188. {
  189. return 0;
  190. }
  191. memset(UnSafeDataInfo, 0, sizeof(struct UnSafeData));
  192. return 1;
  193. }
  194. static void get_char(char *word)
  195. {
  196. fd_set rfds;
  197. struct timeval tv;
  198. FD_ZERO(&rfds);
  199. FD_SET(0, &rfds);
  200. tv.tv_sec = 0;
  201. tv.tv_usec = 10; //wait input timout time
  202. //if input
  203. if (select(1, &rfds, NULL, NULL, &tv) > 0)
  204. {
  205. fgets(word, 128, stdin);
  206. }
  207. }
  208. void GetInputString()
  209. {
  210. char word[128];
  211. char newString[7][10];
  212. int i, j, ctr;
  213. get_char(word);
  214. if (strlen(word) == 0)
  215. return;
  216. //fgets(word, sizeof(word), stdin);
  217. j = 0;
  218. ctr = 0;
  219. for (i = 0; i <= (strlen(word)); i++) {
  220. if (word[i] == ' ' || word[i] == '\0' || word[i] == 10) {
  221. newString[ctr][j] = '\0';
  222. ctr++;
  223. j = 0;
  224. } else {
  225. newString[ctr][j] = word[i];
  226. j++;
  227. }
  228. }
  229. VOLTAGE = atof(newString[0]);
  230. CURRENT = atof(newString[1]);
  231. FANRPM = atoi(newString[2]);
  232. if (VOLTAGE <= UnSafeDataInfo->PSU_VOLTAGE && CURRENT <= UnSafeDataInfo->PSU_CURRENT)
  233. {
  234. //printf("OutputVol = %f, OutputCur = %f \n", VOLTAGE, CURRENT);
  235. }
  236. else
  237. {
  238. ShowMainMsg();
  239. }
  240. }
  241. void GetIavailableCallback(byte address, unsigned short Iavail, unsigned short Vext)
  242. {
  243. //printf("address = %d, Iavail = %d, Vext = %d \n", address, Iavail, Vext);
  244. }
  245. void GetOutputAndTempCallback(byte address, unsigned short outputVol,
  246. unsigned short outputCur, unsigned short outputPower, unsigned char Temperature)
  247. {
  248. //printf("***Output Value and Temp*** address = %d, Vol = %d, Cur = %d, Pow = %d, Temp = %d \n",
  249. // address, outputVol, outputCur, outputPower, Temperature);
  250. }
  251. void GetModuleStatusCallback(byte address, unsigned char isErr, unsigned char status,
  252. unsigned char err1, unsigned char err2, unsigned char err3, unsigned char err4)
  253. {
  254. //int alarm = (err2 << 24) | (err3 << 16) | (err4 << 8);
  255. // err2 == state 2
  256. // err3 == state 1
  257. // err4 == state 0
  258. //printf("***Status*** address = %d, alarm = %d \n", address, alarm);
  259. // printf("***Status*** address = %d, err1 = %d, err2 = %d, err3 = %d, err4 = %d \n",
  260. // address, err1,err2,err3,err4);
  261. }
  262. void GetModuleInputCallback(byte address, unsigned short inputR,
  263. unsigned short inputS, unsigned short inputT)
  264. {
  265. }
  266. int main(void)
  267. {
  268. isOpen =false;
  269. if(CreateShareMemory() == 0)
  270. {
  271. printf("CreateShareMemory fail. \n");
  272. return 0;
  273. }
  274. RefreshModuleCount(&GetModuleCountCallback);
  275. RefreshAvailableCap(&GetAvailableCapCallback);
  276. RefreshStatus(&GetStatusCallback);
  277. RefreshInputVol(&GetInputVoltageCallback);
  278. RefreshIavailable(&GetIavailableCallback);
  279. AutoMode_RefreshOutputAndTemp(&GetOutputAndTempCallback);
  280. AutoMode_RefreshModuleStatus(&GetModuleStatusCallback);
  281. AutoMode_RefreshModuleInput(&GetModuleInputCallback);
  282. Uart1Fd = InitComPort();
  283. Uart5Fd = InitComPort5();
  284. libInitialize = InitialCommunication();
  285. if (Uart1Fd < 0 || !libInitialize)
  286. {
  287. printf("Initial port fail. \n");
  288. return 0;
  289. }
  290. if(Uart5Fd < 0)
  291. {
  292. printf("(Internal) open port error. \n");
  293. return 0;
  294. }
  295. sleep(5);
  296. gettimeofday(&_cmdSubPriority_time, NULL);
  297. VOLTAGE = 0.0;
  298. CURRENT = 0.0;
  299. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  300. // while (1)
  301. // {
  302. // printf("++++++++++++++2++++++++++++++++++++++++++++++++++++++\n");
  303. // SetWalkInConfig(0, YES, 0);
  304. // SetWalkInConfig(1, NO, 0);
  305. // printf("++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
  306. // sleep(1);
  307. // }
  308. //
  309. // sleep(1);
  310. // printf("++++++++++++++2++++++++++++++++++++++++++++++++++++++\n");
  311. // SetWalkInConfig(SYSTEM_CMD, NO, 0);
  312. // printf("++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
  313. // return 0;
  314. while (1)
  315. {
  316. GetInputGpioStatus();
  317. //ChkButtonStatus();
  318. // 切換 Walk-in mode (default 5s -> 2s)
  319. SetWalkInConfig(SYSTEM_CMD, NO, 0);
  320. int time = GetTimeoutValue(_cmdSubPriority_time) / 1000;
  321. while(isGetCount == YES)
  322. {
  323. if (_charging_mode == CHARGING_MODE_START)
  324. {
  325. // 取得模塊輸出額定電流能力
  326. GetModuleIavailable(0);
  327. }
  328. GetInputString();
  329. if (VOLTAGE > 150 && CURRENT >= 0)
  330. _charging_mode = CHARGING_MODE_START;
  331. else
  332. _charging_mode = CHARGING_MODE_TERMINATING;
  333. //printf("_charging_mode = %d \n", _charging_mode);
  334. if(PresentRPM != FANRPM)
  335. {
  336. if(Set_Fan_Speed(Uart5Fd, FANRPM) == PASS)
  337. {
  338. printf("Set Fan RPM: %d OK\n", FANRPM);
  339. }
  340. else
  341. {
  342. printf("Set Fan RPM: %d Fail\n", FANRPM);
  343. }
  344. }
  345. switch(_charging_mode)
  346. {
  347. case CHARGING_MODE_START:
  348. {
  349. if (!isOpen)
  350. {
  351. SwitchPower(SYSTEM_CMD, PSU_POWER_ON);
  352. FlashLed(SYSTEM_CMD, PSU_FLASH_ON);
  353. isOpen = true;
  354. //SetDirModulePresentOutput(0,
  355. // VOLTAGE * 10,
  356. // CURRENT * 10,
  357. // 0x01,
  358. // 0x01);
  359. }
  360. PresentOutputVol(SYSTEM_CMD, VOLTAGE * 10, CURRENT * 10);
  361. }
  362. break;
  363. case CHARGING_MODE_TERMINATING:
  364. {
  365. if (isOpen)
  366. {
  367. //SetDirModulePresentOutput(0,
  368. // VOLTAGE * 10,
  369. // CURRENT * 10,
  370. // 0x00,
  371. // 0x01);
  372. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  373. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  374. isOpen = false;
  375. }
  376. }
  377. break;
  378. }
  379. //GetStatus(0);
  380. //GetModuleInput(0);
  381. sleep(1);
  382. }
  383. if (UnSafeDataInfo->PSU_COUNT <= 0)
  384. {
  385. if (time > 1000)
  386. {
  387. printf("Step 1 : GetModuleCount...... \n");
  388. GetModuleCount(SYSTEM_CMD);
  389. gettimeofday(&_cmdSubPriority_time, NULL);
  390. }
  391. }
  392. else if (time < 5000)
  393. {
  394. printf("Step 2 : GetModuleCap...... \n");
  395. GetModuleCap(0);
  396. SwitchPower(SYSTEM_CMD, PSU_POWER_OFF);
  397. FlashLed(SYSTEM_CMD, PSU_FLASH_NORMAL);
  398. }
  399. else
  400. {
  401. ShowMainMsg();
  402. isGetCount = YES;
  403. }
  404. sleep(1);
  405. }
  406. return 0;
  407. }