basic_serial_port.hpp 25 KB

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