User avatar
jefferson_palheta
Posts: 10
Joined: Thu Apr 27, 2017 8:22 pm
Contact: Twitter

read() is blocked forever on raspberry pi 3 UART

Tue Aug 14, 2018 8:44 pm

The C read() function frequently get blocked, especially if there is nothing connected on the Gpio pins (tx / rx), but I just hoped it would stop for itself when there is no conection, the same when there is wire conection but no data to read, but it just get blocked until I force it to finish.

Open

Code: Select all

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

Read

Code: Select all

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

Set attribs

Code: Select all

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 */

    cfmakeraw(&options);

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

    return tcsetattr( fd, TCSANOW, &options );
}

User avatar
jefferson_palheta
Posts: 10
Joined: Thu Apr 27, 2017 8:22 pm
Contact: Twitter

Re: read() is blocked forever on raspberry pi 3 UART

Wed Aug 15, 2018 12:24 pm

I just add the code below, after open() and it's not blocked anymore when tx/rx pins are not connected. Probably the port was going blocked after open().

Code: Select all

if (fd == -1)
        {
          /*
           * Could not open the port.
           */
     
          perror("open_port: Unable to open /dev/serial0 - ");
        }
else fcntl(fd, F_SETFL, O_NONBLOCK);

scotty101
Posts: 3031
Joined: Fri Jun 08, 2012 6:03 pm

Re: read() is blocked forever on raspberry pi 3 UART

Wed Aug 15, 2018 12:46 pm

Why not try the serial port functions included in the WiringPi library. You may have more luck with that.

http://wiringpi.com/reference/serial-library/
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

wernermeurer
Posts: 6
Joined: Tue Sep 30, 2014 4:41 am

Re: read() is blocked forever on raspberry pi 3 UART

Thu Aug 16, 2018 6:23 am

Hi!

Your code does not check return-values of read() which are positive.
read() returns the number of charaters which are read, negative on errors and zero if no characters are there.
So I would check like:
n=read(....)

if (n<0) /* error */
{ printf("error\n"); }
else if (n==0) /* nothing read */
{ printf ("Nothing there\"); }
else /* normal read */
{ printf ("normal read\"); }

And of course - first check if the filedescriptor was opened without error!
Hope this helps....

Regards
Werner

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 9 guests