0

I am using ASIO to read/write to a device through a serial port. When I start my program, I first need to ensure that the device is properly configured. This is done by a series of writes in an initialize_device() function. The device sends back some data which is of no interest after these calls.

When initialize_device() is done, I call my start_receive() function which in turns calls an asio::async_read_until(), and my handler again calls start_receive()

The problem, however, is that the irrelevant data that was sent to me during initialize_device() is "received" by my handler, even though this was data that was received before my first read. This was unexpected for me.

I am thus wondering if there's a correct way to "flush" the internal buffer that the asio::serial_port uses after initialize_device() is completed? I really ain't interested in any data that was received before I explicitly start reading the port.

1 Answer 1

2

The device will have a documented protocol.

You will have to be able to tell what is sent in response to your initializing commands.

Just use async_read_until to get this information into a dummy buffer (boost::asio::null_buffer?) before you start reading the "real" data.

2
  • I could not get null_buffer to work with async_read_until. I have now decided to handle the returned initialization data, though I could still see cases where one wouldn't want to handle any data received prior to the first receive. Guess one could use a simple bool to indicate if data should be processed by the handler or not. May 9, 2014 at 11:55
  • Indeed. You simply cannot decide "not to receive" data that the device has decided to send. You'll have to explicitly ignore/discard it
    – sehe
    May 9, 2014 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.