1

我有一个通过 termios 串行 API 使用的 FTDI USB 串行设备。我设置了端口,以便它在半秒内调用 read() 超时(通过使用 VTIME 参数),这在 Linux 和 FreeBSD 上都有效。然而,在 OpenBSD 5.1 上,当没有数据可用时, read() 调用会永远阻塞(见下文。)我希望 read() 在 500 毫秒后返回 0。

谁能想到 termios API 在 OpenBSD 下的行为会有所不同的原因,至少在超时功能方面?

编辑:无超时问题是由链接到 pthread 引起的。无论我是否实际使用任何 pthread、互斥锁等,简单地链接到该库都会导致 read() 永远阻塞,而不是根据 VTIME 设置超时。同样,这个问题只在 OpenBSD 上出现——Linux 和 FreeBSD 按预期工作。

if ((sd = open(devPath, O_RDWR | O_NOCTTY)) >= 0)
{
  struct termios newtio;
  char input;

  memset(&newtio, 0, sizeof(newtio));

  // set options, including non-canonical mode
  newtio.c_cflag = (CREAD | CS8 | CLOCAL);
  newtio.c_lflag = 0;

  // when waiting for responses, wait until we haven't received
  // any characters for 0.5 seconds before timing out
  newtio.c_cc[VTIME] = 5;
  newtio.c_cc[VMIN] = 0;

  // set the input and output baud rates to 7812
  cfsetispeed(&newtio, 7812);
  cfsetospeed(&newtio, 7812);

  if ((tcflush(sd, TCIFLUSH) == 0) &&
      (tcsetattr(sd, TCSANOW, &newtio) == 0))
  {
    read(sd, &input, 1); // even though VTIME is set on the device,
                         // this read() will block forever when no
                         // character is available in the Rx buffer
  }
}
4

1 回答 1

1

来自 termios 手册页:

 Another dependency is whether the O_NONBLOCK flag is set by open() or
 fcntl().  If the O_NONBLOCK flag is clear, then the read request is
 blocked until data is available or a signal has been received.  If the
 O_NONBLOCK flag is set, then the read request is completed, without
 blocking, in one of three ways:

       1.   If there is enough data available to satisfy the entire
            request, and the read completes successfully the number of
            bytes read is returned.

       2.   If there is not enough data available to satisfy the entire
            request, and the read completes successfully, having read as
            much data as possible, the number of bytes read is returned.

       3.   If there is no data available, the read returns -1, with errno
            set to EAGAIN.

你能检查一下是不是这样吗?干杯。

编辑:OP 将问题追溯到导致读取功能阻塞的 pthread 链接。通过升级到 OpenBSD >5.2,这个问题通过将新的 rthreads 实现更改为 openbsd 上的默认线程库而得到解决。有关guenther@ EuroBSD2012 幻灯片的更多信息

于 2013-04-12T13:16:01.810 回答