title: Receiving information from serial port in while loop
body: <p>I'm reading information from a serial port using this code:</p>
  
  <pre><code>struct termios tio;
  memset(&amp;tio, 0, sizeof(tio));
  
  // Open serial port in mode `8N1', non-blocking
  tio.c_cflag = CS8 | CREAD | CLOCAL;
  tio.c_cc[VMIN] = 1;
  tio.c_cc[VTIME] = 10;
  
  int fd = open("/dev/ttyACM1", O_RDONLY);
  
  cfsetospeed(&amp;tio, B9600);
  cfsetispeed(&amp;tio, B9600);
  tcsetattr(fd, TCSANOW, &amp;tio);
  unsigned char byte = '0';
  
  // check for input from arduino
  while (!quit)
  {
      keyboardInput(quit);
      read(fd, &amp;byte, 1);
  
      if ((byte == '1' || quit)
      {
          oldByteDoor = '1';
          break;
      }
  }
  </code></pre>
  
  <p>where <code>keyboardInput(quit)</code> sets quit to true when the close button of the window is pressed.</p>
  
  <p>If nothing is in the serial port it gets stuck at <code>read(fd, &amp;byte, 1)</code> forever.</p>
  
  <p>How can I prevent this?</p>
  
  <p>Thanks</p>