unsigned TTYPort::SetBaudrate(unsigned BaudRate) { if (fd < 0) return 0; speed_t speed = baud_rate_to_speed_t(BaudRate); if (speed == B0) /* not supported */ return 0; unsigned old = baud_rate; struct termios attr; if (tcgetattr(fd, &attr) < 0) return 0; attr.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); attr.c_oflag &= ~OPOST; attr.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); attr.c_cflag &= ~(CSIZE | PARENB | CRTSCTS | IXON | IXOFF); attr.c_cflag |= (CS8 | CLOCAL); attr.c_cc[VMIN] = 0; attr.c_cc[VTIME] = 1; cfsetospeed(&attr, speed); cfsetispeed(&attr, speed); if (tcsetattr(fd, TCSANOW, &attr) < 0) { close(fd); return 0; } baud_rate = BaudRate; return old; }
bool TTYPort::SetBaudrate(unsigned BaudRate) { assert(tty.IsDefined()); speed_t speed = baud_rate_to_speed_t(BaudRate); if (speed == B0) /* not supported */ return false; struct termios attr; if (!tty.GetAttr(attr)) return false; attr.c_iflag &= ~(BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); attr.c_iflag |= (IGNPAR | IGNBRK); attr.c_oflag &= ~OPOST; attr.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); attr.c_cflag &= ~(CSIZE | PARENB | CRTSCTS); attr.c_cflag |= (CS8 | CLOCAL); attr.c_cc[VMIN] = 0; attr.c_cc[VTIME] = 1; cfsetospeed(&attr, speed); cfsetispeed(&attr, speed); if (!tty.SetAttr(TCSANOW, attr)) return false; baud_rate = BaudRate; return true; }