您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在Linux中使用termios API(C ++)检测字符设备是否已断开连接

在Linux中使用termios API(C ++)检测字符设备是否已断开连接

首先值得一提的是,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));
         /* close fd and cleanup or reconnect etc...*/
         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流量控制,则还可以检测到线路中断。

其他 2022/1/1 18:15:02 有505人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶