How to determine if there are bytes available to b

2019-07-08 23:45发布

问题:

I am trying to use boost to communicate serially between my desktop and an arduino. In arduino space, I can ask the serial port whether or not there are bytes available before trying to perform a read.

I am having trouble finding the equivalent for boost::asio::serial_port.

回答1:

While Boost.Asio does not provide direct support for this, one can still accomplish this by using serial port's native_handle() with system specific calls. Consult the system's documentation to determine how to query for the available bytes ready to be read, but it is often ioctl(..., FIONREAD, ...) on Linux, and ClearCommError() on Windows.


Here is a complete minimal example that uses system specific calls to get the number of bytes available. The example program will continue to query the serial port until there are greater than 20 bytes available, at which point it will read all but 5 bytes:

#include <iostream>
#include <vector>
#include <boost/asio.hpp>
#include <boost/thread.hpp>

/// @brief Returns the number of bytes available for reading from a serial
///        port without blocking.
std::size_t get_bytes_available(
  boost::asio::serial_port& serial_port,
  boost::system::error_code& error)
{
  error = boost::system::error_code();
  int value = 0;
#if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  COMSTAT status;
  if (0 != ::ClearCommError(serial_port.lowest_layer().native_handle(),
                            NULL, &status))
  {
    value = status.cbInQue;
  }
  // On error, set the error code.
  else
  {
    error = boost::system::error_code(::GetLastError(),
       boost::asio::error::get_system_category());
  }
#else // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  if (0 == ::ioctl(serial_port.lowest_layer().native_handle(),
                   FIONREAD, &value))
  {
    error = boost::system::error_code(errno,
       boost::asio::error::get_system_category());
  }
#endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)

  return error ? static_cast<std::size_t>(0)
               : static_cast<size_t>(value);

}

/// @brief Returns the number of bytes available for reading from a serial
///        port without blocking.  Throws on error.
std::size_t get_bytes_available(boost::asio::serial_port& serial_port)
{
  boost::system::error_code error;
  std::size_t bytes_available = get_bytes_available(serial_port, error);
  if (error)
  {
    boost::throw_exception((boost::system::system_error(error)));
  }
  return bytes_available;
}

int main(int argc, char* argv[])
{
  if (argc < 2)
  {
    std::cerr << "Usage: " << argv[0] << " <device_name>" << std::endl;
    return 1;
  }

  // Create all I/O objects.
  boost::asio::io_service io_service;
  boost::asio::serial_port serial_port(io_service, argv[1]);

  // Continue quering the serial port until at least 20 bytes are available
  // to be read.
  std::size_t bytes_available = 0;
  while (bytes_available < 20)
  {
    bytes_available = get_bytes_available(serial_port);
    std::cout << "available: " << bytes_available << std::endl;
    boost::this_thread::sleep_for(::boost::chrono::seconds(3));
  }

  // Read all but 5 available bytes.
  std::vector<char> buffer(bytes_available - 5);
  std::size_t bytes_transferred =
      read(serial_port, boost::asio::buffer(buffer));
  bytes_available = get_bytes_available(serial_port);

  // Print results.
  std::cout << "Read " << bytes_transferred << " bytes\n";
  std::cout.write(&buffer[0], bytes_transferred);
  std::cout << "\navailable: " << bytes_available << std::endl;
}

Create virtual serial ports with socat:

$ socat -d -d PTY: PTY
2015/02/01 21:12:31 socat[3056] N PTY is /dev/pts/2
2015/02/01 21:12:31 socat[3056] N PTY is /dev/pts/3
2015/02/01 21:12:31 socat[3056] N starting data transfer loop
                                with FDs [3,3] and [5,5]

After starting the program in one terminal, I write to /dev/pts/3 in another terminal:

$ echo -n "This is" > /dev/pts/3
$ echo -n " an example" > /dev/pts/3
$ echo -n " with asio." > /dev/pts/3

And the resulting output from the program:

$ ./a.out /dev/pts/2
available: 0
available: 7
available: 18
available: 29
Read 24 bytes
This is an example with
available: 5


回答2:

I don't know of such a thing in asio, but as comments above have already stated, you don't really need it. I have an example of how to use boost asio serial at:

https://github.com/cdesjardins/ComBomb/blob/master/TargetConnection/TgtSerialConnection.cpp

It uses async_read_some to fill a buffer with serial data, the buffer data is then queued up for other parts of the program to process.