Closing boost :: asio :: serial_port awaiting read_async_some

I bind read_async_some() calls to asynchronously read from the serial port. At some point, I need to cancel asynchronous readings, using and detecting this fact in related handlers. From the documentation for cancel() I expected to do this simply by checking the error_code passed to my handlers:

This function calls all outstanding asynchronous reads or writes of the operation for immediate completion, and boost::asio::error::operation_aborted will be passed to the handlers for canceling boost::asio::error::operation_aborted .

However, when I try to do this, my handlers are called with an invalid_argument error instead of the expected operation_aborted error. Here is a minimal example that reproduces the problem using 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 someone explain why I do not understand the behavior described in the documentation?

+4
source share
1 answer

Nevermind In case someone else is facing the same problem, the problem was actually using pttys for testing. As it turned out, pttys behaved incorrectly when used for asynchronous input, and boost::asio points to this problem with the error message above.

I managed to solve this problem by creating a simulated loopback serial port with socat . There are good instructions for this in this blog post .

+4
source

Source: https://habr.com/ru/post/1400938/


All Articles