Created
October 13, 2013 14:57
-
-
Save dagon666/6963233 to your computer and use it in GitHub Desktop.
serial port initialization routines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define BAUD_2400 B2400 | |
| #define BAUD_4800 B4800 | |
| #define BAUD_9600 B9600 | |
| #define BAUD_38400 B38400 | |
| #define BAUD_57600 B57600 | |
| #define BAUD_115200 B115200 | |
| static int tty_attrib_conf(int fd, int speed, int parity) { | |
| struct termios tty; | |
| memset (&tty, 0, sizeof(tty)); | |
| if (tcgetattr (fd, &tty) != 0) { | |
| fprintf(stderr, "Error %d from tcgetattr\n", errno); | |
| return -1; | |
| } | |
| // set I/O tty speed | |
| cfsetospeed (&tty, speed); | |
| cfsetispeed (&tty, speed); | |
| // 8-bit chars; | |
| tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; | |
| // disable IGNBRK for mismatched speed tests; otherwise receive break as \000 chars | |
| // ignore break signal | |
| tty.c_iflag &= ~IGNBRK; | |
| // no signaling chars, no echo, | |
| tty.c_lflag = 0; | |
| // no canonical processing | |
| // no remapping, no delays | |
| tty.c_oflag = 0; | |
| // read doesn't block | |
| tty.c_cc[VMIN] = 0; | |
| // 0.5 seconds read timeout | |
| tty.c_cc[VTIME] = 5; | |
| // shut off xon/xoff ctrl | |
| tty.c_iflag &= ~(IXON | IXOFF | IXANY); | |
| // ignore modem controls, | |
| tty.c_cflag |= (CLOCAL | CREAD); | |
| // enable reading / shut off parity | |
| tty.c_cflag &= ~(PARENB | PARODD); | |
| tty.c_cflag |= parity; | |
| tty.c_cflag &= ~CSTOPB; | |
| tty.c_cflag &= ~CRTSCTS; | |
| if (tcsetattr (fd, TCSANOW, &tty) != 0) { | |
| fprintf(stderr, "Error %d from tcsetattr\n", errno); | |
| return -1; | |
| } | |
| return 0; | |
| } | |
| /** | |
| * @brief whether a port should block (read() will block until the requested amount of bytes is received) or not | |
| * | |
| * @param fd port descriptor | |
| * @param should_block 1 - should block, 0 shouldn't | |
| */ | |
| static void tty_block_conf(int fd, int should_block) { | |
| struct termios tty; | |
| memset (&tty, 0, sizeof tty); | |
| if (tcgetattr (fd, &tty) != 0) { | |
| fprintf(stderr, "Error %d from tcgetattr\n", errno); | |
| return; | |
| } | |
| tty.c_cc[VMIN] = should_block ? 1 : 0; | |
| tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout | |
| if (tcsetattr (fd, TCSANOW, &tty) != 0) { | |
| fprintf(stderr, "Error %d setting term attributes\n", errno); | |
| return; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment