Can someone provide an Asio Serial Port example?
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
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
Michael Caisse
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
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
Jeff
[...] 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
If you create one, please also post it here.
Thanks
Andrew
On Thu, Jan 21, 2010 at 8:05 PM, Markus Werle
Jeff
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
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- ___________________________________________ 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/ ___________________________________________
participants (5)
-
Andrew Maclean
-
Jeff
-
Jeff Dunlap
-
Markus Werle
-
Michael Caisse