title: serial port read/write returns too early
body: <pre><code>I have written a small and simple serial port read/write application to connect to my modem and to run commands. There is a problem. The program works perfectly for the most part but when the command takes a bit of time to execute and respond, the program acts strangely. Here is my code:
  
  void write (char cmd[256]) {
      int n_written = 0;
      char atcmd [259];
      sprintf (atcmd, "%s\r\n", cmd);
  
      n_written = write( fd, &amp;atcmd, strlen(atcmd) );
      printf ("WROTE: %d\n", n_written);
  }
  
  void read (char * cmdresp) {
      int n = 0;
      char response[1024];
      memset(response, 0 , sizeof response);
  
      n = read( fd, &amp;response, sizeof (response) );
      if (n &lt; 0) {
          printf ("ERROR=%d - %s\n", errno, strerror(errno));
      } else if (n == 0) {
          printf ("Read nothing!\n");
      } else {
          printf ("READ: %s\n", response);
          strcpy (cmdresp, response);
      }
  }
  
  void manage_transaction (char * message, char * response) {
      tv.tv_sec = 5;
      tv.tv_usec = 0;
  
      write (message);
  
      FD_ZERO(&amp;rfds);
      FD_SET(fd, &amp;rfds);
      int retval = select(fd+1, &amp;rfds, NULL, NULL, &amp;tv);
      if (retval == -1) {
      perror("select()");
      } else if (retval) {
      if (FD_ISSET(fd, &amp;rfds) ) {
          usleep(200000);
          read(response);
      }
      } else {
          printf("No data within five seconds.\n");
      }
  }
  </code></pre>
  
  <p>Here is two successive runs, one successful one unsuccessful:</p>
  
  <pre><code>Successful:
  AT&gt; AT+CGSN
  Client ConnectedHere is the message: AT+CGSN
  
  WROTE: 11
  READ: AT+CGSN
  123456789012345
  
  OK
  
  AT+CGSN
  123456789012345
  
  OK
  
  FAILED:
  AT&gt; ATD11234567890;
  Here is the message: ATD11234567890;
  
  WROTE: 19
  READ: ATD11234567890;
  AT&gt; 1234567890;
  </code></pre>
  
  <p>&lt;&lt;-- If I press an enter key or send another message the rest of the message comes through: </p>
  
  <pre><code>Here is the message:
  
  WROTE: 4
  READ:
  OK
  
  
  OK
  AT&gt;
  </code></pre>
  
  <p>Am I missing something? How can I fix this problem?</p>
  
  <p>Any ideas is greatly appreciated.</p>
  
  <p>*********** EDIT ***************
  Here are the open and config codes:</p>
  
  <pre><code>int open_port(void)
  {
      fd = open("/dev/ttyUSB2", O_RDWR | O_NOCTTY);
  
      if(fd == -1) {
          perror("open_port: Unable to open /dev/ttyUSB2");
      }
      else {
          fcntl(fd, F_SETFL, 0);
          printf("port is open.\n");
      }
      tcflush(fd, TCIFLUSH);
      tcflush(fd, TCOFLUSH);
  
      return fd;
  }
  
  void config () {
  struct termios tty;
  struct termios tty_old;
  memset (&amp;tty, 0, sizeof tty);
  if ( tcgetattr ( fd, &amp;tty ) != 0 ) {
      printf ("ERROR=%d - %s\n", errno, strerror(errno));
  }
  
  tty_old = tty;
  
  /* Set Baud Rate */
  cfsetospeed (&amp;tty, (speed_t)B115200);
  cfsetispeed (&amp;tty, (speed_t)B115200);
  
  tty.c_cflag     &amp;=  ~PARENB;            // Make 8n1
  tty.c_cflag     &amp;=  ~CSTOPB;
  tty.c_cflag     &amp;=  ~CSIZE;
  tty.c_cflag     |=  CS8;
  tty.c_iflag     &amp;= ~INPCK;
  tty.c_iflag     |=  (IGNBRK | BRKINT); 
  
  tty.c_iflag     |= IGNPAR;
  
  tty.c_iflag &amp;= ~ICRNL;
  
  tty.c_iflag &amp;= ~IXON;
  tty.c_iflag &amp;= ~ISTRIP;
  
  tty.c_iflag &amp;= ~INLCR;
  tty.c_iflag &amp;= ~IGNCR;
  tty.c_cflag     &amp;= ~CRTSCTS;            // no flow control
  tty.c_cc[VMIN]   =  0;                  // read doesn't block
  tty.c_cc[VTIME]  =  0;                  // 5 seconds read timeout
  tty.c_cflag     |=  CREAD | CLOCAL;     // turn on READ &amp; ignore ctrl lines
  tty.c_iflag     &amp;= ~(IXON |IXOFF|IXANY); /* no XON/XOFF flow control */
  tty.c_oflag     &amp;= ~(IXON |IXOFF|IXANY); /* no XON/XOFF flow control */
  tty.c_oflag     &amp;= ~(OPOST | ONLCR );
  /* Make raw */
  cfmakeraw(&amp;tty);
  
  /* Flush Port, then applies attributes */
  tcflush(fd, TCIFLUSH );
  tcflush(fd, TCIOFLUSH );
  
  int reuse = 1;
  setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&amp;reuse, sizeof(reuse));
  
  if ( (tcsetattr(fd,TCSANOW,&amp;tty) &lt; 0) ) {
      printf ("ERROR=%d - %s\n", errno, strerror(errno));
  }
  }
  </code></pre>