serial_port.hpp 27 KB

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