title: Can't receive bytes over serial port but can transfer bytes
body: <p>I have a simple application that runs on an embedded device running Linux that sends and receives bytes over UART. I have a minicom session open in my host machine to which the RS-232 USB cable is connected.</p>
  <p>So far, I am only able to send data from the app to the serial session but not the other way around. This should indicate my UART configs including the baud rate are fine since I can send the data at least, and the serial session is also running at the same baud rate.</p>
  <p><code>select()</code> returns 0 which indicates the call timed out. Is it implying the socket is busy at the moment and can't be used?</p>
  <p>Opening a socket</p>
  <pre><code>#DEVICE_PORT      &quot;/dev/ttyHSL0&quot;
  
  static int buildFlowControl(Enum_FlowCtrl fc, struct termios* term)
  {
      if (NO_FLOW_CTRL == fc)
      {
          term-&gt;c_cflag |= CRTSCTS;           
          term-&gt;c_iflag &amp;= ~(IXON | IXOFF);
      }
      else if (FC_RTSCTS == fc)
      {
          term-&gt;c_cflag &amp;= ~CRTSCTS;
          term-&gt;c_iflag |= (IXON | IXOFF);
      }
      else if (FC_XONXOFF == fc)
      {
          term-&gt;c_cflag &amp;= ~CRTSCTS;
          term-&gt;c_iflag &amp;= ~(IXON | IXOFF);
      }else{
          return -1;
      }
      
      return 0;
  }
  int UART_Open(const char* port, unsigned int baudrate, Enum_FlowCtrl flowCtrl)
  {
      int fd;
      struct termios term;
      
      fd = open(port, O_RDWR);
      
      bzero(&amp;term, sizeof(term));
      cfmakeraw(&amp;term);
      term.c_cflag |= CREAD;
      tcgetattr(fd, &amp;term);
      
      buildBaudrate(baudrate, &amp;term);
      buildDataBit(8, &amp;term);
      buildStopBit(1, &amp;term);
      buildParity(PB_NONE, &amp;term);
      buildFlowControl(flowCtrl, &amp;term);
  
      term.c_iflag &amp;= ~ICRNL;
      term.c_iflag &amp;= ~INLCR;
      term.c_iflag |= IGNBRK;
  
      term.c_oflag &amp;= ~OCRNL;
      term.c_oflag &amp;= ~ONLCR;
      term.c_oflag &amp;= ~OPOST;
  
      term.c_lflag &amp;= ~ICANON;
      term.c_lflag &amp;= ~ISIG;
      term.c_lflag &amp;= ~IEXTEN;
      term.c_lflag &amp;= ~(ECHO|ECHOE|ECHOK|ECHONL|ECHOCTL|ECHOPRT|ECHOKE);
  
      term.c_cc[VMIN] = 5;    // minimum received bytes before unblocking
      term.c_cc[VTIME] = 100; // 10 SECONDS timeout
  
      tcsetattr(fd, TCSANOW, &amp;term);
      tcflush(fd, TCIOFLUSH);
  
      return fd;
  }
  </code></pre>
  <p>Main</p>
  <pre><code>int socketFileDescriptor;
  
  int main(void argc, char *argv[])
  {
     int baudRate = atoi(argv[1]);
     socketFileDescriptor = UART_Open(DEVICE_PORT, baudRate, NO_FLOW_CTRL);   
  
     if (pthread_create(&amp;threadUartRcv, NULL, UartRcv, NULL) != 0)
     { 
        // error handling
     }  ​
     while(true);  
  }
  
  </code></pre>
  <p>UART receive function:</p>
  <pre><code>static void* UartRcv(void* arg)
  {
      int ret;
      fd_set fdset;
      struct timeval timeout = {5, 0};    
      char buffer[100] = {0};
  
      FD_ZERO(&amp;fdset); 
      FD_SET(socketFileDescriptor, &amp;fdset); 
      while(true)
      {
          ret = select(socketFileDescriptor + 1, &amp;fdset, NULL, NULL, &amp;timeout); // waits for 5 seconds for any activity on socket
  
          // reset the time value back to the original since it's reset by select()
          timeout.tv_sec = 5;
          
          if (ret == -1)
          {
              printf(&quot;&lt; failed to select &gt;\n&quot;);
              exit(-1);
          }
          else if (ret == 0)
          {
              printf(&quot;&lt; no data &gt;\n&quot;);
          }
          else
          {
              if (FD_ISSET(socketFileDescriptor, &amp;fdset)) 
              {
                  do {
                      memset(buffer, 0x0, sizeof(buffer));
                      ret = read(socketFileDescriptor, buffer, 100);
                      sleep(3);
                  } while (true);
              }
          }
      }
  }
  </code></pre>