Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read all available data in serial port with boost asio

I cannot find a way to make what seems to be one of the simplest thing with the boost asio library :

Read all the data available in a serial port

For example in Qt there is a readAll() method from QextSerialPort.

Actually, all the examples I found are about reading a certain amount of data, or multi threaded.

In my case I need it to read the data from a GSM controller. The response size depends on the sent command and the controller context so I can't specify it.

like image 445
Oswin Avatar asked Oct 26 '25 07:10

Oswin


2 Answers

Try this:

boost::asio::read(
    sock,
    boost::asio::buffer(buf),
    boost::asio::transfer_at_least(1),
    ec
);
like image 54
Vlad Markushin Avatar answered Oct 28 '25 03:10

Vlad Markushin


You want the transfer_all() completion condition combined with the read() or async_read() free functions:

boost::array<char, 128> buf;
boost::system::error_code ec;
std::size_t n = boost::asio::read(
    sock, boost::asio::buffer(buf),
    boost::asio::transfer_all(), ec);
if (ec)
{
  // An error occurred.
}
else
{
  // n == 128
}
like image 38
Sam Miller Avatar answered Oct 28 '25 04:10

Sam Miller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!