Boost logo

Boost Users :

Subject: [Boost-users] combining asio::serial_port with iostreams::stream
From: Christoph Gysin (christoph.gysin_at_[hidden])
Date: 2009-01-18 15:34:30


Hi boost users

I'm a newbie to boost trying to use a serial port as an iostream.
While searching the archives I found a post by Rob Desbois (2008-01-18
13:02) trying to achive the same. Unfortunately the solution wasn't
posted.

This is what I got so far:

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

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

        serial_device(boost::asio::io_service& io_service, const std::string&
port, int baudrate = 9600) :
                serial_port(io_service, port)
        {
                typedef boost::asio::serial_port_base spb;
                serial_port.set_option(spb::baud_rate(baudrate));
        };

        std::streamsize read(char* buf, std::streamsize n)
        {
                boost::asio::read(serial_port, boost::asio::buffer(buf, n));
        }

        std::streamsize write(const char* buf, std::streamsize n)
        {
                boost::asio::write(serial_port, boost::asio::buffer(buf, n));
        }

private:
        boost::asio::serial_port serial_port;
};

int main(void)
{
        boost::asio::io_service io_service;
        serial_device serial(io_service, "/dev/ttyS0", 115200);

        //boost::iostreams::stream<serial_device> s(serial);
}

If I uncomment the last line it fails to compile:

g++ -lboost_iostreams-mt -lboost_system-mt -lboost_thread-mt
iostream.cpp -o iostream
/usr/include/boost/noncopyable.hpp: In copy constructor
'boost::asio::basic_io_object<boost::asio::serial_port_service>::basic_io_object(const
boost::asio::basic_io_object<boost::asio::serial_port_service>&)':
/usr/include/boost/asio/basic_io_object.hpp:30: instantiated from
'boost::iostreams::stream<Device, Tr, Alloc>::stream(Device&, int,
int) [with Device = serial_device, Tr = std::char_traits<char>, Alloc
= std::allocator<char>]'
iostream.cpp:36: instantiated from here
/usr/include/boost/noncopyable.hpp:27: error:
'boost::noncopyable_::noncopyable::noncopyable(const
boost::noncopyable_::noncopyable&)' is private
/usr/include/boost/asio/basic_io_object.hpp:30: error: within this context
/usr/include/boost/noncopyable.hpp: In copy constructor
'boost::asio::detail::reactive_descriptor_service<boost::asio::detail::epoll_reactor<false>
>::implementation_type::implementation_type(const
boost::asio::detail::reactive_descriptor_service<boost::asio::detail::epoll_reactor<false>
>::implementation_type&)':
/usr/include/boost/noncopyable.hpp:27: error:
'boost::noncopyable_::noncopyable::noncopyable(const
boost::noncopyable_::noncopyable&)' is private
/usr/include/boost/asio/detail/reactive_descriptor_service.hpp:47:
error: within this context
/usr/include/boost/asio/basic_io_object.hpp: In copy constructor
'boost::asio::basic_io_object<boost::asio::serial_port_service>::basic_io_object(const
boost::asio::basic_io_object<boost::asio::serial_port_service>&)':
/usr/include/boost/asio/basic_io_object.hpp:30: note: synthesized
method 'boost::asio::detail::reactive_descriptor_service<boost::asio::detail::epoll_reactor<false>
>::implementation_type::implementation_type(const
boost::asio::detail::reactive_descriptor_service<boost::asio::detail::epoll_reactor<false>
>::implementation_type&)' first required here
/usr/include/boost/asio/basic_serial_port.hpp: In copy constructor
'boost::asio::basic_serial_port<boost::asio::serial_port_service>::basic_serial_port(const
boost::asio::basic_serial_port<boost::asio::serial_port_service>&)':
/usr/include/boost/asio/basic_serial_port.hpp:51: note: synthesized
method 'boost::asio::basic_io_object<boost::asio::serial_port_service>::basic_io_object(const
boost::asio::basic_io_object<boost::asio::serial_port_service>&)'
first required here
iostream.cpp: In copy constructor 'serial_device::serial_device(const
serial_device&)':
iostream.cpp:5: note: synthesized method
'boost::asio::basic_serial_port<boost::asio::serial_port_service>::basic_serial_port(const
boost::asio::basic_serial_port<boost::asio::serial_port_service>&)'
first required here
/usr/include/boost/iostreams/stream.hpp: In constructor
'boost::iostreams::stream<Device, Tr, Alloc>::stream(Device&, int,
int) [with Device = serial_device, Tr = std::char_traits<char>, Alloc
= std::allocator<char>]':
/usr/include/boost/iostreams/stream.hpp:129: note: synthesized method
'serial_device::serial_device(const serial_device&)' first required
here
make: *** [iostream] Error 1

Could anyone please explain what I'm doing wrong?

Thanks,
Chris

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

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