title: Read from serial port linux
body: <p>I'm trying to read from serial port, but always get 0 (zero) characters back. Already read the "Serial Programming Guide for POSIX Operating Systems", but can't find out why the program not waiting (blocking).
  The code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;   /* Standard input/output definitions */
  #include &lt;string.h&gt;  /* String function definitions */
  #include &lt;unistd.h&gt;  /* UNIX standard function definitions */
  #include &lt;fcntl.h&gt;   /* File control definitions */
  #include &lt;errno.h&gt;   /* Error number definitions */
  #include &lt;termios.h&gt; /* POSIX terminal control definitions */
  
  void main()
  {
      printf("Hello world\n");
  
      int fd; /* File descriptor for the port */
      int n;
      int bytes;
  
      char c;
  
      char buffer[10];
      char *bufptr;
  
      struct termios options;
  
      fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
  
      if (fd == -1) {
          perror("open_port: Unable to open /dev/ttyUSB0 - ");
      }
      else {
          fcntl(fd, F_SETFL, FNDELAY);
      }
  
    tcgetattr( fd, &amp;options );
  
    /* SEt Baud Rate */
  
    cfsetispeed( &amp;options, B9600 );
    cfsetospeed( &amp;options, B9600 );
  
    //I don't know what this is exactly
  
    options.c_cflag |= ( CLOCAL | CREAD );
  
    // Set the Charactor size
  
    options.c_cflag &amp;= ~CSIZE; /* Mask the character size bits */
    options.c_cflag |= CS8;    /* Select 8 data bits */
  
    // Set parity - No Parity (8N1)
  
    options.c_cflag &amp;= ~PARENB;
    options.c_cflag &amp;= ~CSTOPB;
    options.c_cflag &amp;= ~CSIZE;
    options.c_cflag |= CS8;
  
    // Disable Hardware flowcontrol
  
    //  options.c_cflag &amp;= ~CNEW_RTSCTS;  -- not supported
  
    // Enable Raw Input
  
    options.c_lflag &amp;= ~(ICANON | ECHO | ECHOE | ISIG);
  
    // Disable Software Flow control
  
    options.c_iflag &amp;= ~(IXON | IXOFF | IXANY);
  
    // Chose raw (not processed) output
  
    options.c_oflag &amp;= ~OPOST;
  
    if ( tcsetattr( fd, TCSANOW, &amp;options ) == -1 )
      printf ("Error with tcsetattr = %s\n", strerror ( errno ) );
    else
      printf ( "%s\n", "tcsetattr succeed" );
  
      fcntl(fd, F_SETFL, 0);
  
      // Write to the port
      n = write(fd, "1", 1);
  
      if (n &lt; 0) {
          fputs("write() of 1 bytes failed!\n", stderr);
      }
  
      // Read from the port
  
      //fcntl(fd, F_SETFL, FNDELAY);
  
      bytes = read(fd, &amp;buffer, sizeof(buffer));
      printf("number of bytes read is %d\n", bytes);
      printf("%s\n", buffer);
      //perror ("read error:");
  
      close(fd);
  }
  </code></pre>