title: Program hangs when closing serial port connection
body: <p>I am reading data through a USB connection as a serial port with the PL2303 driver. It returns successfully when doing an <code>open</code> and when I set they TTY options and non blocking. When I try to close the connection, it hangs. In this state it reads "�" instead of characters.</p>
  
  <p>I can connect to the device perfectly fine with cutecom. Here is the strange part: </p>
  
  <ol>
  <li>If I first connect to the device via cutecom (a serial monitor), my program will connect and close perfectly fine every time afterwards. It reads the characters as I expect them to be read. (No �).</li>
  <li>If I disconnect and reconnect the hardware, my program will hang again until I run cutecom.</li>
  </ol>
  
  <p>Since it works after I use cutecom, it makes me think that I am missing something in my initial connection, or connection settings. Here's what I use to connect:</p>
  
  <pre><code>baud_rate = 38400;
  fd =  open (device_path, O_RDONLY | O_NOCTTY );
  </code></pre>
  
  <p>In my <code>set_tty_options</code> function:</p>
  
  <pre><code>struct termios tty_options;
  
  memset (&amp;tty_options, 0, sizeof(tty_options));
  tcgetattr (fd, &amp;tty_options);
  
  cfsetispeed(&amp;tty_options, baud_rate);                         // set baud rate
  tty_options.c_cflag = (tty_options.c_cflag &amp; ~CSIZE) | CS8;   // 8 bit msgs
  tty_options.c_cflag |= (CLOCAL | CREAD);                      // enable reading
  
  tty_options.c_cflag &amp;= ~(PARENB | PARODD);                    // shut off parity
  tty_options.c_cflag |= parity;
  tty_options.c_cflag &amp;= ~CSTOPB;
  tty_options.c_cflag &amp;= ~CRTSCTS;
  
  if (tcsetattr (fd, TCSANOW, &amp;tty_options) != 0) 
  {
    printf("error %d from tcsetattr\n", errno);
    return TTY_ERROR;
  }
  </code></pre>
  
  <p>In <code>set_blocking</code> function:</p>
  
  <pre><code>if (tcgetattr (fd, &amp;tty) != 0)
  {
    printf("error %d from tggetattr", errno);
    return FAILURE;
  }
  
  // 0 or 1 byte is enough to return from read
  tty.c_cc[VMIN]  = should_block ? 1 : 0; 
  tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout
  
  if (tcsetattr (fd, TCSANOW, &amp;tty) != 0) 
  {
    printf("error %d setting term attributes", errno);
    return FAILURE;
  }
  </code></pre>