Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> /* Standard input/output definitions */
- #include <string.h> /* String function definitions */
- #include <unistd.h> /* UNIX standard function definitions */
- #include <fcntl.h> /* File control definitions */
- #include <errno.h> /* Error number definitions */
- #include <termios.h> /* POSIX terminal control definitions */
- #include <stdlib.h>
- int
- modem_send_cmd(int *fd, char *cmd, char *ret) {
- int rd;
- tcflush(*fd, TCIFLUSH);
- write(*fd, cmd, strlen(cmd));
- sleep(1);
- rd = read(*fd, ret, 255);
- ret[rd] = '\0';
- return (0);
- }
- int
- sendsms(int *fd, char *num, char *msg)
- {
- char *cmd1 = "AT+CMGF=1\r";
- char *cmd2;
- char res[256];
- char *ctl_z = "\x1A\x00";
- cmd2 = (char*)malloc(strlen(num) + 20);
- strcpy(cmd2, "AT+CMGS=\"");
- strcat(cmd2, num);
- strcat(cmd2, "\"\r");
- modem_send_cmd(fd, cmd1, res);
- modem_send_cmd(fd, cmd2, res);
- modem_send_cmd(fd, msg, res);
- modem_send_cmd(fd, ctl_z, res);
- return (0);
- }
- int
- main(int argc, char **argv)
- {
- int fd;
- if (argc != 3) {
- printf("usage: %s Message Phone_no\n", argv[0]);
- exit (3);
- }
- char *msg = (char*) malloc(50 * sizeof(char));
- char *phone = (char*) malloc(10 * sizeof(char));
- strcpy(msg, argv[1]);
- strcpy(phone, argv[2]);
- struct termios options, oldconf;
- fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
- if (fd == -1)
- perror("open_port: Unable to open /dev/ttyUSB0 - ");
- else
- fcntl(fd, F_SETFL, 0);
- tcgetattr(fd, &oldconf);
- memset(&options, 0, sizeof(options));
- cfsetispeed(&options, B4800);
- cfsetospeed(&options, B4800);
- options.c_cflag |= (CLOCAL | CREAD);
- options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
- options.c_oflag &= ~OPOST;
- options.c_cc[VMIN] = 0;
- options.c_cc[VTIME] = 10;
- tcflush(fd, TCIFLUSH);
- tcsetattr(fd, TCSANOW, &options);
- if (sendsms(&fd, phone, msg) == 0)
- printf("successfully sent\n");
- /*
- * kplease return the terminal to normal as it was here
- */
- tcsetattr(fd,TCSANOW,&oldconf);
- close(fd);
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment