tcgetattr_to_ioctl.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <termios.h>
  6. #include <time.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <sys/select.h>
  10. #include <string.h>
  11. #include <sys/ioctl.h>
  12. #define TTYDEVNAME "/dev/ttyS4"
  13. #define BAUDRATE B38400
  14. int speed_arr[] = {B115200, B57600, B38400, B19200, B9600, B4800, B2400, B1200, B300};
  15. int name_arr[] = {115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200, 300};
  16. int tcgetattr_to_ioctl(void)
  17. {
  18. int fd, flag, rd_num = 0;
  19. struct termios newtios, oldtios;
  20. struct timeval timeout;
  21. speed_t baud_rate_i, baud_rate_o;
  22. char recv_buf[256];
  23. int baudrate = 115200;
  24. char dev_file[32];
  25. int len;
  26. unsigned char send_msg_flag = 0;
  27. fd_set fds;
  28. int ret=0;
  29. int i=0;
  30. int size = sizeof(speed_arr) / sizeof(int);
  31. for( i = 0; i < size; i++)
  32. {
  33. if(BAUDRATE == speed_arr[i])
  34. {
  35. baudrate = name_arr[i];
  36. break;
  37. }
  38. }
  39. fd = open(TTYDEVNAME, O_RDWR | O_NONBLOCK);
  40. if(fd < 0)
  41. {
  42. printf("can not open %s!\n", TTYDEVNAME);
  43. exit(1);
  44. }
  45. else
  46. printf("open %s ok! baudrate = %d\n", TTYDEVNAME, baudrate);
  47. memset(&oldtios, 0, sizeof(oldtios));
  48. ret = tcgetattr(fd, &oldtios);
  49. if(ret == 0)
  50. {
  51. printf("tcgetattr old termios is:\n");
  52. printf("oldtios.c_iflag=0x%08x;\n", oldtios.c_iflag);
  53. printf("oldtios.c_oflag=0x%08x;\n", oldtios.c_oflag);
  54. printf("oldtios.c_cflag=0x%08x;\n", oldtios.c_cflag);
  55. printf("oldtios.c_lflag=0x%08x;\n", oldtios.c_lflag);
  56. printf("oldtios.c_line=0x%08x;\n", oldtios.c_line);
  57. }
  58. newtios = oldtios;
  59. memset(&oldtios, 0, sizeof(oldtios));
  60. ret = ioctl(fd, TCGETA, &oldtios);
  61. if(ret == 0)
  62. {
  63. printf("ioctl old termios is:\n");
  64. printf("oldtios.c_iflag=0x%08x;\n", oldtios.c_iflag);
  65. printf("oldtios.c_oflag=0x%08x;\n", oldtios.c_oflag);
  66. printf("oldtios.c_cflag=0x%08x;\n", oldtios.c_cflag);
  67. printf("oldtios.c_lflag=0x%08x;\n", oldtios.c_lflag);
  68. printf("oldtios.c_line=0x%08x;\n", oldtios.c_line);
  69. }
  70. newtios.c_cflag &= ~CBAUD;
  71. newtios.c_cflag |= BAUDRATE;
  72. newtios.c_cflag &= ~CSIZE;
  73. newtios.c_cflag |= CS8;
  74. newtios.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
  75. newtios.c_oflag &= ~OPOST; /*Output*/
  76. newtios.c_cflag &= ~CSTOPB;
  77. newtios.c_cc[VTIME] = 5; // 0.5 seconds
  78. newtios.c_cc[VMIN] = 1;
  79. newtios.c_cflag &= ~HUPCL;
  80. newtios.c_iflag &= ~INPCK;
  81. newtios.c_iflag |= IGNBRK;
  82. newtios.c_iflag &= ~ICRNL;
  83. newtios.c_iflag &= ~IXON;
  84. newtios.c_lflag &= ~IEXTEN;
  85. newtios.c_lflag &= ~ECHOK;
  86. newtios.c_lflag &= ~ECHOCTL;
  87. newtios.c_lflag &= ~ECHOKE;
  88. newtios.c_oflag &= ~ONLCR;
  89. printf("new termios is changed:\n");
  90. printf("newtios.c_iflag=0x%08x;\n", newtios.c_iflag);
  91. printf("newtios.c_oflag=0x%08x;\n", newtios.c_oflag);
  92. printf("newtios.c_cflag=0x%08x;\n", newtios.c_cflag);
  93. printf("newtios.c_lflag=0x%08x;\n", newtios.c_lflag);
  94. printf("newtios.c_line=0x%08x;\n", newtios.c_line);
  95. //tcflush(fd, TCIFLUSH); /* Update the options and do it NOW */
  96. tcsetattr(fd, TCSAFLUSH, &newtios);
  97. ret = tcgetattr(fd, &newtios);
  98. if(ret == 0)
  99. {
  100. printf("now tcgetattr termios is:\n");
  101. printf("newtios.c_iflag=0x%08x;\n", newtios.c_iflag);
  102. printf("newtios.c_oflag=0x%08x;\n", newtios.c_oflag);
  103. printf("newtios.c_cflag=0x%08x;\n", newtios.c_cflag);
  104. printf("newtios.c_lflag=0x%08x;\n", newtios.c_lflag);
  105. printf("newtios.c_line=0x%08x;\n", newtios.c_line);
  106. }
  107. ret = ioctl(fd, TCGETA, &newtios);
  108. if(ret == 0)
  109. {
  110. printf("now ioctl termios is:\n");
  111. printf("newtios.c_iflag=0x%08x;\n", newtios.c_iflag);
  112. printf("newtios.c_oflag=0x%08x;\n", newtios.c_oflag);
  113. printf("newtios.c_cflag=0x%08x;\n", newtios.c_cflag);
  114. printf("newtios.c_lflag=0x%08x;\n", newtios.c_lflag);
  115. printf("newtios.c_line=0x%08x;\n", newtios.c_line);
  116. }
  117. while(1)
  118. {
  119. memset(recv_buf, 0x00, sizeof(recv_buf));
  120. printf("Input AT cammand:\n");
  121. fflush(stdin);
  122. if(fgets(recv_buf, 254, stdin) == NULL)
  123. continue;
  124. //printf("read from stdin: %s\n", recv_buf);
  125. if((!memcmp(recv_buf, "quit", 4) || !memcmp(recv_buf, "exit", 4)))
  126. break;
  127. if(memcmp(recv_buf, "AT", 2) && memcmp(recv_buf, "at", 2))
  128. continue;
  129. len = strlen(recv_buf);
  130. recv_buf[--len] = 0x00;
  131. recv_buf[len++] = 0x0D;
  132. recv_buf[len++] = 0x0A;
  133. tcflush(fd, TCIFLUSH);
  134. //printf("wrtie length: %d data: %s(%d) ", len, recv_buf,send_msg_flag);
  135. if(write(fd, recv_buf, len) <= 0)
  136. continue;
  137. FD_ZERO(&fds);
  138. FD_SET(fd, &fds);
  139. timeout.tv_sec = 0;
  140. timeout.tv_usec =100000;
  141. while(1)
  142. {
  143. select(fd+1, &fds, NULL, NULL, &timeout);
  144. if(!FD_ISSET(fd, &fds))
  145. break;
  146. //if(!memcmp(recv_buf, "AT^HCMGS=", 9))
  147. // send_msg_flag = 1;
  148. memset(recv_buf, 0x00, sizeof(recv_buf));
  149. rd_num = read(fd, recv_buf, sizeof(recv_buf));
  150. if(rd_num > 0)
  151. {
  152. #if 0
  153. int i=0;
  154. printf("\n");
  155. for(i=0; i<rd_num; i++)
  156. {
  157. printf("%02x ", recv_buf[i]);
  158. }
  159. printf("\n");
  160. printf("read length: %d\n"
  161. "data: %s\n", rd_num, recv_buf);
  162. #endif
  163. printf("\n%s\n", recv_buf);
  164. }
  165. }
  166. }
  167. close(fd);
  168. return 0;
  169. }
  170. int main(void)
  171. {
  172. tcgetattr_to_ioctl();
  173. return 0;
  174. }