Boost logo

Boost Users :

Subject: [Boost-users] Error when calling boost::asio::async_read on a boost::asio::serial_port
From: Whitham Reeve (thetawaves_at_[hidden])
Date: 2010-11-25 11:07:59


Hello all, I am having trouble getting asio serial ports to work.
Specifically when I call boost::asio::async_read, it immediately calls the
read completion handler with error EINVAL aka Invalid argument. Please
help! I have invested significant time in asio serial ports!

Mac OSX 10.6.4
GCC 4.2.1
boost version 1.44.0

The test case console output:
$ ./test
ERROR 22 Invalid argument
$

The test case code:

#include <iostream>
#include <cstdlib>
#include <vector>
#include <boost/asio.hpp>
#include <boost/bind.hpp>

const int HeaderSize = 2;
std::vector<char> ReadVector;

void ReadHandler(const boost::system::error_code& error, std::size_t
bytes_transferred)
{
    if (error)
    {
        std::cout << "ERROR " << error.value() << " " <<
error.category().message(
error.value()) << std::endl;
        return;
    }
    else
    {
        std::cout << "Bytes Received" << std::endl;
    }

}

int main ()
{

    ReadVector.resize(2000);

    boost::asio::io_service SerialService;
    boost::asio::serial_port SerialPort(SerialService,
"/dev/tty.usbserial-FTCW1RJN");

    boost::asio::serial_port_base::baud_rate baud_option(115200);
    SerialPort.set_option(baud_option);

    assert(SerialPort.is_open());

    boost::asio::async_read(SerialPort, boost::asio::buffer(ReadVector,
HeaderSize),
                                            boost::bind(&ReadHandler,

boost::asio::placeholders::error,

boost::asio::placeholders::bytes_transferred));

    SerialService.run();
}

-- 
Whitham D. Reeve II
907-764-5263


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net