title: Boost ASIO and serial port async read
body: <p>I am trying to use Boost ASIO for a serial port async read that I must continuously listen to when my program is run, additionally I am also listening to <a href="https://www.boost.org/doc/libs/1_68_0/doc/html/boost_asio/example/cpp11/multicast/receiver.cpp" rel="nofollow noreferrer">multicast udp reciever</a> example I found in the Boost ASIO. Both UDP and serial receiver I am initializing with the same io_context. When I try to create serial port in my console app main program, I can compile and read from serial port but only once. So I thought I should make it into a separate class like in this <a href="https://www.boost.org/doc/libs/1_68_0/doc/html/boost_asio/tutorial/tutdaytime6/src.html" rel="nofollow noreferrer">example</a> and maybe make use of a separate thread. However I get compilation error even when I am using the same exact syntax that I tried inside the main program which runs fine but only once. How can I continue to listen in async fashion on serial port too while I am continuously (still async) reading from the udp port data. Please advice. </p>
  
  <p>I tried to look around for non-official examples, but they were really not helpful, either they were poor, outdated or just unnecessarily complicated. What I am trying to build is far more simpler like this </p>
  
  <pre><code>#pragma once
  #include &lt;boost/asio.hpp&gt;
  #include &lt;boost/array.hpp&gt;
  #include &lt;boost/bind.hpp&gt;
  #include &lt;boost/shared_ptr.hpp&gt;
  #include &lt;iostream&gt;
  
  class SerialComm
  {
  public:
      SerialComm(boost::asio::io_context&amp; io_context, std::string&amp; serial_port_name) :_serialport(io_context, serial_port_name)
      {
          start_receive();
      }
  
      ~SerialComm()
      {
      }
  
  private:
      void start_receive()
      {
          _serialport.async_read_some(boost::asio::buffer(my_buffer, 1024),
              &amp;SerialComm::handle_receive);
      }
  
      void handle_receive(const boost::system::error_code&amp; error,
          std::size_t bytes_transferred)
      {
          if (!error)
          {
              std::cout &lt;&lt; "Recieved:" &lt;&lt; my_buffer &lt;&lt; std::endl;
          }
      }
  
  private:
  
      boost::asio::serial_port _serialport;
      unsigned char my_buffer[1024];
      //boost::asio::buffer&lt;char, 1024&gt; recv_buffer_;
  };
  </code></pre>
  
  <p>This is the error I get :</p>
  
  <blockquote>
    <p>1>c:\users\alam
    syed\documents\someproject\boost\include\boost-root\boost\asio\serial_port.hpp(746):
    error C2338: ReadHandler type requirements not met 1>c:\users\alam
    syed\documents\someproject\public\serialcomm.h(24): note: see
    reference to function template instantiation 'void
    boost::asio::serial_port::async_read_some(const
    MutableBufferSequence &amp;,ReadHandler &amp;&amp;)' being compiled 1>        with
    1>        [ 1><br>
    MutableBufferSequence=boost::asio::mutable_buffers_1, 1><br>
    ReadHandler=void (__cdecl SerialComm::* )(const
    boost::system::error_code &amp;,size_t) 1>        ] 1>c:\users\alam
    syed\documents\someproject\boost\include\boost-root\boost\asio\use_future.hpp(138):
    note: see reference to class template instantiation
    'boost::asio::use_future_t>::std_allocator_void'
    being compiled 1>c:\users\alam
    syed\documents\someproject\boost\include\boost-root\boost\asio\use_future.hpp(146):
    note: see reference to class template instantiation
    'boost::asio::use_future_t>' being compiled</p>
  </blockquote>