basic_serial_port.hpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. //
  2. // basic_serial_port.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef ASIO_BASIC_SERIAL_PORT_HPP
  12. #define ASIO_BASIC_SERIAL_PORT_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include "asio/detail/config.hpp"
  17. #if defined(ASIO_ENABLE_OLD_SERVICES)
  18. #if defined(ASIO_HAS_SERIAL_PORT) \
  19. || defined(GENERATING_DOCUMENTATION)
  20. #include <string>
  21. #include "asio/basic_io_object.hpp"
  22. #include "asio/detail/handler_type_requirements.hpp"
  23. #include "asio/detail/throw_error.hpp"
  24. #include "asio/error.hpp"
  25. #include "asio/serial_port_base.hpp"
  26. #include "asio/serial_port_service.hpp"
  27. #include "asio/detail/push_options.hpp"
  28. namespace asio {
  29. /// Provides serial port functionality.
  30. /**
  31. * The basic_serial_port class template provides functionality that is common
  32. * to all serial ports.
  33. *
  34. * @par Thread Safety
  35. * @e Distinct @e objects: Safe.@n
  36. * @e Shared @e objects: Unsafe.
  37. */
  38. template <typename SerialPortService = serial_port_service>
  39. class basic_serial_port
  40. : public basic_io_object<SerialPortService>,
  41. public serial_port_base
  42. {
  43. public:
  44. /// The native representation of a serial port.
  45. typedef typename SerialPortService::native_handle_type native_handle_type;
  46. /// A basic_serial_port is always the lowest layer.
  47. typedef basic_serial_port<SerialPortService> lowest_layer_type;
  48. /// Construct a basic_serial_port without opening it.
  49. /**
  50. * This constructor creates a serial port without opening it.
  51. *
  52. * @param io_context The io_context object that the serial port will use to
  53. * dispatch handlers for any asynchronous operations performed on the port.
  54. */
  55. explicit basic_serial_port(asio::io_context& io_context)
  56. : basic_io_object<SerialPortService>(io_context)
  57. {
  58. }
  59. /// Construct and open a basic_serial_port.
  60. /**
  61. * This constructor creates and opens a serial port for the specified device
  62. * name.
  63. *
  64. * @param io_context The io_context object that the serial port will use to
  65. * dispatch handlers for any asynchronous operations performed on the port.
  66. *
  67. * @param device The platform-specific device name for this serial
  68. * port.
  69. */
  70. explicit basic_serial_port(asio::io_context& io_context,
  71. const char* device)
  72. : basic_io_object<SerialPortService>(io_context)
  73. {
  74. asio::error_code ec;
  75. this->get_service().open(this->get_implementation(), device, ec);
  76. asio::detail::throw_error(ec, "open");
  77. }
  78. /// Construct and open a basic_serial_port.
  79. /**
  80. * This constructor creates and opens a serial port for the specified device
  81. * name.
  82. *
  83. * @param io_context The io_context object that the serial port will use to
  84. * dispatch handlers for any asynchronous operations performed on the port.
  85. *
  86. * @param device The platform-specific device name for this serial
  87. * port.
  88. */
  89. explicit basic_serial_port(asio::io_context& io_context,
  90. const std::string& device)
  91. : basic_io_object<SerialPortService>(io_context)
  92. {
  93. asio::error_code ec;
  94. this->get_service().open(this->get_implementation(), device, ec);
  95. asio::detail::throw_error(ec, "open");
  96. }
  97. /// Construct a basic_serial_port on an existing native serial port.
  98. /**
  99. * This constructor creates a serial port object to hold an existing native
  100. * serial port.
  101. *
  102. * @param io_context The io_context object that the serial port will use to
  103. * dispatch handlers for any asynchronous operations performed on the port.
  104. *
  105. * @param native_serial_port A native serial port.
  106. *
  107. * @throws asio::system_error Thrown on failure.
  108. */
  109. basic_serial_port(asio::io_context& io_context,
  110. const native_handle_type& native_serial_port)
  111. : basic_io_object<SerialPortService>(io_context)
  112. {
  113. asio::error_code ec;
  114. this->get_service().assign(this->get_implementation(),
  115. native_serial_port, ec);
  116. asio::detail::throw_error(ec, "assign");
  117. }
  118. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  119. /// Move-construct a basic_serial_port from another.
  120. /**
  121. * This constructor moves a serial port from one object to another.
  122. *
  123. * @param other The other basic_serial_port object from which the move will
  124. * occur.
  125. *
  126. * @note Following the move, the moved-from object is in the same state as if
  127. * constructed using the @c basic_serial_port(io_context&) constructor.
  128. */
  129. basic_serial_port(basic_serial_port&& other)
  130. : basic_io_object<SerialPortService>(
  131. ASIO_MOVE_CAST(basic_serial_port)(other))
  132. {
  133. }
  134. /// Move-assign a basic_serial_port from another.
  135. /**
  136. * This assignment operator moves a serial port from one object to another.
  137. *
  138. * @param other The other basic_serial_port object from which the move will
  139. * occur.
  140. *
  141. * @note Following the move, the moved-from object is in the same state as if
  142. * constructed using the @c basic_serial_port(io_context&) constructor.
  143. */
  144. basic_serial_port& operator=(basic_serial_port&& other)
  145. {
  146. basic_io_object<SerialPortService>::operator=(
  147. ASIO_MOVE_CAST(basic_serial_port)(other));
  148. return *this;
  149. }
  150. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  151. /// Get a reference to the lowest layer.
  152. /**
  153. * This function returns a reference to the lowest layer in a stack of
  154. * layers. Since a basic_serial_port cannot contain any further layers, it
  155. * simply returns a reference to itself.
  156. *
  157. * @return A reference to the lowest layer in the stack of layers. Ownership
  158. * is not transferred to the caller.
  159. */
  160. lowest_layer_type& lowest_layer()
  161. {
  162. return *this;
  163. }
  164. /// Get a const reference to the lowest layer.
  165. /**
  166. * This function returns a const reference to the lowest layer in a stack of
  167. * layers. Since a basic_serial_port cannot contain any further layers, it
  168. * simply returns a reference to itself.
  169. *
  170. * @return A const reference to the lowest layer in the stack of layers.
  171. * Ownership is not transferred to the caller.
  172. */
  173. const lowest_layer_type& lowest_layer() const
  174. {
  175. return *this;
  176. }
  177. /// Open the serial port using the specified device name.
  178. /**
  179. * This function opens the serial port for the specified device name.
  180. *
  181. * @param device The platform-specific device name.
  182. *
  183. * @throws asio::system_error Thrown on failure.
  184. */
  185. void open(const std::string& device)
  186. {
  187. asio::error_code ec;
  188. this->get_service().open(this->get_implementation(), device, ec);
  189. asio::detail::throw_error(ec, "open");
  190. }
  191. /// Open the serial port using the specified device name.
  192. /**
  193. * This function opens the serial port using the given platform-specific
  194. * device name.
  195. *
  196. * @param device The platform-specific device name.
  197. *
  198. * @param ec Set the indicate what error occurred, if any.
  199. */
  200. ASIO_SYNC_OP_VOID open(const std::string& device,
  201. asio::error_code& ec)
  202. {
  203. this->get_service().open(this->get_implementation(), device, ec);
  204. ASIO_SYNC_OP_VOID_RETURN(ec);
  205. }
  206. /// Assign an existing native serial port to the serial port.
  207. /*
  208. * This function opens the serial port to hold an existing native serial port.
  209. *
  210. * @param native_serial_port A native serial port.
  211. *
  212. * @throws asio::system_error Thrown on failure.
  213. */
  214. void assign(const native_handle_type& native_serial_port)
  215. {
  216. asio::error_code ec;
  217. this->get_service().assign(this->get_implementation(),
  218. native_serial_port, ec);
  219. asio::detail::throw_error(ec, "assign");
  220. }
  221. /// Assign an existing native serial port to the serial port.
  222. /*
  223. * This function opens the serial port to hold an existing native serial port.
  224. *
  225. * @param native_serial_port A native serial port.
  226. *
  227. * @param ec Set to indicate what error occurred, if any.
  228. */
  229. ASIO_SYNC_OP_VOID assign(const native_handle_type& native_serial_port,
  230. asio::error_code& ec)
  231. {
  232. this->get_service().assign(this->get_implementation(),
  233. native_serial_port, ec);
  234. ASIO_SYNC_OP_VOID_RETURN(ec);
  235. }
  236. /// Determine whether the serial port is open.
  237. bool is_open() const
  238. {
  239. return this->get_service().is_open(this->get_implementation());
  240. }
  241. /// Close the serial port.
  242. /**
  243. * This function is used to close the serial port. Any asynchronous read or
  244. * write operations will be cancelled immediately, and will complete with the
  245. * asio::error::operation_aborted error.
  246. *
  247. * @throws asio::system_error Thrown on failure.
  248. */
  249. void close()
  250. {
  251. asio::error_code ec;
  252. this->get_service().close(this->get_implementation(), ec);
  253. asio::detail::throw_error(ec, "close");
  254. }
  255. /// Close the serial port.
  256. /**
  257. * This function is used to close the serial port. Any asynchronous read or
  258. * write operations will be cancelled immediately, and will complete with the
  259. * asio::error::operation_aborted error.
  260. *
  261. * @param ec Set to indicate what error occurred, if any.
  262. */
  263. ASIO_SYNC_OP_VOID close(asio::error_code& ec)
  264. {
  265. this->get_service().close(this->get_implementation(), ec);
  266. ASIO_SYNC_OP_VOID_RETURN(ec);
  267. }
  268. /// Get the native serial port representation.
  269. /**
  270. * This function may be used to obtain the underlying representation of the
  271. * serial port. This is intended to allow access to native serial port
  272. * functionality that is not otherwise provided.
  273. */
  274. native_handle_type native_handle()
  275. {
  276. return this->get_service().native_handle(this->get_implementation());
  277. }
  278. /// Cancel all asynchronous operations associated with the serial port.
  279. /**
  280. * This function causes all outstanding asynchronous read or write operations
  281. * to finish immediately, and the handlers for cancelled operations will be
  282. * passed the asio::error::operation_aborted error.
  283. *
  284. * @throws asio::system_error Thrown on failure.
  285. */
  286. void cancel()
  287. {
  288. asio::error_code ec;
  289. this->get_service().cancel(this->get_implementation(), ec);
  290. asio::detail::throw_error(ec, "cancel");
  291. }
  292. /// Cancel all asynchronous operations associated with the serial port.
  293. /**
  294. * This function causes all outstanding asynchronous read or write operations
  295. * to finish immediately, and the handlers for cancelled operations will be
  296. * passed the asio::error::operation_aborted error.
  297. *
  298. * @param ec Set to indicate what error occurred, if any.
  299. */
  300. ASIO_SYNC_OP_VOID cancel(asio::error_code& ec)
  301. {
  302. this->get_service().cancel(this->get_implementation(), ec);
  303. ASIO_SYNC_OP_VOID_RETURN(ec);
  304. }
  305. /// Send a break sequence to the serial port.
  306. /**
  307. * This function causes a break sequence of platform-specific duration to be
  308. * sent out the serial port.
  309. *
  310. * @throws asio::system_error Thrown on failure.
  311. */
  312. void send_break()
  313. {
  314. asio::error_code ec;
  315. this->get_service().send_break(this->get_implementation(), ec);
  316. asio::detail::throw_error(ec, "send_break");
  317. }
  318. /// Send a break sequence to the serial port.
  319. /**
  320. * This function causes a break sequence of platform-specific duration to be
  321. * sent out the serial port.
  322. *
  323. * @param ec Set to indicate what error occurred, if any.
  324. */
  325. ASIO_SYNC_OP_VOID send_break(asio::error_code& ec)
  326. {
  327. this->get_service().send_break(this->get_implementation(), ec);
  328. ASIO_SYNC_OP_VOID_RETURN(ec);
  329. }
  330. /// Set an option on the serial port.
  331. /**
  332. * This function is used to set an option on the serial port.
  333. *
  334. * @param option The option value to be set on the serial port.
  335. *
  336. * @throws asio::system_error Thrown on failure.
  337. *
  338. * @sa SettableSerialPortOption @n
  339. * asio::serial_port_base::baud_rate @n
  340. * asio::serial_port_base::flow_control @n
  341. * asio::serial_port_base::parity @n
  342. * asio::serial_port_base::stop_bits @n
  343. * asio::serial_port_base::character_size
  344. */
  345. template <typename SettableSerialPortOption>
  346. void set_option(const SettableSerialPortOption& option)
  347. {
  348. asio::error_code ec;
  349. this->get_service().set_option(this->get_implementation(), option, ec);
  350. asio::detail::throw_error(ec, "set_option");
  351. }
  352. /// Set an option on the serial port.
  353. /**
  354. * This function is used to set an option on the serial port.
  355. *
  356. * @param option The option value to be set on the serial port.
  357. *
  358. * @param ec Set to indicate what error occurred, if any.
  359. *
  360. * @sa SettableSerialPortOption @n
  361. * asio::serial_port_base::baud_rate @n
  362. * asio::serial_port_base::flow_control @n
  363. * asio::serial_port_base::parity @n
  364. * asio::serial_port_base::stop_bits @n
  365. * asio::serial_port_base::character_size
  366. */
  367. template <typename SettableSerialPortOption>
  368. ASIO_SYNC_OP_VOID set_option(const SettableSerialPortOption& option,
  369. asio::error_code& ec)
  370. {
  371. this->get_service().set_option(this->get_implementation(), option, ec);
  372. ASIO_SYNC_OP_VOID_RETURN(ec);
  373. }
  374. /// Get an option from the serial port.
  375. /**
  376. * This function is used to get the current value of an option on the serial
  377. * port.
  378. *
  379. * @param option The option value to be obtained from the serial port.
  380. *
  381. * @throws asio::system_error Thrown on failure.
  382. *
  383. * @sa GettableSerialPortOption @n
  384. * asio::serial_port_base::baud_rate @n
  385. * asio::serial_port_base::flow_control @n
  386. * asio::serial_port_base::parity @n
  387. * asio::serial_port_base::stop_bits @n
  388. * asio::serial_port_base::character_size
  389. */
  390. template <typename GettableSerialPortOption>
  391. void get_option(GettableSerialPortOption& option)
  392. {
  393. asio::error_code ec;
  394. this->get_service().get_option(this->get_implementation(), option, ec);
  395. asio::detail::throw_error(ec, "get_option");
  396. }
  397. /// Get an option from the serial port.
  398. /**
  399. * This function is used to get the current value of an option on the serial
  400. * port.
  401. *
  402. * @param option The option value to be obtained from the serial port.
  403. *
  404. * @param ec Set to indicate what error occurred, if any.
  405. *
  406. * @sa GettableSerialPortOption @n
  407. * asio::serial_port_base::baud_rate @n
  408. * asio::serial_port_base::flow_control @n
  409. * asio::serial_port_base::parity @n
  410. * asio::serial_port_base::stop_bits @n
  411. * asio::serial_port_base::character_size
  412. */
  413. template <typename GettableSerialPortOption>
  414. ASIO_SYNC_OP_VOID get_option(GettableSerialPortOption& option,
  415. asio::error_code& ec)
  416. {
  417. this->get_service().get_option(this->get_implementation(), option, ec);
  418. ASIO_SYNC_OP_VOID_RETURN(ec);
  419. }
  420. /// Write some data to the serial port.
  421. /**
  422. * This function is used to write data to the serial port. The function call
  423. * will block until one or more bytes of the data has been written
  424. * successfully, or until an error occurs.
  425. *
  426. * @param buffers One or more data buffers to be written to the serial port.
  427. *
  428. * @returns The number of bytes written.
  429. *
  430. * @throws asio::system_error Thrown on failure. An error code of
  431. * asio::error::eof indicates that the connection was closed by the
  432. * peer.
  433. *
  434. * @note The write_some operation may not transmit all of the data to the
  435. * peer. Consider using the @ref write function if you need to ensure that
  436. * all data is written before the blocking operation completes.
  437. *
  438. * @par Example
  439. * To write a single data buffer use the @ref buffer function as follows:
  440. * @code
  441. * serial_port.write_some(asio::buffer(data, size));
  442. * @endcode
  443. * See the @ref buffer documentation for information on writing multiple
  444. * buffers in one go, and how to use it with arrays, boost::array or
  445. * std::vector.
  446. */
  447. template <typename ConstBufferSequence>
  448. std::size_t write_some(const ConstBufferSequence& buffers)
  449. {
  450. asio::error_code ec;
  451. std::size_t s = this->get_service().write_some(
  452. this->get_implementation(), buffers, ec);
  453. asio::detail::throw_error(ec, "write_some");
  454. return s;
  455. }
  456. /// Write some data to the serial port.
  457. /**
  458. * This function is used to write data to the serial port. The function call
  459. * will block until one or more bytes of the data has been written
  460. * successfully, or until an error occurs.
  461. *
  462. * @param buffers One or more data buffers to be written to the serial port.
  463. *
  464. * @param ec Set to indicate what error occurred, if any.
  465. *
  466. * @returns The number of bytes written. Returns 0 if an error occurred.
  467. *
  468. * @note The write_some operation may not transmit all of the data to the
  469. * peer. Consider using the @ref write function if you need to ensure that
  470. * all data is written before the blocking operation completes.
  471. */
  472. template <typename ConstBufferSequence>
  473. std::size_t write_some(const ConstBufferSequence& buffers,
  474. asio::error_code& ec)
  475. {
  476. return this->get_service().write_some(
  477. this->get_implementation(), buffers, ec);
  478. }
  479. /// Start an asynchronous write.
  480. /**
  481. * This function is used to asynchronously write data to the serial port.
  482. * The function call always returns immediately.
  483. *
  484. * @param buffers One or more data buffers to be written to the serial port.
  485. * Although the buffers object may be copied as necessary, ownership of the
  486. * underlying memory blocks is retained by the caller, which must guarantee
  487. * that they remain valid until the handler is called.
  488. *
  489. * @param handler The handler to be called when the write operation completes.
  490. * Copies will be made of the handler as required. The function signature of
  491. * the handler must be:
  492. * @code void handler(
  493. * const asio::error_code& error, // Result of operation.
  494. * std::size_t bytes_transferred // Number of bytes written.
  495. * ); @endcode
  496. * Regardless of whether the asynchronous operation completes immediately or
  497. * not, the handler will not be invoked from within this function. Invocation
  498. * of the handler will be performed in a manner equivalent to using
  499. * asio::io_context::post().
  500. *
  501. * @note The write operation may not transmit all of the data to the peer.
  502. * Consider using the @ref async_write function if you need to ensure that all
  503. * data is written before the asynchronous operation completes.
  504. *
  505. * @par Example
  506. * To write a single data buffer use the @ref buffer function as follows:
  507. * @code
  508. * serial_port.async_write_some(asio::buffer(data, size), handler);
  509. * @endcode
  510. * See the @ref buffer documentation for information on writing multiple
  511. * buffers in one go, and how to use it with arrays, boost::array or
  512. * std::vector.
  513. */
  514. template <typename ConstBufferSequence, typename WriteHandler>
  515. ASIO_INITFN_RESULT_TYPE(WriteHandler,
  516. void (asio::error_code, std::size_t))
  517. async_write_some(const ConstBufferSequence& buffers,
  518. ASIO_MOVE_ARG(WriteHandler) handler)
  519. {
  520. // If you get an error on the following line it means that your handler does
  521. // not meet the documented type requirements for a WriteHandler.
  522. ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  523. return this->get_service().async_write_some(this->get_implementation(),
  524. buffers, ASIO_MOVE_CAST(WriteHandler)(handler));
  525. }
  526. /// Read some data from the serial port.
  527. /**
  528. * This function is used to read data from the serial port. The function
  529. * call will block until one or more bytes of data has been read successfully,
  530. * or until an error occurs.
  531. *
  532. * @param buffers One or more buffers into which the data will be read.
  533. *
  534. * @returns The number of bytes read.
  535. *
  536. * @throws asio::system_error Thrown on failure. An error code of
  537. * asio::error::eof indicates that the connection was closed by the
  538. * peer.
  539. *
  540. * @note The read_some operation may not read all of the requested number of
  541. * bytes. Consider using the @ref read function if you need to ensure that
  542. * the requested amount of data is read before the blocking operation
  543. * completes.
  544. *
  545. * @par Example
  546. * To read into a single data buffer use the @ref buffer function as follows:
  547. * @code
  548. * serial_port.read_some(asio::buffer(data, size));
  549. * @endcode
  550. * See the @ref buffer documentation for information on reading into multiple
  551. * buffers in one go, and how to use it with arrays, boost::array or
  552. * std::vector.
  553. */
  554. template <typename MutableBufferSequence>
  555. std::size_t read_some(const MutableBufferSequence& buffers)
  556. {
  557. asio::error_code ec;
  558. std::size_t s = this->get_service().read_some(
  559. this->get_implementation(), buffers, ec);
  560. asio::detail::throw_error(ec, "read_some");
  561. return s;
  562. }
  563. /// Read some data from the serial port.
  564. /**
  565. * This function is used to read data from the serial port. The function
  566. * call will block until one or more bytes of data has been read successfully,
  567. * or until an error occurs.
  568. *
  569. * @param buffers One or more buffers into which the data will be read.
  570. *
  571. * @param ec Set to indicate what error occurred, if any.
  572. *
  573. * @returns The number of bytes read. Returns 0 if an error occurred.
  574. *
  575. * @note The read_some operation may not read all of the requested number of
  576. * bytes. Consider using the @ref read function if you need to ensure that
  577. * the requested amount of data is read before the blocking operation
  578. * completes.
  579. */
  580. template <typename MutableBufferSequence>
  581. std::size_t read_some(const MutableBufferSequence& buffers,
  582. asio::error_code& ec)
  583. {
  584. return this->get_service().read_some(
  585. this->get_implementation(), buffers, ec);
  586. }
  587. /// Start an asynchronous read.
  588. /**
  589. * This function is used to asynchronously read data from the serial port.
  590. * The function call always returns immediately.
  591. *
  592. * @param buffers One or more buffers into which the data will be read.
  593. * Although the buffers object may be copied as necessary, ownership of the
  594. * underlying memory blocks is retained by the caller, which must guarantee
  595. * that they remain valid until the handler is called.
  596. *
  597. * @param handler The handler to be called when the read operation completes.
  598. * Copies will be made of the handler as required. The function signature of
  599. * the handler must be:
  600. * @code void handler(
  601. * const asio::error_code& error, // Result of operation.
  602. * std::size_t bytes_transferred // Number of bytes read.
  603. * ); @endcode
  604. * Regardless of whether the asynchronous operation completes immediately or
  605. * not, the handler will not be invoked from within this function. Invocation
  606. * of the handler will be performed in a manner equivalent to using
  607. * asio::io_context::post().
  608. *
  609. * @note The read operation may not read all of the requested number of bytes.
  610. * Consider using the @ref async_read function if you need to ensure that the
  611. * requested amount of data is read before the asynchronous operation
  612. * completes.
  613. *
  614. * @par Example
  615. * To read into a single data buffer use the @ref buffer function as follows:
  616. * @code
  617. * serial_port.async_read_some(asio::buffer(data, size), handler);
  618. * @endcode
  619. * See the @ref buffer documentation for information on reading into multiple
  620. * buffers in one go, and how to use it with arrays, boost::array or
  621. * std::vector.
  622. */
  623. template <typename MutableBufferSequence, typename ReadHandler>
  624. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  625. void (asio::error_code, std::size_t))
  626. async_read_some(const MutableBufferSequence& buffers,
  627. ASIO_MOVE_ARG(ReadHandler) handler)
  628. {
  629. // If you get an error on the following line it means that your handler does
  630. // not meet the documented type requirements for a ReadHandler.
  631. ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  632. return this->get_service().async_read_some(this->get_implementation(),
  633. buffers, ASIO_MOVE_CAST(ReadHandler)(handler));
  634. }
  635. };
  636. } // namespace asio
  637. #include "asio/detail/pop_options.hpp"
  638. #endif // defined(ASIO_HAS_SERIAL_PORT)
  639. // || defined(GENERATING_DOCUMENTATION)
  640. #endif // defined(ASIO_ENABLE_OLD_SERVICES)
  641. #endif // ASIO_BASIC_SERIAL_PORT_HPP