Boost logo

Boost Users :

Subject: [Boost-users] serial port and boost::iostream
From: Christoph Gysin (christoph.gysin_at_[hidden])
Date: 2009-03-02 07:34:20

I wrote a simple serial device for the boost::iostream library using
the serial interface from boost:asio. Feel free to include it as an
example if you find it useful.


#include <boost/iostreams/stream.hpp>
#include <boost/asio/serial_port.hpp>

class serial_device
        typedef char char_type;
        typedef boost::iostreams::bidirectional_device_tag category;

        serial_device(boost::asio::serial_port& port) : serial_port(port) {};

        std::streamsize read(char* buf, std::streamsize n)
                return serial_port.read_some(boost::asio::buffer(buf, n));
        std::streamsize write(const char* buf, std::streamsize n)
                return serial_port.write_some(boost::asio::buffer(buf, n));

        boost::asio::serial_port& serial_port;

void example()
        // set up serial port
        boost::asio::io_service io_service;
#ifdef WIN32
        boost::asio::serial_port serial_port(io_service, "COM1");
        boost::asio::serial_port serial_port(io_service, "/dev/ttyS0");

        typedef boost::asio::serial_port_base spb;

        // create iostream
        serial_device dev(serial_port);
        boost::iostreams::stream<serial_device> serial(dev);

        // use serial port with iostream interface
        serial << "boost::iostreams are fun!" << std::endl;
        std::string response;
        serial >> response;

echo mailto: NOSPAM !#$.'<*>'|sed 's. ..'|tr "<*> !#:2" org_at_fr33z3

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at