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

chr*_*892 16 c++ linux serial-port termios

我在Linux中使用termios api与串行设备通信.我正在尝试检测设备是否已断开连接,以便在超时后尝试重新连接.我有以下示例代码:

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
    select(tty_fd+1, &rfds, NULL, NULL, NULL);

    // 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();
}
Run Code Online (Sandbox Code Playgroud)

在ttyUSB设备物理断开连接后,我实际上并没有看到select()或fcntl返回错误值(-1).当然,我可以检查/ dev /中的文件是否存在,但我希望有一个更优雅的解决方案.

非常感谢任何建议,谢谢!

Maq*_*fel 5

首先值得一提的是,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();
    }
}
Run Code Online (Sandbox Code Playgroud)

很遗憾您没有说明您使用的是哪种设备。

如果您的设备能够进行 RTS/CTS 流控制,则也可以检测到换行符。