#define _DEFAULT_SOURCE 1 #include #include #include "serial.h" int set_terminal_attributes(int fd) { int ret; struct termios tty; ret = tcgetattr(fd, &tty); if (ret == -1) { perror("tcgetattr"); return -1; } tty.c_cflag &= ~PARENB; // no parity tty.c_cflag &= ~CSTOPB; // one stop bit tty.c_cflag &= ~CSIZE; // clear size bits tty.c_cflag |= CS8; // 8 bit tty.c_cflag &= ~CRTSCTS; // disable RTS/CTS hardware flow control tty.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines tty.c_lflag &= ~ICANON; tty.c_lflag &= ~ECHO; // disable echo tty.c_lflag &= ~ECHOE; // disable erasure tty.c_lflag &= ~ECHONL; // disable new-line echo tty.c_lflag &= ~ISIG; // disable interpretation of INTR, QUIT and SUSP tty.c_iflag &= ~(IXON|IXOFF|IXANY); // turn off xon/xoff ctrl tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // disable any special handling of received bytes tty.c_oflag &= ~OPOST; // prevent special interpretation of output bytes (e.g. newline chars) tty.c_oflag &= ~ONLCR; // prevent conversion of newline to carriage return/line feed tty.c_cc[VMIN] = 1; tty.c_cc[VTIME] = 0; cfsetospeed(&tty, B1200); cfsetispeed(&tty, B1200); ret = tcsetattr(fd, TCSANOW, &tty); if (ret == -1) { perror("tcsetattr"); return -1; } return 0; }