使用 termios api (c++) 在 Linux 中检测字符设备是否已断开连接 [英] Detecting if a character device has disconnected in Linux in with termios api (c++)

查看:12
本文介绍了使用 termios api (c++) 在 Linux 中检测字符设备是否已断开连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using the termios api in Linux to communicate with a serial device. I'm trying to detect if the device has disconnected so I can try to reconnect after some timeout. I have the following example code:

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();
}

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

I'm not actually seeing select() or fcntl return error values (-1) after the ttyUSB device is physically disconnected. I could, of course, check to see if the file in /dev/ exists, but I was hoping there was a more elegant solution.

不胜感激,谢谢!

推荐答案

首先值得一提的是serial-usb的行为如下:

First of all it worth mentioning, that the behavior serial-usb is following:

在 USB 设备上调用拔出断开连接

On usb device unplugged disconnect is called

@disconnect:接口不再可访问时调用,通常因为它的设备已经(或正在)断开连接或正在卸载驱动模块.

@disconnect: Called when the interface is no longer accessible, usually because its device has been (or is being) disconnected or the driver module is being unloaded.

在我们的例子中是 usb_serial_disconnect(struct usb_interface *interface)

in our case it is usb_serial_disconnect(struct usb_interface *interface)

调用usb_serial_console_disconnect(serial),调用tty_hangup ...等等.

which calles usb_serial_console_disconnect(serial), which calles tty_hangup ... and so on.

您可以从这里开始关注链:http://lxr.free-electrons.com/source/drivers/usb/serial/usb-serial.c#L1091

You can follow chain started from here: http://lxr.free-electrons.com/source/drivers/usb/serial/usb-serial.c#L1091

简而言之,这导致以下经典方式:

In short this results in following classic manner:

pselect 表示文件描述符已准备就绪,并且 ioctl(fd, FIONREAD, &len) 返回零 len.

pselect signals that file descriptor is ready and ioctl(fd, FIONREAD, &len) returns zero len.

就是你拔掉了设备.

总结(源自您的代码):

Summurizing (derived from your code) :

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();
    }
}

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

It's a pity that you did not say what kind of device you are using.

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

In case if your device is capable of RTS/CTS flow control it is also possbile to detect line break.

这篇关于使用 termios api (c++) 在 Linux 中检测字符设备是否已断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆