Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C read()在raspberry pi 3 UART上被永久阻止_C_Serial Port_Raspberry Pi3_Uart - Fatal编程技术网

C read()在raspberry pi 3 UART上被永久阻止

C read()在raspberry pi 3 UART上被永久阻止,c,serial-port,raspberry-pi3,uart,C,Serial Port,Raspberry Pi3,Uart,C read()函数经常会被阻塞,特别是当Gpio引脚(tx/rx)上没有连接时,但我只是希望它在没有连接时会自动停止,当有有线连接但没有数据要读取时也是如此,但它只是被阻塞,直到我强制它完成 打开 fd = open("/dev/serial0", O_RDWR | O_NDELAY | O_NOCTTY | O_NONBLOCK); 阅读 n = read( fd, value, 1 ); if (n < 0) { printf ( "Error = %s\n", strerror(

C read()函数经常会被阻塞,特别是当Gpio引脚(tx/rx)上没有连接时,但我只是希望它在没有连接时会自动停止,当有有线连接但没有数据要读取时也是如此,但它只是被阻塞,直到我强制它完成

打开

fd = open("/dev/serial0", O_RDWR | O_NDELAY | O_NOCTTY | O_NONBLOCK);
阅读

n = read( fd, value, 1 );
if (n < 0) {
printf ( "Error = %s\n", strerror( errno ) );
}
else if (n == 0) {
  printf ( "Read Nothing...\n");
}

对于只有tx/rx引脚的UART端口,没有明显的“未连接”状态。对于您想要的功能,端口将需要DCE/DTR引脚,并且必须从termios设置中删除
CLOCAL
标志(忽略调制解调器控制线)。

我只需在open()之后添加下面的代码,当tx/rx引脚未连接时,它不再被阻止。可能端口在open()之后被阻塞


当有连接时,它不会停止,但也没有要读取的数据。事实上,由于您使用O_NONBLOCK打开了它,所以如果没有要读取的数据,它应该返回-1并将errno设置为EAGAIN。。。
int setAttr(int fd)
{
    //Read the configureation of the port 
    struct termios options;
    tcgetattr( fd, &options );

    //Set Baud Rate 
    cfsetispeed( &options, B9600 );
    cfsetospeed( &options, B9600 );

    //Setting other Port Stuff 
    options.c_cflag &= ~PARENB; /*Make 8n1 */
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE; /* Mask the character size bits */
    options.c_cflag |= CS8;    /* Select 8 data bits */
    options.c_cflag &= ~CRTSCTS;  /* No flow control */
    options.c_cc[VMIN] = 0; /*READ doesn't block */
    options.c_cc[VTIME] = 1; /* 0.1 seconds read timout */
    options.c_cflag |= CREAD | CLOCAL; /* Turn on READ & ignore crtl lines */

    //Make raw
    cfmakeraw(&options);

    //Flush port, then applies attributes
    tcflush(fd, TCIOFLUSH);

    return tcsetattr( fd, TCSANOW, &options );
}
  if (fd == -1)
    {
      /*
       * Could not open the port.
       */

      perror("open_port: Unable to open /dev/serial0 - ");
    }
  else fcntl(fd, F_SETFL, O_NONBLOCK);