title: C++ Serial port reading issue: does ioctl(FIONREAD) set a wrong value?
body: <p>I am facing a very strange problem, which I wasn't able to solve. I want to read (just read) data collected and sent by a micro-controller via usb as serial port (FTDI) on Mac Os X using c++. The the size of one complete data-sequence is always exactly 10 bytes. However I was using the following code to read the data:</p>
  
  <p>Imported libraries:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;termios.h&gt;
  #include &lt;sys/ioctl.h&gt;
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>void init(){
      serial = open(port.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK); // 0_RDONLY ?
      struct termios options;
      //set opt to 115200-8n1
      cfsetspeed(&amp;options, B115200);
      options.c_cflag &amp;= ~PARENB;
      options.c_cflag &amp;= ~CSTOPB;
      options.c_cflag &amp;= ~CSIZE;
      options.c_cflag |= CS8;
  
      tcsetattr(serial, TCSANOW, &amp;options);
      if (serial &lt; 0){
          //Error
      }else{
          //run loop
      }
  }
  
  void serial_loop(){
      long bytes_read;
      int bytes_available;
      unsigned char msg[10];
      while(1){
          do{
              usleep(1000);
              ioctl(serial, FIONREAD, &amp;bytes_available);
  
          }while(bytes_available &lt; 10); //wait for the sequence to complete
  
          bytes_read = read(serial, msg, 10);
  
          //do some parsing here
      }
  }
  </code></pre>
  
  <p>This code worked a few days ago but now it isn't anymore. The data is reaching the computer perfectly according to the Terminal -> screen -command. I checked the port-file-name which is still correct and the port is opened successfully as well. 
  I narrowed down my issue to the ioctl-command FIONREAD which doesn't write the correct number to the bytes_available-var (anymore). 
  It did work, and I believe, I didn't change anything in the code. </p>
  
  <p>Do you see any problem that could cause this issue?
  Are there any dangerous passages in my code? 
  Thank you for your help, I'm really stuck here...</p>
  
  <p>EDIT:
  Thanks to the feedback, I was able to get it running again. Here is the current code:</p>
  
  <pre><code>int serial;
  void init(){
      serial = open(port.c_str(), O_RDWR | O_NOCTTY); //removed 0_NONBLOCK
      struct termios options;
      //set opt to 115200-8n1
      cfsetspeed(&amp;options, B115200);
      options.c_cflag &amp;= ~PARENB;
      options.c_cflag &amp;= ~CSTOPB;
      options.c_cflag &amp;= ~CSIZE;
      options.c_cflag |= CS8;
  
      options.c_lflag &amp;= ~(ICANON | ECHO | ECHOE | ISIG); //Non-canonical
      options.c_cc[VMIN]     = 1; //block read until at least 1 byte was recieved
      options.c_lflag = 0;
  
      tcsetattr(serial, TCSANOW, &amp;options);
      if (serial &lt; 0){
          //Error
      }else{
          //run loop
      }
  }
  
  void serial_loop(){
      int datalength = 10;
      long bytes_read = 0;
      int bytes_in_msg = 0;
      unsigned char buf[datalength];
      unsigned char msg[datalength];
      do{
          bytes_read = read(serial, buf, datalength-bytes_in_msg);
          usleep(1000);
          if (bytes_read&gt;0){
              memcpy(&amp;msg[bytes_in_msg], &amp;buf, bytes_read);
          }
          bytes_in_msg += bytes_read;
      }while(bytes_in_msg &lt; datalength);
  
      //do some parsing here
      }
  }
  </code></pre>
  
  <p>This works, but is there anything left, that could be problematic?</p>
  
  <p>Thank you for your support!</p>