43 struct termios orig_termios, cur_termios;
46 const speed_t baud_rate = B115200;
48 if (!port_name || !port_name[0]) { errno = EINVAL;
return -1; }
49 fd =
open(port_name, O_RDWR | O_NOCTTY | O_NONBLOCK | O_CLOEXEC);
52 fprintf(stderr,
"USHER:\tfailed to open serial device %s: %s\n", port_name, strerror(errno));
56 if (tcgetattr(
fd, &orig_termios)) {
60 cur_termios = orig_termios;
63 cur_termios.c_iflag &= ~(IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP | INLCR | IGNCR
64 | ICRNL | IXON | IXANY | IXOFF | IMAXBEL);
66 cur_termios.c_iflag |= BRKINT;
69 cur_termios.c_oflag &= ~(OPOST | ONLCR | OCRNL | ONOCR | ONLRET);
72 cur_termios.c_cflag &= ~(CSIZE | CSTOPB | CREAD | PARENB | PARODD | HUPCL | CLOCAL);
73 cur_termios.c_cflag |= CREAD | CS8 | CLOCAL;
76 cur_termios.c_lflag &= ~(ISIG | ICANON | IEXTEN | ECHO);
77 cur_termios.c_lflag |= NOFLSH;
78 cur_termios.c_cc[VMIN] = 1;
79 cur_termios.c_cc[VTIME] = 0;
81 if (cfsetispeed(&cur_termios, baud_rate) || cfsetospeed(&cur_termios, baud_rate)) {
86 if (tcsetattr(
fd, TCSANOW, &cur_termios)) {
94 const int error = errno;
97 fprintf(stderr,
"USHER:\tfailed to configure serial device %s: %s\n", port_name, strerror(
error));