[asio-users] async_read zero bytes transferred
Brought to you by:
chris_kohlhoff
From: Tom B. <tom...@bd...> - 2019-04-30 18:02:26
|
Using boost asio 1.69.0 on Windows. Planning to port to Linux. I'm trying to send a binary command to a serial device and get a binary reply. The device always receives the command and sends the reply. However, sometimes my async_read handler is called with 0 bytes transferred instead of with the reply. Inserting a small delay before the async_read works around the problem, although obviously that is not a reliable fix. Retrying the async_read is a better workaround. It looks like the problem is in boost::asio::detail::read_op<AsyncReadStream, MutableBufferSequence, MutableBufferIterator, CompletionCondition, ReadHandler> void operator()(const boost::system::error_code& ec, std::size_t bytes_transferred, int start = 0) { std::size_t max_size; switch (start_ = start) { case 1: max_size = this->check_for_completion(ec, buffers_.total_consumed()); do { stream_.async_read_some(buffers_.prepare(max_size), BOOST_ASIO_MOVE_CAST(read_op)(*this)); return; default: buffers_.consume(bytes_transferred); if ((!ec && bytes_transferred == 0) || buffers_.empty()) break; max_size = this->check_for_completion(ec, buffers_.total_consumed()); } while (max_size > 0); handler_(ec, buffers_.total_consumed()); } } If (!ec && bytes_transferred == 0) the asynchronous operation terminates before any data is received. This is contrary to the async_read documentation. In our workaround, retrying the async_read means we are basically polling for the reply. That is okay when we know a reply is coming within a few milliseconds. But we also need to handle an event from the device that it sends after a relatively long time. It would be good if we don't have to poll for that message. Is there an asio API we can use to get a characters available event callback from a serial port? If not, is there any chance that async_read can be changes so it will read the requested amount of data before completion? Thanks, Tom ******************************************************************* IMPORTANT MESSAGE FOR RECIPIENTS IN THE U.S.A.: This message may constitute an advertisement of a BD group's products or services or a solicitation of interest in them. If this is such a message and you would like to opt out of receiving future advertisements or solicitations from this BD group, please forward this e-mail to opt...@bd.... [BD.v1.0] ******************************************************************* This message (which includes any attachments) is intended only for the designated recipient(s). It may contain confidential or proprietary information and may be subject to the attorney-client privilege or other confidentiality protections. If you are not a designated recipient, you may not review, use, copy or distribute this message. If you received this in error, please notify the sender by reply e-mail and delete this message. Thank you. ******************************************************************* Corporate Headquarters Mailing Address: BD (Becton, Dickinson and Company) 1 Becton Drive Franklin Lakes, NJ 07417 U.S.A. |