title: serial programming on linux in C
body: <p>I want to send a certain string on my serial port and read the answer into a buffer for further analysis. I have come up with some code but am not able to read any answer even tho screen <strong>/dev/ttyUSB0 19200</strong> on the shell works just fine for me. The decive expects 8 data bits, 1 start bit, 1 stop bit, and no parity.  at 19200 baud. 
  Now my code looks like this and it keeps timning out: :(</p>
  
  <pre><code>/////////////////////////////////////////////////
  // Serial port interface program               //
  /////////////////////////////////////////////////
  #include &lt;stdio.h&gt; // standard input / output functions
  #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 definitionss
  #include &lt;time.h&gt;   // time calls
  
  
  int open_port(void)
  {
  int fd; // file description for the serial port
  
  fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
  
  if(fd == -1) // if open is unsucessful
  {
  perror("open_port: Unable to open /dev/ttyUSB0");
  }
  else
  {
  fcntl(fd, F_SETFL, 0);
  }
  
  return(fd);
  }
  
  int configure_port(int fd)      // configure the port
  {
  struct termios port_settings;      // structure to store the port settings in
  
  cfsetispeed(&amp;port_settings, B19200);    // set baud rates
  cfsetospeed(&amp;port_settings, B19200);
  
  port_settings.c_cflag &amp;= ~PARENB;    // set no parity, stop bits, data bits
  port_settings.c_cflag &amp;= ~CSTOPB;
  port_settings.c_cflag &amp;= ~CSIZE;
  port_settings.c_cflag |= CS8;
  
  cfmakeraw(&amp;port_settings);
  tcsetattr(fd, TCSANOW, &amp;port_settings);    // apply the settings to the port
  return(fd);
  
  }
  
  int query_modem(int fd)   // query modem with an AT command
  {
  int n;
  fd_set rdfs;
  struct timeval timeout;
  ssize_t retval;
  char bufptr[100];
  char chr;
  int cnt = 0;
  int i = 0;
  
  // initialise the timeout structure
  timeout.tv_sec = 2; // ten second timeout
  timeout.tv_usec = 0;
  
  if (FD_ISSET(fd, &amp;rdfs)){
    FD_ZERO(&amp;rdfs);
    FD_CLR(fd,&amp;rdfs);
  }
  
  retval = write(fd, "TEST\r", 5);  // send an AT command followed by a CR
  /*usleep(50);
  while (read(fd, &amp;chr, 1))
  {
  printf("0x%x\n",chr);
  usleep(10);
  }*/
  
  // do the select
  n = select(fd + 1, &amp;rdfs, NULL, NULL, &amp;timeout);
  
  // check if an error has occured
  if(n &lt; 0)
  {
  perror("select failed\n");
  }
  else if (n == 0)
  {
  printf("Timeout\n");
  }
  else
  {
  printf("\nBytes detected on the port!\n");
  }
  
  }
  
  int main(void)
  {
  int fd = open_port();
  configure_port(fd);
  query_modem(fd);
  return(0);
  }
  </code></pre>
  
  <p>What I would expect back is a string saying 
  "TEST" the same as in screen upon I hit enter.
  Any help would be appreciated!
  Thank you very much!</p>
  
  <p>Ron</p>