Block and non-blocking operation of serial port under Linux __linux

Source: Internet
Author: User
There are two to control the serial port blocking (while controlling read and write): One is when opening the serial port, the Open function is O_ndelay, and the second one can be controlled by the Fcntl () function after opening the serial port.

Blocking definition:

For Read,block, when there is no data in the serial port input buffer, the Read function will block here, the data can be read by porting to the serial input buffer, and after read the number of bytes needed, the returned value is the number of bytes read;

For Write,block means that when the serial port output buffer is full, or the remaining space is less than the number of bytes to be written, write will block until the remaining space in the serial output buffer is equal to the number of bytes to be written, and the number of bytes written is returned by performing a write operation.

Non-blocking definition:

For Read,no block, the Read function returns immediately when there is no data in the serial port input buffer, and the return value is 0.

For Write,no block, when the serial port output buffer is full, or the remaining space is less than the number of bytes to be written, write writes, writes the number of bytes allowed in the remaining space of the current serial output buffer, and returns the number of bytes written.[CPP] View Plain copy static int set_opt (Int fd, int nspeed, int nbits, char  nevent, int nstop)       {           struct termios newtio;          struct  termios oldtio;                     if (Tcgetattr (fd,&oldtio)  != 0)            {              perror ("setupserial  1 ");              return -1;           }                     bzero (&newtio,sizeof (newtio));          newtio.c_cflag |= clocal | cread;          newtio.c_cflag &= ~CSIZE;             /*********** Data bit selection ****************/            switch (nbits)            {              case 7:               newtio.c_cflag |= CS7;               break;               case 8:               newtio.c_cflag |= CS8;               break;              }     /*********** Check bit selection ****************/          switch (nevent)            {              case  ' O ':               newtio.c_cflag |= PARENB;               newtio.c_cflag |=  parodd;              newtio.c_iflag |=   (Inpck | istrip);                   break;               case  ' E ':              newtio.c_ iflag |=  (inpck&Nbsp;|istrip);              newtio.c_cflag  |= PARENB;              newtio.c_ cflag &= ~parodd;                   break;                case  ' N ':              newtio.c_ cflag &= ~parenb;                   break;          }      /*********** baud rate selection ****************/          switch (nSpeed)           {               case 2400:               Cfsetispeed (&newtio,b2400);               Cfsetospeed (&newtio,b2400);                   break;                case 4800:              cfsetispeed (&newtio,b4800);              cfsetospeed ( &newtio,b4800);                   break;               case  9600:              cfsetispeed (& newtio,b9600);               cfsetospeed (&newtio,B9600);                   break;              case 57600:               cfsetispeed (&newtio,b57600);               cfsetospeed (&newtio,b57600);                   break;                case 115200:              cfsetispeed (&newtio,b115200);               cfsetospeed (&newtio,b115200);                   break;                case 460800:               cfsetispeed (&newtio,b460800);               cfsetospeed (&newtio,b460800);                   break;                           default:              cfsetispeed (&newtio, B9600);              cfsetospeed (&newtio, B9600);                       break;          }     /*********** Stop bit selection * * /         if (nstop == 1) {               newtio.c_cflag &= ~CSTOPB;           }          else  if (nstop ==2) {               newtio.c_cflag |= cstopb;          }           newtio.c_cc[VTIME] = 1;        Effective      under    newtio.c_cc[vmin] = frame_maxsize;   //blocking conditions         tcflush (fd,tciflush);           if (Tcsetattr (fd,tCsanow,&newtio)  != 0)           {               perror ("Com set error");               return -1;           }          printf ("set done!\n" );          return 0;     }      
[CPP] View Plain copy static int open_port (int fd,int comport)       {        /*********** Open serial 1****************/       if ( comport == 1)           {              fd = open ("/dev/ttyat1", O_RDWR| o_noctty| O_ndelay);          if (fd == -1) {               perror ("Can ' T open serial port");               return -1;               }           }       /*********** Open Serial 2****************/         else&nbSp;if (comport == 2)           {               fd = open ("/dev/ttyat2", O_RDWR| o_noctty| O_ndelay);              if (fd == -1) {                  perror ( "Can ' T open serial port");                   return -1;               }          }      /*********** Open serial 3****************/         else if (comport ==  3)           {            &nBsp;  fd = open ("/dev/ttyat3", o_rdwr| o_noctty| O_ndelay);              if (fd == -1) {                  perror ( "Can ' T open serial port");                   return -1;               }          }          if (comport == 1)        {            if (Fcntl (fd,f_setfl,fndelay)  < 0)//Non-blocking, overriding the properties of the front open            {                   printf ("Fcntl failed\n ");              }               else{              printf ("fcntl=%d\n", Fcntl (fd,f_setfl,fndelay));               }        }        else       {            if (Fcntl (fd,f_setfl,0)  < 0) {   //blocked, Even if the previous set on the open serial device is non-blocking, this is set to block, this is the Quasi            printf ("Fcntl  failed\n ");              }               else{               printf ("fcntl=%d\n", Fcntl (fd,f_setfl,0));               }        }           if (Isatty (Stdin_fileno)  == 0) {                  printf ("standard input is not a terminal device\n" );          }           else{                          printf ("isatty sucess!\n");           }            printf ("fd-open=%d\n", FD);           return fd;     }   

So, the blocking of Linux serial port is set by Fcntl () function.

[CPP] view plain copy blocking: Fcntl (fd,f_setfl,0) [CPP] view plain copy non-blocking: Fcntl (Fd,f_setfl,fndelay)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.