basic_serial_port.hpp 25 KB

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