Take the 2-minute tour ×

I'm having a peculiar problem with boost::asio and a boost::asio::serial_port device. The code is finally working pretty well with asynchronous reads and stuff, but I can't figure out how to change the speed of the serial port on the fly.

What I'm trying to do right now is just telling the device connected in my serial port to change the serial port speed to say 38400 baud, then I'm setting my computers serial port to the same speed via:


But what's really happening is that if I do the set_option part, the device never receives the command to change the speed. If I don't do the set_option part the device changes speed correctly. From what I gather what's happening is that the (blocking, synchronous) write puts stuff in the hardware buffer on my computer and returns, then does the set_option which discards the buffer (before it has had time to send data to the device). So I need to think of some way to check if the hardware buffer is empty and the device really has received the command to change the speed, before reconfiguring my computers serial port. I also cannot find any info on if I have to do close() and open() on the port for the speed change to take affect. I'm also wondering if close() discards the stuff in the buffer or not... I'm using a USB->serial port adapter and my platform is Ubuntu 10.10 if it makes any difference.

share|improve this question
add comment

2 Answers

Have you looked at man 3 termios? It seems tcdrain does what you need

tcdrain() waits until all output written to the object referred to by fd has been transmitted.

You can get the native descriptor from the boost::asio::serial_port::native method.

share|improve this answer
add comment

Did you try flushing the buffer or looking for an appropriate flush alternative?

Are the client and server in the same process?

ASIO iostream flush not working?

share|improve this answer
Well I basically have a boost::asio:io_service and a boost::asio::serial_port, io_service gets initialized, serial_port gets initialized with the io_service in the constructor and then the serial_port is just configured with the set_options. I can't say I can see any flush() methods in serial_port or io_service. I do not have an asio::iostream anywhere in my code. –  Dago Dec 17 '10 at 7:43
I've always closed the serial port when I wanted to connect to a different device operating at a different speed. It's an interesting problem - sorry I can't be of more help. –  Kieveli Dec 17 '10 at 14:38
-1 the OP is not using Asio streams –  Sam Miller Dec 23 '10 at 16:22
add comment

Your Answer


