samir82show

receive sms termios

Mar 5th, 2014
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. #include <stdio.h> /* Standard input/output definitions */
  2. #include <string.h> /* String function definitions */
  3. #include <unistd.h> /* UNIX standard function definitions */
  4. #include <fcntl.h> /* File control definitions */
  5. #include <errno.h> /* Error number definitions */
  6. #include <termios.h> /* POSIX terminal control definitions */
  7.  
  8. #define MAX 256
  9.  
  10. /*int
  11. ident_word(char *buf, char *word)
  12. {
  13.  
  14. switch (strstr())
  15. {
  16. case 0: printf();
  17. }
  18. return res;
  19. }*/
  20. void serial_init(int *fd, struct termios *options, struct termios *oldconf)
  21. {
  22. if (*fd == -1)
  23. perror("open_port: Unable to open /dev/ttyUSB0 - ");
  24. else
  25. fcntl(*fd, F_SETFL, 0);
  26. tcgetattr(*fd, oldconf);
  27. memset(options, 0, sizeof(*options));
  28.  
  29.  
  30. cfsetispeed(options, B4800);
  31. cfsetospeed(options, B4800);
  32.  
  33. options->c_cflag |= (CLOCAL | CREAD);
  34. options->c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  35. options.c_oflag &= ~OPOST;
  36. options->c_cc[VMIN] = 0;
  37. options->c_cc[VTIME] = 10;
  38. tcflush(*fd, TCIFLUSH);
  39. tcsetattr(*fd, TCSANOW, options);
  40. }
  41. int
  42. main()
  43. {
  44. int fd;
  45. /* struct command
  46. {
  47. char instruct1[20], instruct2[4];
  48. int num;
  49. } command;*/
  50. char line[MAX], *word = "+CMTI", cmd_r[] = "AT+CMGR=0\r", cmd_d[] = "AT+CMGD=0,4\r", cmd_mode[] = "AT+CMGF=1\r", read_buf[MAX];
  51. memset(read_buf, 0, sizeof(read_buf));
  52. struct termios options, oldconf;
  53.  
  54. fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
  55. serial_init(&fd, &options, &oldconf);
  56. write(fd, cmd_mode, sizeof(cmd_mode));
  57. while (1) {
  58. while (read(fd, line, sizeof(line)) > 0) {
  59. if (strstr(line, word) > 0) {
  60. write(fd, cmd_r, sizeof(cmd_r));
  61. read(fd, read_buf, sizeof(read_buf));
  62. write(1, read_buf, sizeof(read_buf));
  63. write(fd, cmd_d, sizeof(cmd_d));
  64. }
  65. }
  66. }
  67. tcsetattr(fd, TCSANOW, &oldconf);
  68. return (0);
  69. }
Advertisement
Add Comment
Please, Sign In to add comment