我写了一个必须在Linux上使用串口的应用程序,尤其是ttyUSB.使用标准的select()/ read()循环和write()执行读写操作,并且它们可能没有任何问题,但初始化代码(或缺少某些部分)会损坏tty子系统中的某些内容.这里是:
vuxboot(string filename, unsigned baud = B115200) : _debug(false) {
_fd = open(filename.c_str(), O_RDWR | O_NOCTTY);
if(_fd < 0) throw new io_error("cannot open port");
// Serial initialization was written with FTDI USB-to-serial converters
// in mind. Anyway, who wants to use non-8n1 protocol?
tcgetattr(_fd, &_termios);
termios tio = {0};
tio.c_iflag = IGNPAR;
tio.c_oflag = 0;
tio.c_cflag = baud | CLOCAL | CREAD | CS8;
tio.c_lflag = 0;
tcflush(_fd, TCIFLUSH);
tcsetattr(_fd, TCSANOW, &tio);
}
Run Code Online (Sandbox Code Playgroud)
另一个tcsetattr(_fd, TCSANOW, &_termios)坐在析构函数中,但它无关紧要.
无论有没有这个termios初始化, …