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.
Chris
#include
#include
class serial_device
{
public:
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));
}
private:
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");
#else
boost::asio::serial_port serial_port(io_service, "/dev/ttyS0");
#endif
typedef boost::asio::serial_port_base spb;
serial_port.set_option(spb::baud_rate(9600));
// create iostream
serial_device dev(serial_port);
boost::iostreams::stream 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@fr33z3