title: Boost Asio Serial port How to find out the size of the buffer in the queue for reading C++
body: <p>In Arduino data can be read as follows:</p>
  <pre><code>void setup() {
      Serial.begin(9600);
  }
  
  String Comand = &quot;&quot;;
  void loop() {
      if (Serial.available() &gt; 0) //If there is data read 
          {
          char c = Serial.read(); //get byte
          if(c!='\n')
          {
            Comand+=c;
          }
          else
          {
            if(Comand == &quot;MyComand&quot;)
            {
              Serial.println(&quot;Comand 1&quot;);
            }
            //else
            //...
            Comand = &quot;&quot;;
          }
      }
  }
  </code></pre>
  <p>С++ boost asio
  Here is an example of c ++ playback of data from the port, but what function to check if there is anything to read?</p>
  <pre><code>#include &lt;boost/asio.hpp&gt;
  #include &lt;windows.h&gt;
  
  //....
  
  int main(int, char**)
  {
      {
          boost::asio::io_service io;
          boost::asio::serial_port serial(io,&quot;COM5&quot;);
          serial.set_option(boost::asio::serial_port_base::baud_rate(9600));
  
          std::string s = &quot;MyComand\n&quot;;
          boost::asio::write(serial,boost::asio::buffer(s.c_str(),s.size()));
          std::string result;
  
          //while serial data &gt; 0 ???  How to check
          {
              using namespace boost;
              char c;
              asio::read(serial,asio::buffer(&amp;c,1));
              result+=c;
          }
  
          std::cout&lt;&lt;result.c_str();
  
      }
  
  return 0;
  }
  </code></pre>
  <p><strong>If I read in a cycle, and there is no data, the program will stop and wait for the data, it does not suit me. How to check if there is data before trying to read.</strong></p>