samir82show

send_sms from linux host

Mar 2nd, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 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. #include <stdlib.h>
  8.  
  9. int
  10. modem_send_cmd(int *fd, char *cmd, char *ret) {
  11. int rd;
  12.  
  13. tcflush(*fd, TCIFLUSH);
  14. write(*fd, cmd, strlen(cmd));
  15. sleep(1);
  16. rd = read(*fd, ret, 255);
  17. ret[rd] = '\0';
  18.  
  19. return (0);
  20. }
  21.  
  22. int
  23. sendsms(int *fd, char *num, char *msg)
  24. {
  25. char *cmd1 = "AT+CMGF=1\r";
  26. char *cmd2;
  27. char res[256];
  28.  
  29. char *ctl_z = "\x1A\x00";
  30.  
  31. cmd2 = (char*)malloc(strlen(num) + 20);
  32. strcpy(cmd2, "AT+CMGS=\"");
  33. strcat(cmd2, num);
  34. strcat(cmd2, "\"\r");
  35.  
  36. modem_send_cmd(fd, cmd1, res);
  37. modem_send_cmd(fd, cmd2, res);
  38. modem_send_cmd(fd, msg, res);
  39. modem_send_cmd(fd, ctl_z, res);
  40.  
  41. return (0);
  42. }
  43.  
  44.  
  45. int
  46. main(int argc, char **argv)
  47. {
  48. int fd;
  49. if (argc != 3) {
  50. printf("usage: %s Message Phone_no\n", argv[0]);
  51. exit (3);
  52. }
  53.  
  54. char *msg = (char*) malloc(50 * sizeof(char));
  55. char *phone = (char*) malloc(10 * sizeof(char));
  56. strcpy(msg, argv[1]);
  57. strcpy(phone, argv[2]);
  58. struct termios options, oldconf;
  59. fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
  60. if (fd == -1)
  61. perror("open_port: Unable to open /dev/ttyUSB0 - ");
  62. else
  63. fcntl(fd, F_SETFL, 0);
  64. tcgetattr(fd, &oldconf);
  65. memset(&options, 0, sizeof(options));
  66.  
  67.  
  68. cfsetispeed(&options, B4800);
  69. cfsetospeed(&options, B4800);
  70.  
  71. options.c_cflag |= (CLOCAL | CREAD);
  72. options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  73. options.c_oflag &= ~OPOST;
  74. options.c_cc[VMIN] = 0;
  75. options.c_cc[VTIME] = 10;
  76. tcflush(fd, TCIFLUSH);
  77. tcsetattr(fd, TCSANOW, &options);
  78. if (sendsms(&fd, phone, msg) == 0)
  79. printf("successfully sent\n");
  80. /*
  81. * kplease return the terminal to normal as it was here
  82. */
  83. tcsetattr(fd,TCSANOW,&oldconf);
  84. close(fd);
  85. return (0);
  86. }
Advertisement
Add Comment
Please, Sign In to add comment