Linux Reading Data from UART
<p>I want to read data from UART, i followed <a href="http://xanthium.in/Serial-Port-Programming-on-Linux" rel="nofollow noreferrer">this tutorial</a>, the write function works as expected, however i'am getting problem with the read function :</p>
<p>This is the uart_init function:</p>
<pre><code>void uart_init()
{
 printf(&quot;\n +----------------------------------+&quot;);
 printf(&quot;\n |        Serial Port Write         |&quot;);
 printf(&quot;\n +----------------------------------+&quot;);

/*------------------------------- Opening the Serial Port -------------------------------*/

  fd = open(&quot;/dev/ttyUSB0&quot;,O_RDWR | O_NOCTTY| O_SYNC);      /* !!blocks the read  */
                                                            /* O_RDWR Read/Write access to serial port           */
                                                            /* O_NOCTTY - No terminal will control the process   */
                                                            /* O_NDELAY -Non Blocking Mode,Does not care about-  */
                                                            /* -the status of DCD line,Open() returns immediatly */                                        
                                
 if(fd == -1)                                               /* Error Checking */
  printf(&quot;\n  Error! in Opening ttyUSB0  &quot;);
 else
  printf(&quot;\n  ttyUSB0 Opened Successfully &quot;);


 /*---------- Setting the Attributes of the serial port using termios structure --------- */
    
struct termios SerialPortSettings;          /* Create the structure                          */

tcgetattr(fd, &amp;SerialPortSettings);         /* Get the current attributes of the Serial port */

cfsetispeed(&amp;SerialPortSettings,B19200);        /* Set Read  Speed as 19200                       */
cfsetospeed(&amp;SerialPortSettings,B19200);        /* Set Write Speed as 19200                       */

SerialPortSettings.c_cflag &amp;= ~PARENB;          /* Disables the Parity   Enable bit(PARENB),So No Parity   */
SerialPortSettings.c_cflag &amp;= ~CSTOPB;          /* CSTOPB = 2 Stop bits,here it is cleared so 1 Stop bit */
SerialPortSettings.c_cflag &amp;= ~CSIZE;           /* Clears the mask for setting the data size             */
SerialPortSettings.c_cflag |=  CS8;             /* Set the data bits = 8                                 */

SerialPortSettings.c_cflag &amp;= ~CRTSCTS;         /* No Hardware flow Control                         */
SerialPortSettings.c_cflag |= CREAD | CLOCAL;   /* Enable receiver,Ignore Modem Control lines       */ 
    
    
SerialPortSettings.c_iflag &amp;= ~(IXON | IXOFF | IXANY);          /* Disable XON/XOFF flow control both i/p and o/p */
SerialPortSettings.c_iflag &amp;= ~(ICANON | ECHO | ECHOE | ISIG);  /* Non Cannonical mode                            */

SerialPortSettings.c_oflag &amp;= ~OPOST;/*No Output Processing*/

/* Setting Time outs */
SerialPortSettings.c_cc[VMIN] = 10; /* Read at least 10 characters */
SerialPortSettings.c_cc[VTIME] = 0; /* Wait indefinetly   */

if((tcsetattr(fd,TCSANOW,&amp;SerialPortSettings)) != 0) /* Set the attributes to the termios structure*/
 printf(&quot;\n  ERROR ! in Setting attributes&quot;);
else
 printf(&quot;\n  BaudRate = 19200 \n  StopBits = 1 \n  Parity   = none&quot;);
        
}
</code></pre>
<p>the receive function :</p>
<pre><code>void uart_receive()
{
 char read_buffer[32];   /* Buffer to store the data received              */
 int  bytes_read = 0;    /* Number of bytes read by the read() system call */
 int i = 0;

 bytes_read = read(fd,&amp;read_buffer,10); /* Read the data                   */
        
 printf(&quot;\n\n  Bytes Rxed %d&quot;, bytes_read); /* Print the number of bytes read */
 printf(&quot;\n\n  &quot;);

 for(i=0;i&lt;bytes_read;i++)   /*printing only the received characters*/
 printf(&quot;%c&quot;,read_buffer[i]);

 printf(&quot;\n +----------------------------------+\n\n\n&quot;);
}
</code></pre>
<p>the main function :</p>
<pre><code>void main(void)
{ 
  uart_init();
  /*------------------------------- Write data to serial port -----------------------------*/
  //uart_write_commande(write_buffer); //Write function works well
  uart_receive();

  close(fd);/* Close the Serial port */
}
</code></pre>
<p>I execute the program and wait for data bytes to be received in UART, i send data using UART but the read function keeps blocked.</p>
<p>I'am using a Virtual machine with Ubunutu 14.04 on it, and i'am not sure that using an emulated UART can cause problems during reception.</p>