16 struct termios
term::options() {
21 if (tcgetattr(m_fd, &opts)) {
33 if (tcsetattr(
m_fd, TCSANOW, &opts)) {
41 m_fd =
::open(port.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
44 "opening " + port + std::string(
" failed: ") + std::string(std::strerror(errno)), errno);
48 if (fcntl(
m_fd, F_SETFL, FNDELAY)) {
54 struct termios opts =
options();
57 opts.c_cflag |= (CLOCAL | CREAD);
59 opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
60 opts.c_oflag &= ~OPOST;
63 if (cfsetispeed(&opts, baudrate)) {
68 if (cfsetospeed(&opts, baudrate)) {
75 opts.c_cflag &= ~CSIZE;
76 opts.c_cflag |=
static_cast<tcflag_t
>(charsize);
81 opts.c_cflag &= ~PARENB;
84 opts.c_cflag |= PARENB;
85 opts.c_cflag &= ~PARODD;
88 opts.c_cflag |= PARENB;
89 opts.c_cflag |= PARODD;
99 opts.c_cflag &= ~CSTOPB;
102 opts.c_cflag |= CSTOPB;
141 return ::write(
m_fd, data, len);
148 return ::read(
m_fd, data, len);
void open(std::string port, speed_t baudrate=B9600, charsize_t charsize=charsize_t::_8, parity_t parity=parity_t::NONE, stopbits_t stopbits=stopbits_t::_1)
Open a terminal device.
std::streamsize write(const char *data, std::size_t len)
Write data to the terminal.
void close()
Close the terminal.
std::streamsize read(char *data, std::size_t len)
Read data from the terminal.
struct termios options()
Access terminal options.
void set_options(struct termios &opts)
Set terminal options.
A class that allows to read and write from/to terminal devices.