Module_InternalComm.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include <stdio.h> /*標準輸入輸出定義*/
  2. #include <stdlib.h> /*標準函數庫定義*/
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <termios.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <sys/timeb.h>
  9. #include <sys/ioctl.h>
  10. #include <sys/time.h>
  11. #include "../Config.h"
  12. #include "../Log/log.h"
  13. #include "../ShareMemory/shmMem.h"
  14. #include "Module_InternalComm.h"
  15. //------------------------------------------------------------------------------
  16. #define INTERNAL_COM_PORT ("/dev/ttyS5")
  17. //------------------------------------------------------------------------------
  18. extern void RelayBoardTask(int uartFD);
  19. //extern void LEDBoardTask(int uartFD);
  20. //extern void FanBoardTask(int uartFD);
  21. extern void AcPlugTask(int uartFD);
  22. //------------------------------------------------------------------------------
  23. int DiffTimeb(struct timeb ST, struct timeb ET)
  24. {
  25. //return milli-second
  26. unsigned int StartTime, StopTime;
  27. StartTime = (unsigned int) ST.time;
  28. StopTime = (unsigned int) ET.time;
  29. //return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  30. return (StopTime - StartTime);
  31. }
  32. uint32_t GetTimeoutValue(struct timeval _sour_time)
  33. {
  34. struct timeval _end_time;
  35. gettimeofday(&_end_time, NULL);
  36. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  37. }
  38. void GetClockTime(struct timespec* _now_time, void* null)
  39. {
  40. clock_gettime(CLOCK_MONOTONIC, _now_time);
  41. }
  42. unsigned long GetClockTimeoutValue(struct timespec _start_time)
  43. {
  44. struct timespec ts_end;
  45. unsigned long ret = 0;
  46. clock_gettime(CLOCK_MONOTONIC, &ts_end);
  47. ret = ((unsigned long)(ts_end.tv_sec - _start_time.tv_sec) * 1000000) + ((unsigned long)((ts_end.tv_nsec / 1000) - (_start_time.tv_nsec / 1000)));
  48. return ret;
  49. }
  50. static int Init485ComPort(void)
  51. {
  52. int fd;
  53. struct termios tios;
  54. fd = open(INTERNAL_COM_PORT, O_RDWR);
  55. if (fd <= 0) {
  56. log_error("Module_InternalComm. InitComPort NG");
  57. sleep(5);
  58. return -1;
  59. }
  60. ioctl (fd, TCGETS, &tios);
  61. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  62. tios.c_lflag = 0;
  63. tios.c_iflag = 0;
  64. tios.c_oflag = 0;
  65. tios.c_cc[VMIN] = 0;
  66. tios.c_cc[VTIME] = (uint8_t)0; // timeout 0.5 second
  67. tios.c_lflag = 0;
  68. tcflush(fd, TCIFLUSH);
  69. ioctl (fd, TCSETS, &tios);
  70. return fd;
  71. }
  72. int main(int argc, char *argv[])
  73. {
  74. int fd = 0;
  75. int isContinue = 1;
  76. struct AlarmCodeData *pAlarmCode = NULL;
  77. if (CreateAllCsuShareMemory() == FAIL) {
  78. log_error("create share memory error");
  79. return FAIL;
  80. }
  81. MappingGunChargingInfo("InternalComm Task");
  82. fd = Init485ComPort();
  83. if (fd == FAIL) {
  84. pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  85. pAlarmCode->AlarmEvents.bits.CsuInitFailed = YES;
  86. log_info("ModuleInternalComTask create port error...");
  87. return FAIL;
  88. }
  89. RelayBoardTask(fd);
  90. usleep(100000);
  91. //LEDBoardTask(fd);
  92. //usleep(100000);
  93. //FanBoardTask(fd);
  94. while (isContinue) {
  95. if(AC_QUANTITY > 0)
  96. {
  97. AcPlugTask(fd);
  98. }
  99. usleep(100000);
  100. }
  101. return 0;
  102. }