5

我正在使用 PL2303 驱动程序通过 USB 连接作为串行端口读取数据。open当我设置 TTY 选项和非阻塞时,它会成功返回。当我尝试关闭连接时,它挂起。在这种状态下,它读取“�”而不是字符。

我可以用cutecom完美地连接到设备。这是奇怪的部分:

  1. 如果我首先通过cutecom(串行监视器)连接到设备,我的程序每次都可以完美地连接和关闭。它读取字符,因为我希望它们被读取。(没有�)。
  2. 如果我断开并重新连接硬件,我的程序将再次挂起,直到我运行 cutecom。

由于它在我使用cutecom 后有效,这让我觉得我在初始连接或连接设置中遗漏了一些东西。这是我用来连接的:

baud_rate = 38400;
fd =  open (device_path, O_RDONLY | O_NOCTTY );

在我的set_tty_options功能中:

struct termios tty_options;

memset (&tty_options, 0, sizeof(tty_options));
tcgetattr (fd, &tty_options);

cfsetispeed(&tty_options, baud_rate);                         // set baud rate
tty_options.c_cflag = (tty_options.c_cflag & ~CSIZE) | CS8;   // 8 bit msgs
tty_options.c_cflag |= (CLOCAL | CREAD);                      // enable reading

tty_options.c_cflag &= ~(PARENB | PARODD);                    // shut off parity
tty_options.c_cflag |= parity;
tty_options.c_cflag &= ~CSTOPB;
tty_options.c_cflag &= ~CRTSCTS;

if (tcsetattr (fd, TCSANOW, &tty_options) != 0) 
{
  printf("error %d from tcsetattr\n", errno);
  return TTY_ERROR;
}

set_blocking功能上:

if (tcgetattr (fd, &tty) != 0)
{
  printf("error %d from tggetattr", errno);
  return FAILURE;
}

// 0 or 1 byte is enough to return from read
tty.c_cc[VMIN]  = should_block ? 1 : 0; 
tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

if (tcsetattr (fd, TCSANOW, &tty) != 0) 
{
  printf("error %d setting term attributes", errno);
  return FAILURE;
}
4

2 回答 2

1

我认为您想添加| O_SYNC到打开标志以坚持同步 i/o。我怀疑这会导致问题。

但是,我认为您想忽略中断信号,该信号被报告为 NUL 字符,就像您得到的那样:

tty_settings.c_iflag &= ~IGNBRK;         // ignore break signal

此外,您要确保输入处理完全关闭,以便接收退格、^C、^\ 等不会触发任何反应:

tty_settings.c_lflag = 0;                // no signaling chars, no echo,
                                         // no canonical processing

看起来您已经在使用我的 set_blocking() 函数,所以应该没问题。

于 2013-04-03T06:19:02.383 回答
0

这就是我最终做的事情。我基本上是通过从cutecom的源代码中复制和粘贴部分来解决这个问题的。

  1. 打开时...

    int fd, n;
    fd = open (device_path, O_RDONLY | O_NOCTTY | O_NDELAY);
    
    ... error check fd ...
    
    n = fcntl(ail_info->ail_serial_fd, F_GETFL, 0);
    fcntl(fd, F_SETFL, n & ~O_NDELAY);
    
  2. 你不能像我一样设置波特率。您必须使用定义的B38400;

    baud = B38400;

  3. 然后,我添加了 wallyk 的答案。

    tty_settings.c_lflag = 0;

编辑:根据锯末评论,我找到了一种将其设置为原始输入的更好方法。

tty_options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

它有效。

于 2013-04-03T19:21:28.623 回答