basic_serial_port.hpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. //
  2. // basic_serial_port.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef ASIO_BASIC_SERIAL_PORT_HPP
  12. #define ASIO_BASIC_SERIAL_PORT_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include "asio/detail/config.hpp"
  17. #if defined(ASIO_HAS_SERIAL_PORT) \
  18. || defined(GENERATING_DOCUMENTATION)
  19. #include <string>
  20. #include "asio/any_io_executor.hpp"
  21. #include "asio/async_result.hpp"
  22. #include "asio/detail/handler_type_requirements.hpp"
  23. #include "asio/detail/io_object_impl.hpp"
  24. #include "asio/detail/non_const_lvalue.hpp"
  25. #include "asio/detail/throw_error.hpp"
  26. #include "asio/detail/type_traits.hpp"
  27. #include "asio/error.hpp"
  28. #include "asio/execution_context.hpp"
  29. #include "asio/serial_port_base.hpp"
  30. #if defined(ASIO_HAS_IOCP)
  31. # include "asio/detail/win_iocp_serial_port_service.hpp"
  32. #else
  33. # include "asio/detail/posix_serial_port_service.hpp"
  34. #endif
  35. #if defined(ASIO_HAS_MOVE)
  36. # include <utility>
  37. #endif // defined(ASIO_HAS_MOVE)
  38. #include "asio/detail/push_options.hpp"
  39. namespace asio {
  40. /// Provides serial port functionality.
  41. /**
  42. * The basic_serial_port class provides a wrapper over serial port
  43. * functionality.
  44. *
  45. * @par Thread Safety
  46. * @e Distinct @e objects: Safe.@n
  47. * @e Shared @e objects: Unsafe.
  48. */
  49. template <typename Executor = any_io_executor>
  50. class basic_serial_port
  51. : public serial_port_base
  52. {
  53. public:
  54. /// The type of the executor associated with the object.
  55. typedef Executor executor_type;
  56. /// Rebinds the serial port type to another executor.
  57. template <typename Executor1>
  58. struct rebind_executor
  59. {
  60. /// The serial port type when rebound to the specified executor.
  61. typedef basic_serial_port<Executor1> other;
  62. };
  63. /// The native representation of a serial port.
  64. #if defined(GENERATING_DOCUMENTATION)
  65. typedef implementation_defined native_handle_type;
  66. #elif defined(ASIO_HAS_IOCP)
  67. typedef detail::win_iocp_serial_port_service::native_handle_type
  68. native_handle_type;
  69. #else
  70. typedef detail::posix_serial_port_service::native_handle_type
  71. native_handle_type;
  72. #endif
  73. /// A basic_basic_serial_port is always the lowest layer.
  74. typedef basic_serial_port lowest_layer_type;
  75. /// Construct a basic_serial_port without opening it.
  76. /**
  77. * This constructor creates a serial port without opening it.
  78. *
  79. * @param ex The I/O executor that the serial port will use, by default, to
  80. * dispatch handlers for any asynchronous operations performed on the
  81. * serial port.
  82. */
  83. explicit basic_serial_port(const executor_type& ex)
  84. : impl_(0, ex)
  85. {
  86. }
  87. /// Construct a basic_serial_port without opening it.
  88. /**
  89. * This constructor creates a serial port without opening it.
  90. *
  91. * @param context An execution context which provides the I/O executor that
  92. * the serial port will use, by default, to dispatch handlers for any
  93. * asynchronous operations performed on the serial port.
  94. */
  95. template <typename ExecutionContext>
  96. explicit basic_serial_port(ExecutionContext& context,
  97. typename constraint<
  98. is_convertible<ExecutionContext&, execution_context&>::value,
  99. defaulted_constraint
  100. >::type = defaulted_constraint())
  101. : impl_(0, 0, context)
  102. {
  103. }
  104. /// Construct and open a basic_serial_port.
  105. /**
  106. * This constructor creates and opens a serial port for the specified device
  107. * name.
  108. *
  109. * @param ex The I/O executor that the serial port will use, by default, to
  110. * dispatch handlers for any asynchronous operations performed on the
  111. * serial port.
  112. *
  113. * @param device The platform-specific device name for this serial
  114. * port.
  115. */
  116. basic_serial_port(const executor_type& ex, const char* device)
  117. : impl_(0, ex)
  118. {
  119. asio::error_code ec;
  120. impl_.get_service().open(impl_.get_implementation(), device, ec);
  121. asio::detail::throw_error(ec, "open");
  122. }
  123. /// Construct and open a basic_serial_port.
  124. /**
  125. * This constructor creates and opens a serial port for the specified device
  126. * name.
  127. *
  128. * @param context An execution context which provides the I/O executor that
  129. * the serial port will use, by default, to dispatch handlers for any
  130. * asynchronous operations performed on the serial port.
  131. *
  132. * @param device The platform-specific device name for this serial
  133. * port.
  134. */
  135. template <typename ExecutionContext>
  136. basic_serial_port(ExecutionContext& context, const char* device,
  137. typename constraint<
  138. is_convertible<ExecutionContext&, execution_context&>::value
  139. >::type = 0)
  140. : impl_(0, 0, context)
  141. {
  142. asio::error_code ec;
  143. impl_.get_service().open(impl_.get_implementation(), device, ec);
  144. asio::detail::throw_error(ec, "open");
  145. }
  146. /// Construct and open a basic_serial_port.
  147. /**
  148. * This constructor creates and opens a serial port for the specified device
  149. * name.
  150. *
  151. * @param ex The I/O executor that the serial port will use, by default, to
  152. * dispatch handlers for any asynchronous operations performed on the
  153. * serial port.
  154. *
  155. * @param device The platform-specific device name for this serial
  156. * port.
  157. */
  158. basic_serial_port(const executor_type& ex, const std::string& device)
  159. : impl_(0, ex)
  160. {
  161. asio::error_code ec;
  162. impl_.get_service().open(impl_.get_implementation(), device, ec);
  163. asio::detail::throw_error(ec, "open");
  164. }
  165. /// Construct and open a basic_serial_port.
  166. /**
  167. * This constructor creates and opens a serial port for the specified device
  168. * name.
  169. *
  170. * @param context An execution context which provides the I/O executor that
  171. * the serial port will use, by default, to dispatch handlers for any
  172. * asynchronous operations performed on the serial port.
  173. *
  174. * @param device The platform-specific device name for this serial
  175. * port.
  176. */
  177. template <typename ExecutionContext>
  178. basic_serial_port(ExecutionContext& context, const std::string& device,
  179. typename constraint<
  180. is_convertible<ExecutionContext&, execution_context&>::value
  181. >::type = 0)
  182. : impl_(0, 0, context)
  183. {
  184. asio::error_code ec;
  185. impl_.get_service().open(impl_.get_implementation(), device, ec);
  186. asio::detail::throw_error(ec, "open");
  187. }
  188. /// Construct a basic_serial_port on an existing native serial port.
  189. /**
  190. * This constructor creates a serial port object to hold an existing native
  191. * serial port.
  192. *
  193. * @param ex The I/O executor that the serial port will use, by default, to
  194. * dispatch handlers for any asynchronous operations performed on the
  195. * serial port.
  196. *
  197. * @param native_serial_port A native serial port.
  198. *
  199. * @throws asio::system_error Thrown on failure.
  200. */
  201. basic_serial_port(const executor_type& ex,
  202. const native_handle_type& native_serial_port)
  203. : impl_(0, ex)
  204. {
  205. asio::error_code ec;
  206. impl_.get_service().assign(impl_.get_implementation(),
  207. native_serial_port, ec);
  208. asio::detail::throw_error(ec, "assign");
  209. }
  210. /// Construct a basic_serial_port on an existing native serial port.
  211. /**
  212. * This constructor creates a serial port object to hold an existing native
  213. * serial port.
  214. *
  215. * @param context An execution context which provides the I/O executor that
  216. * the serial port will use, by default, to dispatch handlers for any
  217. * asynchronous operations performed on the serial port.
  218. *
  219. * @param native_serial_port A native serial port.
  220. *
  221. * @throws asio::system_error Thrown on failure.
  222. */
  223. template <typename ExecutionContext>
  224. basic_serial_port(ExecutionContext& context,
  225. const native_handle_type& native_serial_port,
  226. typename constraint<
  227. is_convertible<ExecutionContext&, execution_context&>::value
  228. >::type = 0)
  229. : impl_(0, 0, context)
  230. {
  231. asio::error_code ec;
  232. impl_.get_service().assign(impl_.get_implementation(),
  233. native_serial_port, ec);
  234. asio::detail::throw_error(ec, "assign");
  235. }
  236. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  237. /// Move-construct a basic_serial_port from another.
  238. /**
  239. * This constructor moves a serial port from one object to another.
  240. *
  241. * @param other The other basic_serial_port object from which the move will
  242. * occur.
  243. *
  244. * @note Following the move, the moved-from object is in the same state as if
  245. * constructed using the @c basic_serial_port(const executor_type&)
  246. * constructor.
  247. */
  248. basic_serial_port(basic_serial_port&& other)
  249. : impl_(std::move(other.impl_))
  250. {
  251. }
  252. /// Move-assign a basic_serial_port from another.
  253. /**
  254. * This assignment operator moves a serial port from one object to another.
  255. *
  256. * @param other The other basic_serial_port object from which the move will
  257. * occur.
  258. *
  259. * @note Following the move, the moved-from object is in the same state as if
  260. * constructed using the @c basic_serial_port(const executor_type&)
  261. * constructor.
  262. */
  263. basic_serial_port& operator=(basic_serial_port&& other)
  264. {
  265. impl_ = std::move(other.impl_);
  266. return *this;
  267. }
  268. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  269. /// Destroys the serial port.
  270. /**
  271. * This function destroys the serial port, cancelling any outstanding
  272. * asynchronous wait operations associated with the serial port as if by
  273. * calling @c cancel.
  274. */
  275. ~basic_serial_port()
  276. {
  277. }
  278. /// Get the executor associated with the object.
  279. executor_type get_executor() ASIO_NOEXCEPT
  280. {
  281. return impl_.get_executor();
  282. }
  283. /// Get a reference to the lowest layer.
  284. /**
  285. * This function returns a reference to the lowest layer in a stack of
  286. * layers. Since a basic_serial_port cannot contain any further layers, it
  287. * simply returns a reference to itself.
  288. *
  289. * @return A reference to the lowest layer in the stack of layers. Ownership
  290. * is not transferred to the caller.
  291. */
  292. lowest_layer_type& lowest_layer()
  293. {
  294. return *this;
  295. }
  296. /// Get a const reference to the lowest layer.
  297. /**
  298. * This function returns a const reference to the lowest layer in a stack of
  299. * layers. Since a basic_serial_port cannot contain any further layers, it
  300. * simply returns a reference to itself.
  301. *
  302. * @return A const reference to the lowest layer in the stack of layers.
  303. * Ownership is not transferred to the caller.
  304. */
  305. const lowest_layer_type& lowest_layer() const
  306. {
  307. return *this;
  308. }
  309. /// Open the serial port using the specified device name.
  310. /**
  311. * This function opens the serial port for the specified device name.
  312. *
  313. * @param device The platform-specific device name.
  314. *
  315. * @throws asio::system_error Thrown on failure.
  316. */
  317. void open(const std::string& device)
  318. {
  319. asio::error_code ec;
  320. impl_.get_service().open(impl_.get_implementation(), device, ec);
  321. asio::detail::throw_error(ec, "open");
  322. }
  323. /// Open the serial port using the specified device name.
  324. /**
  325. * This function opens the serial port using the given platform-specific
  326. * device name.
  327. *
  328. * @param device The platform-specific device name.
  329. *
  330. * @param ec Set the indicate what error occurred, if any.
  331. */
  332. ASIO_SYNC_OP_VOID open(const std::string& device,
  333. asio::error_code& ec)
  334. {
  335. impl_.get_service().open(impl_.get_implementation(), device, ec);
  336. ASIO_SYNC_OP_VOID_RETURN(ec);
  337. }
  338. /// Assign an existing native serial port to the serial port.
  339. /*
  340. * This function opens the serial port to hold an existing native serial port.
  341. *
  342. * @param native_serial_port A native serial port.
  343. *
  344. * @throws asio::system_error Thrown on failure.
  345. */
  346. void assign(const native_handle_type& native_serial_port)
  347. {
  348. asio::error_code ec;
  349. impl_.get_service().assign(impl_.get_implementation(),
  350. native_serial_port, ec);
  351. asio::detail::throw_error(ec, "assign");
  352. }
  353. /// Assign an existing native serial port to the serial port.
  354. /*
  355. * This function opens the serial port to hold an existing native serial port.
  356. *
  357. * @param native_serial_port A native serial port.
  358. *
  359. * @param ec Set to indicate what error occurred, if any.
  360. */
  361. ASIO_SYNC_OP_VOID assign(const native_handle_type& native_serial_port,
  362. asio::error_code& ec)
  363. {
  364. impl_.get_service().assign(impl_.get_implementation(),
  365. native_serial_port, ec);
  366. ASIO_SYNC_OP_VOID_RETURN(ec);
  367. }
  368. /// Determine whether the serial port is open.
  369. bool is_open() const
  370. {
  371. return impl_.get_service().is_open(impl_.get_implementation());
  372. }
  373. /// Close the serial port.
  374. /**
  375. * This function is used to close the serial port. Any asynchronous read or
  376. * write operations will be cancelled immediately, and will complete with the
  377. * asio::error::operation_aborted error.
  378. *
  379. * @throws asio::system_error Thrown on failure.
  380. */
  381. void close()
  382. {
  383. asio::error_code ec;
  384. impl_.get_service().close(impl_.get_implementation(), ec);
  385. asio::detail::throw_error(ec, "close");
  386. }
  387. /// Close the serial port.
  388. /**
  389. * This function is used to close the serial port. Any asynchronous read or
  390. * write operations will be cancelled immediately, and will complete with the
  391. * asio::error::operation_aborted error.
  392. *
  393. * @param ec Set to indicate what error occurred, if any.
  394. */
  395. ASIO_SYNC_OP_VOID close(asio::error_code& ec)
  396. {
  397. impl_.get_service().close(impl_.get_implementation(), ec);
  398. ASIO_SYNC_OP_VOID_RETURN(ec);
  399. }
  400. /// Get the native serial port representation.
  401. /**
  402. * This function may be used to obtain the underlying representation of the
  403. * serial port. This is intended to allow access to native serial port
  404. * functionality that is not otherwise provided.
  405. */
  406. native_handle_type native_handle()
  407. {
  408. return impl_.get_service().native_handle(impl_.get_implementation());
  409. }
  410. /// Cancel all asynchronous operations associated with the serial port.
  411. /**
  412. * This function causes all outstanding asynchronous read or write operations
  413. * to finish immediately, and the handlers for cancelled operations will be
  414. * passed the asio::error::operation_aborted error.
  415. *
  416. * @throws asio::system_error Thrown on failure.
  417. */
  418. void cancel()
  419. {
  420. asio::error_code ec;
  421. impl_.get_service().cancel(impl_.get_implementation(), ec);
  422. asio::detail::throw_error(ec, "cancel");
  423. }
  424. /// Cancel all asynchronous operations associated with the serial port.
  425. /**
  426. * This function causes all outstanding asynchronous read or write operations
  427. * to finish immediately, and the handlers for cancelled operations will be
  428. * passed the asio::error::operation_aborted error.
  429. *
  430. * @param ec Set to indicate what error occurred, if any.
  431. */
  432. ASIO_SYNC_OP_VOID cancel(asio::error_code& ec)
  433. {
  434. impl_.get_service().cancel(impl_.get_implementation(), ec);
  435. ASIO_SYNC_OP_VOID_RETURN(ec);
  436. }
  437. /// Send a break sequence to the serial port.
  438. /**
  439. * This function causes a break sequence of platform-specific duration to be
  440. * sent out the serial port.
  441. *
  442. * @throws asio::system_error Thrown on failure.
  443. */
  444. void send_break()
  445. {
  446. asio::error_code ec;
  447. impl_.get_service().send_break(impl_.get_implementation(), ec);
  448. asio::detail::throw_error(ec, "send_break");
  449. }
  450. /// Send a break sequence to the serial port.
  451. /**
  452. * This function causes a break sequence of platform-specific duration to be
  453. * sent out the serial port.
  454. *
  455. * @param ec Set to indicate what error occurred, if any.
  456. */
  457. ASIO_SYNC_OP_VOID send_break(asio::error_code& ec)
  458. {
  459. impl_.get_service().send_break(impl_.get_implementation(), ec);
  460. ASIO_SYNC_OP_VOID_RETURN(ec);
  461. }
  462. /// Set an option on the serial port.
  463. /**
  464. * This function is used to set an option on the serial port.
  465. *
  466. * @param option The option value to be set on the serial port.
  467. *
  468. * @throws asio::system_error Thrown on failure.
  469. *
  470. * @sa SettableSerialPortOption @n
  471. * asio::serial_port_base::baud_rate @n
  472. * asio::serial_port_base::flow_control @n
  473. * asio::serial_port_base::parity @n
  474. * asio::serial_port_base::stop_bits @n
  475. * asio::serial_port_base::character_size
  476. */
  477. template <typename SettableSerialPortOption>
  478. void set_option(const SettableSerialPortOption& option)
  479. {
  480. asio::error_code ec;
  481. impl_.get_service().set_option(impl_.get_implementation(), option, ec);
  482. asio::detail::throw_error(ec, "set_option");
  483. }
  484. /// Set an option on the serial port.
  485. /**
  486. * This function is used to set an option on the serial port.
  487. *
  488. * @param option The option value to be set on the serial port.
  489. *
  490. * @param ec Set to indicate what error occurred, if any.
  491. *
  492. * @sa SettableSerialPortOption @n
  493. * asio::serial_port_base::baud_rate @n
  494. * asio::serial_port_base::flow_control @n
  495. * asio::serial_port_base::parity @n
  496. * asio::serial_port_base::stop_bits @n
  497. * asio::serial_port_base::character_size
  498. */
  499. template <typename SettableSerialPortOption>
  500. ASIO_SYNC_OP_VOID set_option(const SettableSerialPortOption& option,
  501. asio::error_code& ec)
  502. {
  503. impl_.get_service().set_option(impl_.get_implementation(), option, ec);
  504. ASIO_SYNC_OP_VOID_RETURN(ec);
  505. }
  506. /// Get an option from the serial port.
  507. /**
  508. * This function is used to get the current value of an option on the serial
  509. * port.
  510. *
  511. * @param option The option value to be obtained from the serial port.
  512. *
  513. * @throws asio::system_error Thrown on failure.
  514. *
  515. * @sa GettableSerialPortOption @n
  516. * asio::serial_port_base::baud_rate @n
  517. * asio::serial_port_base::flow_control @n
  518. * asio::serial_port_base::parity @n
  519. * asio::serial_port_base::stop_bits @n
  520. * asio::serial_port_base::character_size
  521. */
  522. template <typename GettableSerialPortOption>
  523. void get_option(GettableSerialPortOption& option) const
  524. {
  525. asio::error_code ec;
  526. impl_.get_service().get_option(impl_.get_implementation(), option, ec);
  527. asio::detail::throw_error(ec, "get_option");
  528. }
  529. /// Get an option from the serial port.
  530. /**
  531. * This function is used to get the current value of an option on the serial
  532. * port.
  533. *
  534. * @param option The option value to be obtained from the serial port.
  535. *
  536. * @param ec Set to indicate what error occurred, if any.
  537. *
  538. * @sa GettableSerialPortOption @n
  539. * asio::serial_port_base::baud_rate @n
  540. * asio::serial_port_base::flow_control @n
  541. * asio::serial_port_base::parity @n
  542. * asio::serial_port_base::stop_bits @n
  543. * asio::serial_port_base::character_size
  544. */
  545. template <typename GettableSerialPortOption>
  546. ASIO_SYNC_OP_VOID get_option(GettableSerialPortOption& option,
  547. asio::error_code& ec) const
  548. {
  549. impl_.get_service().get_option(impl_.get_implementation(), option, ec);
  550. ASIO_SYNC_OP_VOID_RETURN(ec);
  551. }
  552. /// Write some data to the serial port.
  553. /**
  554. * This function is used to write data to the serial port. The function call
  555. * will block until one or more bytes of the data has been written
  556. * successfully, or until an error occurs.
  557. *
  558. * @param buffers One or more data buffers to be written to the serial port.
  559. *
  560. * @returns The number of bytes written.
  561. *
  562. * @throws asio::system_error Thrown on failure. An error code of
  563. * asio::error::eof indicates that the connection was closed by the
  564. * peer.
  565. *
  566. * @note The write_some operation may not transmit all of the data to the
  567. * peer. Consider using the @ref write function if you need to ensure that
  568. * all data is written before the blocking operation completes.
  569. *
  570. * @par Example
  571. * To write a single data buffer use the @ref buffer function as follows:
  572. * @code
  573. * basic_serial_port.write_some(asio::buffer(data, size));
  574. * @endcode
  575. * See the @ref buffer documentation for information on writing multiple
  576. * buffers in one go, and how to use it with arrays, boost::array or
  577. * std::vector.
  578. */
  579. template <typename ConstBufferSequence>
  580. std::size_t write_some(const ConstBufferSequence& buffers)
  581. {
  582. asio::error_code ec;
  583. std::size_t s = impl_.get_service().write_some(
  584. impl_.get_implementation(), buffers, ec);
  585. asio::detail::throw_error(ec, "write_some");
  586. return s;
  587. }
  588. /// Write some data to the serial port.
  589. /**
  590. * This function is used to write data to the serial port. The function call
  591. * will block until one or more bytes of the data has been written
  592. * successfully, or until an error occurs.
  593. *
  594. * @param buffers One or more data buffers to be written to the serial port.
  595. *
  596. * @param ec Set to indicate what error occurred, if any.
  597. *
  598. * @returns The number of bytes written. Returns 0 if an error occurred.
  599. *
  600. * @note The write_some operation may not transmit all of the data to the
  601. * peer. Consider using the @ref write function if you need to ensure that
  602. * all data is written before the blocking operation completes.
  603. */
  604. template <typename ConstBufferSequence>
  605. std::size_t write_some(const ConstBufferSequence& buffers,
  606. asio::error_code& ec)
  607. {
  608. return impl_.get_service().write_some(
  609. impl_.get_implementation(), buffers, ec);
  610. }
  611. /// Start an asynchronous write.
  612. /**
  613. * This function is used to asynchronously write data to the serial port.
  614. * It is an initiating function for an @ref asynchronous_operation, and always
  615. * returns immediately.
  616. *
  617. * @param buffers One or more data buffers to be written to the serial port.
  618. * Although the buffers object may be copied as necessary, ownership of the
  619. * underlying memory blocks is retained by the caller, which must guarantee
  620. * that they remain valid until the completion handler is called.
  621. *
  622. * @param token The @ref completion_token that will be used to produce a
  623. * completion handler, which will be called when the write completes.
  624. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  625. * @ref yield_context, or a function object with the correct completion
  626. * signature. The function signature of the completion handler must be:
  627. * @code void handler(
  628. * const asio::error_code& error, // Result of operation.
  629. * std::size_t bytes_transferred // Number of bytes written.
  630. * ); @endcode
  631. * Regardless of whether the asynchronous operation completes immediately or
  632. * not, the completion handler will not be invoked from within this function.
  633. * On immediate completion, invocation of the handler will be performed in a
  634. * manner equivalent to using asio::post().
  635. *
  636. * @par Completion Signature
  637. * @code void(asio::error_code, std::size_t) @endcode
  638. *
  639. * @note The write operation may not transmit all of the data to the peer.
  640. * Consider using the @ref async_write function if you need to ensure that all
  641. * data is written before the asynchronous operation completes.
  642. *
  643. * @par Example
  644. * To write a single data buffer use the @ref buffer function as follows:
  645. * @code
  646. * basic_serial_port.async_write_some(
  647. * asio::buffer(data, size), handler);
  648. * @endcode
  649. * See the @ref buffer documentation for information on writing multiple
  650. * buffers in one go, and how to use it with arrays, boost::array or
  651. * std::vector.
  652. *
  653. * @par Per-Operation Cancellation
  654. * On POSIX or Windows operating systems, this asynchronous operation supports
  655. * cancellation for the following asio::cancellation_type values:
  656. *
  657. * @li @c cancellation_type::terminal
  658. *
  659. * @li @c cancellation_type::partial
  660. *
  661. * @li @c cancellation_type::total
  662. */
  663. template <typename ConstBufferSequence,
  664. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  665. std::size_t)) WriteToken
  666. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  667. ASIO_INITFN_AUTO_RESULT_TYPE(WriteToken,
  668. void (asio::error_code, std::size_t))
  669. async_write_some(const ConstBufferSequence& buffers,
  670. ASIO_MOVE_ARG(WriteToken) token
  671. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  672. {
  673. return async_initiate<WriteToken,
  674. void (asio::error_code, std::size_t)>(
  675. initiate_async_write_some(this), token, buffers);
  676. }
  677. /// Read some data from the serial port.
  678. /**
  679. * This function is used to read data from the serial port. The function
  680. * call will block until one or more bytes of data has been read successfully,
  681. * or until an error occurs.
  682. *
  683. * @param buffers One or more buffers into which the data will be read.
  684. *
  685. * @returns The number of bytes read.
  686. *
  687. * @throws asio::system_error Thrown on failure. An error code of
  688. * asio::error::eof indicates that the connection was closed by the
  689. * peer.
  690. *
  691. * @note The read_some operation may not read all of the requested number of
  692. * bytes. Consider using the @ref read function if you need to ensure that
  693. * the requested amount of data is read before the blocking operation
  694. * completes.
  695. *
  696. * @par Example
  697. * To read into a single data buffer use the @ref buffer function as follows:
  698. * @code
  699. * basic_serial_port.read_some(asio::buffer(data, size));
  700. * @endcode
  701. * See the @ref buffer documentation for information on reading into multiple
  702. * buffers in one go, and how to use it with arrays, boost::array or
  703. * std::vector.
  704. */
  705. template <typename MutableBufferSequence>
  706. std::size_t read_some(const MutableBufferSequence& buffers)
  707. {
  708. asio::error_code ec;
  709. std::size_t s = impl_.get_service().read_some(
  710. impl_.get_implementation(), buffers, ec);
  711. asio::detail::throw_error(ec, "read_some");
  712. return s;
  713. }
  714. /// Read some data from the serial port.
  715. /**
  716. * This function is used to read data from the serial port. The function
  717. * call will block until one or more bytes of data has been read successfully,
  718. * or until an error occurs.
  719. *
  720. * @param buffers One or more buffers into which the data will be read.
  721. *
  722. * @param ec Set to indicate what error occurred, if any.
  723. *
  724. * @returns The number of bytes read. Returns 0 if an error occurred.
  725. *
  726. * @note The read_some operation may not read all of the requested number of
  727. * bytes. Consider using the @ref read function if you need to ensure that
  728. * the requested amount of data is read before the blocking operation
  729. * completes.
  730. */
  731. template <typename MutableBufferSequence>
  732. std::size_t read_some(const MutableBufferSequence& buffers,
  733. asio::error_code& ec)
  734. {
  735. return impl_.get_service().read_some(
  736. impl_.get_implementation(), buffers, ec);
  737. }
  738. /// Start an asynchronous read.
  739. /**
  740. * This function is used to asynchronously read data from the serial port.
  741. * It is an initiating function for an @ref asynchronous_operation, and always
  742. * returns immediately.
  743. *
  744. * @param buffers One or more buffers into which the data will be read.
  745. * Although the buffers object may be copied as necessary, ownership of the
  746. * underlying memory blocks is retained by the caller, which must guarantee
  747. * that they remain valid until the completion handler is called.
  748. *
  749. * @param token The @ref completion_token that will be used to produce a
  750. * completion handler, which will be called when the read completes.
  751. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  752. * @ref yield_context, or a function object with the correct completion
  753. * signature. The function signature of the completion handler must be:
  754. * @code void handler(
  755. * const asio::error_code& error, // Result of operation.
  756. * std::size_t bytes_transferred // Number of bytes read.
  757. * ); @endcode
  758. * Regardless of whether the asynchronous operation completes immediately or
  759. * not, the completion handler will not be invoked from within this function.
  760. * On immediate completion, invocation of the handler will be performed in a
  761. * manner equivalent to using asio::post().
  762. *
  763. * @par Completion Signature
  764. * @code void(asio::error_code, std::size_t) @endcode
  765. *
  766. * @note The read operation may not read all of the requested number of bytes.
  767. * Consider using the @ref async_read function if you need to ensure that the
  768. * requested amount of data is read before the asynchronous operation
  769. * completes.
  770. *
  771. * @par Example
  772. * To read into a single data buffer use the @ref buffer function as follows:
  773. * @code
  774. * basic_serial_port.async_read_some(
  775. * asio::buffer(data, size), handler);
  776. * @endcode
  777. * See the @ref buffer documentation for information on reading into multiple
  778. * buffers in one go, and how to use it with arrays, boost::array or
  779. * std::vector.
  780. *
  781. * @par Per-Operation Cancellation
  782. * On POSIX or Windows operating systems, this asynchronous operation supports
  783. * cancellation for the following asio::cancellation_type values:
  784. *
  785. * @li @c cancellation_type::terminal
  786. *
  787. * @li @c cancellation_type::partial
  788. *
  789. * @li @c cancellation_type::total
  790. */
  791. template <typename MutableBufferSequence,
  792. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  793. std::size_t)) ReadToken
  794. ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  795. ASIO_INITFN_AUTO_RESULT_TYPE(ReadToken,
  796. void (asio::error_code, std::size_t))
  797. async_read_some(const MutableBufferSequence& buffers,
  798. ASIO_MOVE_ARG(ReadToken) token
  799. ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  800. {
  801. return async_initiate<ReadToken,
  802. void (asio::error_code, std::size_t)>(
  803. initiate_async_read_some(this), token, buffers);
  804. }
  805. private:
  806. // Disallow copying and assignment.
  807. basic_serial_port(const basic_serial_port&) ASIO_DELETED;
  808. basic_serial_port& operator=(const basic_serial_port&) ASIO_DELETED;
  809. class initiate_async_write_some
  810. {
  811. public:
  812. typedef Executor executor_type;
  813. explicit initiate_async_write_some(basic_serial_port* self)
  814. : self_(self)
  815. {
  816. }
  817. executor_type get_executor() const ASIO_NOEXCEPT
  818. {
  819. return self_->get_executor();
  820. }
  821. template <typename WriteHandler, typename ConstBufferSequence>
  822. void operator()(ASIO_MOVE_ARG(WriteHandler) handler,
  823. const ConstBufferSequence& buffers) const
  824. {
  825. // If you get an error on the following line it means that your handler
  826. // does not meet the documented type requirements for a WriteHandler.
  827. ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  828. detail::non_const_lvalue<WriteHandler> handler2(handler);
  829. self_->impl_.get_service().async_write_some(
  830. self_->impl_.get_implementation(), buffers,
  831. handler2.value, self_->impl_.get_executor());
  832. }
  833. private:
  834. basic_serial_port* self_;
  835. };
  836. class initiate_async_read_some
  837. {
  838. public:
  839. typedef Executor executor_type;
  840. explicit initiate_async_read_some(basic_serial_port* self)
  841. : self_(self)
  842. {
  843. }
  844. executor_type get_executor() const ASIO_NOEXCEPT
  845. {
  846. return self_->get_executor();
  847. }
  848. template <typename ReadHandler, typename MutableBufferSequence>
  849. void operator()(ASIO_MOVE_ARG(ReadHandler) handler,
  850. const MutableBufferSequence& buffers) const
  851. {
  852. // If you get an error on the following line it means that your handler
  853. // does not meet the documented type requirements for a ReadHandler.
  854. ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  855. detail::non_const_lvalue<ReadHandler> handler2(handler);
  856. self_->impl_.get_service().async_read_some(
  857. self_->impl_.get_implementation(), buffers,
  858. handler2.value, self_->impl_.get_executor());
  859. }
  860. private:
  861. basic_serial_port* self_;
  862. };
  863. #if defined(ASIO_HAS_IOCP)
  864. detail::io_object_impl<detail::win_iocp_serial_port_service, Executor> impl_;
  865. #else
  866. detail::io_object_impl<detail::posix_serial_port_service, Executor> impl_;
  867. #endif
  868. };
  869. } // namespace asio
  870. #include "asio/detail/pop_options.hpp"
  871. #endif // defined(ASIO_HAS_SERIAL_PORT)
  872. // || defined(GENERATING_DOCUMENTATION)
  873. #endif // ASIO_BASIC_SERIAL_PORT_HPP