关闭串口连接时程序挂起

Nat*_*ate 5 c linux serial-port

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

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

  1. 如果我首先通过cutecom(串行监视器)连接到设备,我的程序每次都会连接并完全关闭.它会读取我希望它们被读取的字符.(不 ).
  2. 如果我断开并重新连接硬件,我的程序将再次挂起,直到我运行cutecom.

因为它在我使用cutecom后工作,它让我觉得我在初始连接或连接设置中遗漏了一些东西.这是我用来连接的内容:

baud_rate = 38400;
fd =  open (device_path, O_RDONLY | O_NOCTTY );
Run Code Online (Sandbox Code Playgroud)

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

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

Nat*_*ate 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);
    
    Run Code Online (Sandbox Code Playgroud)
  2. 你不能像我一样设置波特率。您必须使用定义的B38400;

    baud = B38400;

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

    tty_settings.c_lflag = 0;

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

tty_options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
Run Code Online (Sandbox Code Playgroud)

它有效。