title: C: read() hangs without getting input from serial port on unix
body: <p>I'm having some issues getting data from a serial port using C on my Ubuntu 12 system.</p>
  
  <p>I'm using open() and read(), and here is my code:</p>
  
  <pre><code>Fd = open("/dev/ttyUSB0", O_RDONLY | O_NOCTTY);
  if (Fd == -1) {
      printf("Could not open serial port: %s\n", strerror(errno));
      return 1;
  }
  
  fcntl(Fd, F_SETFL, 0);
  
  char buf;
  while (1) {
      read(Fd, &amp;buf, 1);
      printf("%c", buf);
  }
  </code></pre>
  
  <p>However - my serial device is set to send "Boot.\r\n" followed by "To send: ", but when I attach the device and start the program, I only get the first line ("Boot.") and then no more. If I start gtkterm/picocom, I get both lines straight away.</p>
  
  <p>I've also tried adding a signal handler for SIGTERM to get the port closed properly, using:</p>
  
  <pre><code>void signal_callback_handler(int signum) {
      printf("Caught SIGTERM\n");
      close(Fd);
      exit(signum);
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>signal(SIGINT, signal_callback_handler);
  </code></pre>
  
  <p>Using this, I get the following when I press CTRL-C:</p>
  
  <pre><code>Boot.
  ^CTo send: Caught SIGTERM
  </code></pre>
  
  <p>I've also tried setting up the port first, using:</p>
  
  <pre><code>struct termios port_settings;          // structure to store the port settings in
  cfsetispeed(&amp;port_settings, B115200);  // set baud rates
  cfsetospeed(&amp;port_settings, B115200);
  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;
  tcsetattr(Fd, TCSANOW, &amp;port_settings);// apply the settings to the port
  </code></pre>
  
  <p>This only makes the situation worse - I get spammed with � :(</p>
  
  <p>I'd be very appreciative of any help, thanks in advance!</p>