Blocking and non-blocking operation of Linux under serial port

Source: Internet
Author: User

There are two ports that can be controlled for serial blocking (simultaneous control of Read and write): One is the opening of the serial port, whether the open function with O_ndelay, the second one can be opened after the serial port through the FCNTL () function to control.

Definition of blocking:

For Read,block refers to when the serial input buffer does not have data, the read function will be blocked here, ported to the serial input buffer data can be read, read to the required number of bytes, the return value is read to the number of bytes;

For Write,block when the serial 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 greater than or equal to the number of bytes to be written, and the write operation returns the number of bytes written.

Non-blocking definitions:

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

For Write,no block, when the serial output buffer is full, or the remaining space is less than the number of bytes to be written, write writes the number of bytes allowed in the remaining space of the current serial output buffer, and then returns the number of bytes written.

[CPP]View Plaincopy
  1. static int set_opt (int fd, int nspeed, int nBits, char nevent, int nstop)
  2. {
  3. struct Termios newtio;
  4. struct Termios oldtio;
  5. if (tcgetattr (fd,&oldtio)! = 0)
  6. {
  7. Perror ("Setupserial 1");
  8. return-1;
  9. }
  10. Bzero (&newtio,sizeof (newtio));
  11. Newtio.c_cflag |= clocal | Cread;
  12. Newtio.c_cflag &= ~csize;
  13. /*********** Data bit selection ****************/
  14. switch (nBits)
  15. {
  16. Case 7:
  17. Newtio.c_cflag |= CS7;
  18. Break ;
  19. Case 8:
  20. Newtio.c_cflag |= CS8;
  21. Break ;
  22. }
  23. /*********** Check bit selection ****************/
  24. switch (nevent)
  25. {
  26. case ' O ':
  27. Newtio.c_cflag |= Parenb;
  28. Newtio.c_cflag |= parodd;
  29. Newtio.c_iflag |= (INPCK | Istrip);
  30. Break ;
  31. case ' E ':
  32. Newtio.c_iflag |= (INPCK | Istrip);
  33. Newtio.c_cflag |= Parenb;
  34. Newtio.c_cflag &= ~parodd;
  35. Break ;
  36. case ' N ':
  37. Newtio.c_cflag &= ~parenb;
  38. Break ;
  39. }
  40. /*********** Baud Rate Selection ****************/
  41. switch (nspeed)
  42. {
  43. Case 2400:
  44. Cfsetispeed (&newtio,b2400);
  45. Cfsetospeed (&newtio,b2400);
  46. Break ;
  47. Case 4800:
  48. Cfsetispeed (&newtio,b4800);
  49. Cfsetospeed (&newtio,b4800);
  50. Break ;
  51. Case 9600:
  52. Cfsetispeed (&newtio,b9600);
  53. Cfsetospeed (&newtio,b9600);
  54. Break ;
  55. Case 57600:
  56. Cfsetispeed (&newtio,b57600);
  57. Cfsetospeed (&newtio,b57600);
  58. Break ;
  59. Case 115200:
  60. Cfsetispeed (&newtio,b115200);
  61. Cfsetospeed (&newtio,b115200);
  62. Break ;
  63. Case 460800:
  64. Cfsetispeed (&newtio,b460800);
  65. Cfsetospeed (&newtio,b460800);
  66. Break ;
  67. Default:
  68. Cfsetispeed (&newtio,b9600);
  69. Cfsetospeed (&newtio,b9600);
  70. Break ;
  71. }
  72. /*********** Stop bit selection ****************/
  73. if (nstop = = 1) {
  74. Newtio.c_cflag &= ~CSTOPB;
  75. }
  76. Else if (nstop ==2) {
  77. Newtio.c_cflag |= CSTOPB;
  78. }
  79. Newtio.c_cc[vtime] = 1;
  80. Newtio.c_cc[vmin] = frame_maxsize; //Effective under blocking conditions
  81. Tcflush (Fd,tciflush);
  82. if ((Tcsetattr (fd,tcsanow,&newtio))! = 0)
  83. {
  84. perror ("com set error");
  85. return-1;
  86. }
  87. printf ("set done!\n");
  88. return 0;
  89. }

[CPP]View Plaincopy
  1. static int open_port (int fd,int comport)
  2. {
  3. /*********** Open Serial 1****************/
  4. if (comport = = 1)
  5. {
  6. FD = open ("/DEV/TTYAT1", o_rdwr| o_noctty|     O_ndelay);
  7. if (fd = =-1) {
  8. Perror ("Can ' t Open Serial Port");
  9. return-1;
  10. }
  11. }
  12. /*********** Open Serial 2****************/
  13. Else if (comport = = 2)
  14. {
  15. FD = open ("/dev/ttyat2", o_rdwr| o_noctty|     O_ndelay);
  16. if (fd = =-1) {
  17. Perror ("Can ' t Open Serial Port");
  18. return-1;
  19. }
  20. }
  21. /*********** Open Serial 3****************/
  22. Else if (comport = = 3)
  23. {
  24. FD = open ("/DEV/TTYAT3", o_rdwr| o_noctty|     O_ndelay);
  25. if (fd = =-1) {
  26. Perror ("Can ' t Open Serial Port");
  27. return-1;
  28. }
  29. }
  30. if (comport = = 1)
  31. {
  32. if (fcntl (Fd,f_setfl,fndelay) < 0)//non-blocking, overwrite the properties of the previous open
  33. {
  34. printf ("Fcntl failed\n");
  35. }
  36. else{
  37. printf ("fcntl=%d\n", Fcntl (Fd,f_setfl,fndelay));
  38. }
  39. }
  40. Else
  41. {
  42. if (fcntl (fd,f_setfl,0) < 0) { //block, even if the front of the open serial device is set to be non-blocking, this is set to block, whichever is
  43. printf ("Fcntl failed\n");
  44. }
  45. else{
  46. printf ("fcntl=%d\n", Fcntl (fd,f_setfl,0));
  47. }
  48. }
  49. if (isatty (stdin_fileno) = = 0) {
  50. printf ("standard input was not a terminal device\n");
  51. }
  52. else{
  53. printf ("Isatty sucess!\n");
  54. }
  55. printf ("fd-open=%d\n", FD);
  56. return FD;
  57. }

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

[CPP]View Plaincopy
    1. Blocking: Fcntl (fd,f_setfl,0)
[CPP]View Plaincopy
      1. Non-blocking: Fcntl (Fd,f_setfl,fndelay)

Blocking and non-blocking operation of Linux under serial port

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.