Closing boost::asio::serial_port with pending read_async_some

I am chaining read_async_some() calls to asynchronously read from a serial port. At some point, I need to cancel the asynchronous reads using and detect this fact in the associated handlers. From the documentation for cancel(), I expected to do so simply by checking the error_code passed to my handlers:

This function causes all outstanding asynchronous read or write operations to finish immediately, and the handlers for cancelled operations will be passed the boost::asio::error::operation_aborted error.

However, when I try this, my handlers are called with the invalid_argument error instead of the expected operation_aborted error. Here is a minimal example that reproduces the issue by using a ptty to emulate a serial port:

void handle(boost::system::error_code const& error, size_t count) { std::cout << "error_code = " << error.message() << std::endl; } int main(int argc, char **argv) { std::fstream fs("/dev/ttyp0", std::ios::in | std::ios::ate); boost::asio::io_service io; boost::asio::serial_port serial(io, "/dev/ttyp0"); std::vector<uint8_t> buffer(1); serial.async_read_some(boost::asio::buffer(buffer), boost::bind(&handle, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred ) ); boost::thread thread(boost::bind(&boost::asio::io_service::run, &io)); serial.cancel(); thread.join(); fs.close(); }

At least for me, the output of this program is error_code = Invalid argument. Can anyone explain why I am not getting the behavior described in the documentation?

-------------Problems Reply------------

Nevermind. In case anyone else runs into the same problem, the issue was actually with using pttys for testing. As it turns out, pttys don't behave properly when used for asynchronous input and boost::asio indicates this problem with the above error message.

I was able to solve this issue by creating a simulated loopback serial port with socat. There are good instructions for doing so in this blog post.

Category:c# Views:5 Time:2012-03-12

Related post

  • Boost Asio serial_port - need help with io 2008-11-06

    So I've been trying to learn the boost::asio stuff to communicate to a serial device using RS232. The documementation is sparse and the examples are non-existent. Can't figure out exactly how to communicate with the device. The device can't send data

  • boost::asio::serial_port reading after reconnecting Device 2009-02-12

    I have a problem with the boost::asio::serial_port class reading from a GPS device (USB-Serial). Connecting the device and reading from it works fine, but when I disconnect and reconnect the device, read_some doesn't read any bytes from the port. As

  • change the DCB structure of a boost::asio::serial_port 2009-05-18

    I'd like to disable RTSControl using boost::asio::serial_port::set_option function. and also be able to raise or lower the DTR line? boost::asio::serial_port_base::baud_rate baud_option(115200); serialPort.set_option(baud_option); The standard option

  • Unblock synchronous read on boost::asio::serial_port 2012-02-08

    I have a boost::thread which performs synchronous reads on a boost::asio::serial_port. When I destroy an instance of the class which contains both, I want the thread to end gracefully even if its blocked in a read call. How can I do this? Looking at

  • How to change serial port speed on the fly with boost::asio (or how to find out if the hardware buffer is empty)? 2010-12-16

    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 tr

  • How to use Boost.Asio c++? 2011-04-04

    I would try to use the library to use socket Boost.Asio c++ on multiple platforms. I downloaded the latest version here: but now what do I use in my code? I have compile it? include just enoug

  • Boost::asio and async_read 2011-08-08

    I am trying to use Boost::asio and async_read to implement a timeout in protocol which uses the serial port. I have done a test implementation using synchronous read, which works just find, but my implementation with time out does not work. void set_

  • Boost Asio serial port issue 2011-08-16

    I'm using CodeBlocks on a windows systems and have downloaded Boost, complied and setup my IDE variables and build options. I have been successfully using other boost libraries and now I need to work on a program that reads and writes to a serial por

  • Where to implement a protocol using boost::asio? 2011-11-18

    I am trying to implement a simple serial port protocol. It goes like this: discard all data until 0xff is received read header (node address and data length, 4 bytes) read data (max. 64 bytes) read crc process the received packet send response when 0

  • Boost.Asio linking error 2012-01-11

    I'm trying access an extern device via a serial port and want to use Boost.Asio for this propose. I have build the boost libraries for MinGw and compiled the regex example successful. But I have problems to compile my code if I include something from

  • Is it guaranteed that handler of async_* in boost::asio will be always called? 2012-03-03

    I call boost::asio::serial_port::async_write_some() and asio::io_service::run() in the other thread. There is a callback given to "async_write_some". Can I safely assume, that this callback will be eventually called (possibly with some error code)? U

  • Get event when tcp connection closed in boost::asio 2011-11-24

    I write a tcp server by boost::asio libs and in my application I have object that tend to a client each client have a manager class. How can I to know when a tcp connection closed delete the manager object. I think to use some thing like async_close(

  • boost::asio - know when the conection has to be shutdown/closed 2012-02-03

    I implement a protocol (socks) that requires that my server to relay connections coming from the client to the destination. The way I implement relaying part is by using something like this: socket_.async_read_some(boost::asio::buffer(dataClient_, 10

  • [boost.asio]closing tcp::socket or tcp::acceptor in different thread from the I/O thread 2012-03-05

    Could I close tcp::socket in different thread from the sync-reading thread? It looks like: boost::asio::ip::tcp::socket* tcp_socket; //blocking mode thread1: while(true){ try{ std::vector<char> read_buffer(10); tcp_socket->read_some( boost::

  • BOOST ASIO: Server crash when sending on closed connection 2011-01-17

    Boost version 1.43 Platform: WindowsXP I have this boost TCP server which crashes (throws boost exception somewhere ) only when the client suddenly closes the connection when I send him data (i.e. in the middle of the "asio::send(..)"): Here is the s

  • Thread-safely closing a boost::asio::ip::tcp::socket being used synchronously 2012-11-22

    Given that the boost::asio::ip::tcp::acceptor and boost::asio::ip::tcp::socket are both marked as non-thread safe as of Boost 1.52.0, is it possible to shutdown a tcp::acceptor currently blocking on accept() from a separate thread? I've looked at cal

  • how to clean boost::asio::ssl::stream after closed by server 2014-08-12

    Currently I am developing a gateway to connect apns to push notification. Apns will close the ssl socket when the connection is idle for about one hour. Sometimes when I reconnect, boost.asio keep telling me that handshake fail with error code 335544

  • How should one tear down a boost::asio::ip::udp::socket? 2009-08-03

    I have read the boost asio reference, gone through the tutorial and looked at some of the examples. Still, I am unable to see how a socket should be torn down: Should I call close() or is this done by the socket's destructor? When should I call shutd

  • Interrupt boost::asio synchronous read? 2009-12-06

    I'm using asio synchronous sockets to read data over TCP from a background thread. This is encapsulated in a "server" class. However, I want the thread to exit when the destructor of this class is called. The problem is that a call to any of the read

Copyright (C), All Rights Reserved.

processed in 0.135 (s). 11 q(s)