Holger Hans Peter Freyther | ae73a05 | 2013-05-31 09:51:53 +0200 | [diff] [blame] | 1 | #include <termios.h> |
| 2 | #include <stdio.h> |
| 3 | #include <errno.h> |
| 4 | |
| 5 | int serial_configure(int fd) |
| 6 | { |
| 7 | struct termios term; |
| 8 | int rc; |
| 9 | |
| 10 | rc = tcgetattr(fd, &term); |
| 11 | if (rc != 0) { |
| 12 | printf("Error with tcgetattr: %d\n", errno); |
| 13 | return -1; |
| 14 | } |
| 15 | |
| 16 | /* taken from libqcdm but I doubt that is copyrightable */ |
| 17 | term.c_cflag &= ~(CBAUD | CSIZE | CSTOPB | CLOCAL | PARENB); |
| 18 | term.c_iflag &= ~(HUPCL | IUTF8 | IUCLC | ISTRIP | IXON | IXOFF | IXANY | ICRNL); |
| 19 | term.c_oflag &= ~(OPOST | OCRNL | ONLCR | OLCUC | ONLRET); |
| 20 | term.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO | ECHOE | ECHOK | ECHONL); |
| 21 | term.c_lflag &= ~(NOFLSH | XCASE | TOSTOP | ECHOPRT | ECHOCTL | ECHOKE); |
| 22 | term.c_cc[VMIN] = 1; |
| 23 | term.c_cc[VTIME] = 0; |
| 24 | term.c_cc[VEOF] = 1; |
| 25 | term.c_cflag |= (B115200 | CS8 | CREAD | 0 | 0); |
| 26 | |
| 27 | rc = tcsetattr(fd, TCSANOW, &term); |
| 28 | if (rc != 0) { |
| 29 | printf("Failed to set new attributes: %d\n", errno); |
| 30 | return -2; |
| 31 | } |
| 32 | |
| 33 | return 0; |
| 34 | } |