【发布时间】:2020-10-24 14:02:10
【问题描述】:
我有一个打开 /dev/ttyUSB* 端口的 C 应用程序
fd = open(portName.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_iflag &= (tcflag_t) ~(INLCR | IGNCR | ICRNL | IGNBRK);
options.c_oflag &= (tcflag_t) ~(OPOST);
options.c_cflag |= CS8;
options.c_cflag |= CRTSCTS;
options.c_lflag &= ~(ICANON | ECHO | ISIG);
tcsetattr(fd, TCSANOW, &options);
struct serial_struct kernel_serial_settings;
if (ioctl(fd, TIOCGSERIAL, &kernel_serial_settings) == 0) {
kernel_serial_settings.flags |= ASYNC_LOW_LATENCY;
kernel_serial_settings..
ioctl(fd, TIOCSSERIAL, &kernel_serial_settings);
}
端口打开,我接收数据。但是,如果 USB 设备稍后断开连接,我调用读取函数:
nRead = _read(fd, buffer, length);
nRead 将返回 0。我希望它应该返回一个负数来指示错误(设备不再可用)。
如何检测打开的设备已断开连接?
【问题讨论】:
标签: c linux serial-port