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 */
- #define MAX 256
- /*int
- ident_word(char *buf, char *word)
- {
- switch (strstr())
- {
- case 0: printf();
- }
- return res;
- }*/
- void serial_init(int *fd, struct termios *options, struct termios *oldconf)
- {
- 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);
- }
- int
- main()
- {
- int fd;
- /* struct command
- {
- char instruct1[20], instruct2[4];
- int num;
- } command;*/
- 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];
- memset(read_buf, 0, sizeof(read_buf));
- struct termios options, oldconf;
- fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
- serial_init(&fd, &options, &oldconf);
- write(fd, cmd_mode, sizeof(cmd_mode));
- while (1) {
- while (read(fd, line, sizeof(line)) > 0) {
- if (strstr(line, word) > 0) {
- write(fd, cmd_r, sizeof(cmd_r));
- read(fd, read_buf, sizeof(read_buf));
- write(1, read_buf, sizeof(read_buf));
- write(fd, cmd_d, sizeof(cmd_d));
- }
- }
- }
- tcsetattr(fd, TCSANOW, &oldconf);
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment