Module_InternalComm.c 2.9 KB

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