[Boost-users] Can someone provide an Asio Serial Port example?

2,489 views
Skip to first unread message

Jeff

unread,
Jan 20, 2010, 5:07:12 PM1/20/10
to boost...@lists.boost.org
I would like to use Asio to connect to a microcontroller to send and read from,
but there are no serial_port samples available.

For instance, I would like to type an ‘a’ into the keyboard and have the
microcontroller return 1. The microcontroller is already programmed for this
but I need to create an interface for it.

A sample that does this or any working sample at all would be sincerely
appreciated.

Thank you,

Jeff


_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Michael Caisse

unread,
Jan 21, 2010, 2:33:27 AM1/21/10
to boost...@lists.boost.org
Jeff wrote:
> I would like to use Asio to connect to a microcontroller to send and read from,
> but there are no serial_port samples available.
>
> For instance, I would like to type an ‘a’ into the keyboard and have the
> microcontroller return 1. The microcontroller is already programmed for this
> but I need to create an interface for it.
>
> A sample that does this or any working sample at all would be sincerely
> appreciated.
>
> Thank you,
>
> Jeff
>
>

Hi Jeff -

Using the asio serial port is just like using the socket after you
have one opened and going.

To get things going:

Construct the serial port with the associated io_service as
with other ASIO services...

asio::serial_port port( io_service );


Next, open the device and setup various options. I have found
that the defaults among various OS's for the asio serial port
differ. It can create real headaches so I initialize everything
that I like:

port.open( "/dev/ttyUSB0" ); // or whatever the device might be

if( !port.is_open() ){ // do something clever if the open failed }

typedef boost::asio::serial_port_base asio_serial;

port.set_option( asio_serial::baud_rate( baud ) );
port.set_option( asio_serial::flow_control( asio_serial::flow_control::none ) );
port.set_option( asio_serial::parity( asio_serial::parity::none ) );
port.set_option( asio_serial::stop_bits( asio_serial::stop_bits::one ) );
port.set_option( asio_serial::character_size( 8 ) );


Then just use the port as you would a socket. For example,

port.async_read_some( asio::buffer( in_message, MAX_MESSAGE_SIZE ),
boost::bind( &my_client::read_done,
shared_from_this(),
asio::placeholders::error,
asio::placeholders::bytes_transferred ) );


HTH,
michael

--

----------------------------------
Michael Caisse
Object Modeling Designs
www.objectmodelingdesigns.com

Markus Werle

unread,
Jan 21, 2010, 4:05:52 AM1/21/10
to boost...@lists.boost.org
Jeff <jeff_j_dunlap <at> yahoo.com> writes:

> [...] there are no serial_port samples available.
> [...] A sample that does this or any working sample at all
> would be sincerely appreciated.

First hit in http://www.google.de/search?q=asio+serial+port is
http://old.nabble.com/Simple-serial-port-demonstration-with-boost-asio-
asynchronous-I-O-td19849520.html

which I find helpful ...


Markus

Andrew Maclean

unread,
Jan 21, 2010, 5:02:30 PM1/21/10
to boost...@lists.boost.org
If you create one, please also post it here.

Thanks
Andrew

--
___________________________________________
Andrew J. P. Maclean
Centre for Autonomous Systems
The Rose Street Building J04
The University of Sydney 2006 NSW
AUSTRALIA
Ph: +61 2 9351 3283
Fax: +61 2 9351 7474
URL: http://www.acfr.usyd.edu.au/
___________________________________________

Jeff Dunlap

unread,
Jan 30, 2010, 4:45:07 PM1/30/10
to boost...@lists.boost.org

I'd really like to thank Jeff Gray for his serial example as well as Markus and
Michael for the guidance to get me started.

I used Mr. Gray's example which at first didn't work in my particular case
because of some configuration options that are missing but then I configured
the port as suggested by Michael and next thing I knew I was reading from and
writing to a hobby microcontroller that I was given. I still have quite a bit
to learn though.

Thank you

Reply all
Reply to author
Forward
0 new messages