首先值得一提的是,serial-usb的行为如下:
在USB设备上,拔出的断开连接称为
@disconnect:当不再可访问该接口时调用,通常是因为其设备已(或正在)断开连接或正在卸载驱动程序模块。
在我们的例子中是usb_serial_disconnect(struct usb_interface * interface)
调用usb_serial_console_disconnect(serial),调用tty_hangup …,依此类推。
您可以遵循从这里开始的链条:http : //lxr.free-
electrons.com/source/drivers/usb/serial/usb-serial.c#L1091
简而言之,这导致以下经典方式:
pselect表示文件描述符已准备就绪,并且ioctl(fd,FIONREAD和&len)返回零len。
就是这样,您拔出了设备。
求和(来自您的代码):
while(1){ FD_ZERO(&rfds); FD_SET(tty_fd, &rfds); // have tried checking fcntl(tty_fd, F_GETFL); too // Blocking call to wait until we have data int ready = select(tty_fd + 1, &rfds, NULL, NULL, NULL); if(ready && FD_ISSET(tty_fd, &rfds)) { size_t len = 0; ioctl(tty_fd, FIONREAD, &len); errsv = errno; if(len == 0) { printf("prog_name: zero read from the device: %s.", strerror(errsv)); exit(EXIT_FAILURE); } // While we have data, collect it while (read(tty_fd, &c, 1)>0 && bytesRead++<200) { serialBuffer.push_back(c); } bytesRead = 0; // Try to parse it BufferParse(); }}很遗憾您没有说明您使用的是哪种设备。
如果您的设备能够进行RTS / CTS流量控制,则还可以检测到线路中断。



