5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 // Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com)
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)
12 #ifndef BOOST_ASIO_SERIAL_PORT_HPP
13 #define BOOST_ASIO_SERIAL_PORT_HPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
19 #include <boost/asio/detail/config.hpp>
21 #if defined(BOOST_ASIO_HAS_SERIAL_PORT) \
22 || defined(GENERATING_DOCUMENTATION)
25 #include <boost/asio/async_result.hpp>
26 #include <boost/asio/basic_io_object.hpp>
27 #include <boost/asio/detail/handler_type_requirements.hpp>
28 #include <boost/asio/detail/throw_error.hpp>
29 #include <boost/asio/error.hpp>
30 #include <boost/asio/io_context.hpp>
31 #include <boost/asio/serial_port_base.hpp>
33 #if defined(BOOST_ASIO_HAS_MOVE)
35 #endif // defined(BOOST_ASIO_HAS_MOVE)
37 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
38 # include <boost/asio/basic_serial_port.hpp>
39 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
40 # if defined(BOOST_ASIO_HAS_IOCP)
41 # include <boost/asio/detail/win_iocp_serial_port_service.hpp>
42 # define BOOST_ASIO_SVC_T detail::win_iocp_serial_port_service
44 # include <boost/asio/detail/reactive_serial_port_service.hpp>
45 # define BOOST_ASIO_SVC_T detail::reactive_serial_port_service
47 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
49 #include <boost/asio/detail/push_options.hpp>
54 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
55 // Typedef for the typical usage of a serial port.
56 typedef basic_serial_port<> serial_port;
57 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
58 /// Provides serial port functionality.
60 * The serial_port class provides a wrapper over serial port functionality.
63 * @e Distinct @e objects: Safe.@n
64 * @e Shared @e objects: Unsafe.
67 : BOOST_ASIO_SVC_ACCESS basic_io_object<BOOST_ASIO_SVC_T>,
68 public serial_port_base
71 /// The type of the executor associated with the object.
72 typedef io_context::executor_type executor_type;
74 /// The native representation of a serial port.
75 #if defined(GENERATING_DOCUMENTATION)
76 typedef implementation_defined native_handle_type;
78 typedef BOOST_ASIO_SVC_T::native_handle_type native_handle_type;
81 /// A basic_serial_port is always the lowest layer.
82 typedef serial_port lowest_layer_type;
84 /// Construct a serial_port without opening it.
86 * This constructor creates a serial port without opening it.
88 * @param io_context The io_context object that the serial port will use to
89 * dispatch handlers for any asynchronous operations performed on the port.
91 explicit serial_port(boost::asio::io_context& io_context)
92 : basic_io_object<BOOST_ASIO_SVC_T>(io_context)
96 /// Construct and open a serial_port.
98 * This constructor creates and opens a serial port for the specified device
101 * @param io_context The io_context object that the serial port will use to
102 * dispatch handlers for any asynchronous operations performed on the port.
104 * @param device The platform-specific device name for this serial
107 explicit serial_port(boost::asio::io_context& io_context,
109 : basic_io_object<BOOST_ASIO_SVC_T>(io_context)
111 boost::system::error_code ec;
112 this->get_service().open(this->get_implementation(), device, ec);
113 boost::asio::detail::throw_error(ec, "open");
116 /// Construct and open a serial_port.
118 * This constructor creates and opens a serial port for the specified device
121 * @param io_context The io_context object that the serial port will use to
122 * dispatch handlers for any asynchronous operations performed on the port.
124 * @param device The platform-specific device name for this serial
127 explicit serial_port(boost::asio::io_context& io_context,
128 const std::string& device)
129 : basic_io_object<BOOST_ASIO_SVC_T>(io_context)
131 boost::system::error_code ec;
132 this->get_service().open(this->get_implementation(), device, ec);
133 boost::asio::detail::throw_error(ec, "open");
136 /// Construct a serial_port on an existing native serial port.
138 * This constructor creates a serial port object to hold an existing native
141 * @param io_context The io_context object that the serial port will use to
142 * dispatch handlers for any asynchronous operations performed on the port.
144 * @param native_serial_port A native serial port.
146 * @throws boost::system::system_error Thrown on failure.
148 serial_port(boost::asio::io_context& io_context,
149 const native_handle_type& native_serial_port)
150 : basic_io_object<BOOST_ASIO_SVC_T>(io_context)
152 boost::system::error_code ec;
153 this->get_service().assign(this->get_implementation(),
154 native_serial_port, ec);
155 boost::asio::detail::throw_error(ec, "assign");
158 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
159 /// Move-construct a serial_port from another.
161 * This constructor moves a serial port from one object to another.
163 * @param other The other serial_port object from which the move will
166 * @note Following the move, the moved-from object is in the same state as if
167 * constructed using the @c serial_port(io_context&) constructor.
169 serial_port(serial_port&& other)
170 : basic_io_object<BOOST_ASIO_SVC_T>(std::move(other))
174 /// Move-assign a serial_port from another.
176 * This assignment operator moves a serial port from one object to another.
178 * @param other The other serial_port object from which the move will
181 * @note Following the move, the moved-from object is in the same state as if
182 * constructed using the @c serial_port(io_context&) constructor.
184 serial_port& operator=(serial_port&& other)
186 basic_io_object<BOOST_ASIO_SVC_T>::operator=(std::move(other));
189 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
191 /// Destroys the serial port.
193 * This function destroys the serial port, cancelling any outstanding
194 * asynchronous wait operations associated with the serial port as if by
201 #if !defined(BOOST_ASIO_NO_DEPRECATED)
202 /// (Deprecated: Use get_executor().) Get the io_context associated with the
205 * This function may be used to obtain the io_context object that the I/O
206 * object uses to dispatch handlers for asynchronous operations.
208 * @return A reference to the io_context object that the I/O object will use
209 * to dispatch handlers. Ownership is not transferred to the caller.
211 boost::asio::io_context& get_io_context()
213 return basic_io_object<BOOST_ASIO_SVC_T>::get_io_context();
216 /// (Deprecated: Use get_executor().) Get the io_context associated with the
219 * This function may be used to obtain the io_context object that the I/O
220 * object uses to dispatch handlers for asynchronous operations.
222 * @return A reference to the io_context object that the I/O object will use
223 * to dispatch handlers. Ownership is not transferred to the caller.
225 boost::asio::io_context& get_io_service()
227 return basic_io_object<BOOST_ASIO_SVC_T>::get_io_service();
229 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
231 /// Get the executor associated with the object.
232 executor_type get_executor() BOOST_ASIO_NOEXCEPT
234 return basic_io_object<BOOST_ASIO_SVC_T>::get_executor();
237 /// Get a reference to the lowest layer.
239 * This function returns a reference to the lowest layer in a stack of
240 * layers. Since a serial_port cannot contain any further layers, it simply
241 * returns a reference to itself.
243 * @return A reference to the lowest layer in the stack of layers. Ownership
244 * is not transferred to the caller.
246 lowest_layer_type& lowest_layer()
251 /// Get a const reference to the lowest layer.
253 * This function returns a const reference to the lowest layer in a stack of
254 * layers. Since a serial_port cannot contain any further layers, it simply
255 * returns a reference to itself.
257 * @return A const reference to the lowest layer in the stack of layers.
258 * Ownership is not transferred to the caller.
260 const lowest_layer_type& lowest_layer() const
265 /// Open the serial port using the specified device name.
267 * This function opens the serial port for the specified device name.
269 * @param device The platform-specific device name.
271 * @throws boost::system::system_error Thrown on failure.
273 void open(const std::string& device)
275 boost::system::error_code ec;
276 this->get_service().open(this->get_implementation(), device, ec);
277 boost::asio::detail::throw_error(ec, "open");
280 /// Open the serial port using the specified device name.
282 * This function opens the serial port using the given platform-specific
285 * @param device The platform-specific device name.
287 * @param ec Set the indicate what error occurred, if any.
289 BOOST_ASIO_SYNC_OP_VOID open(const std::string& device,
290 boost::system::error_code& ec)
292 this->get_service().open(this->get_implementation(), device, ec);
293 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
296 /// Assign an existing native serial port to the serial port.
298 * This function opens the serial port to hold an existing native serial port.
300 * @param native_serial_port A native serial port.
302 * @throws boost::system::system_error Thrown on failure.
304 void assign(const native_handle_type& native_serial_port)
306 boost::system::error_code ec;
307 this->get_service().assign(this->get_implementation(),
308 native_serial_port, ec);
309 boost::asio::detail::throw_error(ec, "assign");
312 /// Assign an existing native serial port to the serial port.
314 * This function opens the serial port to hold an existing native serial port.
316 * @param native_serial_port A native serial port.
318 * @param ec Set to indicate what error occurred, if any.
320 BOOST_ASIO_SYNC_OP_VOID assign(const native_handle_type& native_serial_port,
321 boost::system::error_code& ec)
323 this->get_service().assign(this->get_implementation(),
324 native_serial_port, ec);
325 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
328 /// Determine whether the serial port is open.
331 return this->get_service().is_open(this->get_implementation());
334 /// Close the serial port.
336 * This function is used to close the serial port. Any asynchronous read or
337 * write operations will be cancelled immediately, and will complete with the
338 * boost::asio::error::operation_aborted error.
340 * @throws boost::system::system_error Thrown on failure.
344 boost::system::error_code ec;
345 this->get_service().close(this->get_implementation(), ec);
346 boost::asio::detail::throw_error(ec, "close");
349 /// Close the serial port.
351 * This function is used to close the serial port. Any asynchronous read or
352 * write operations will be cancelled immediately, and will complete with the
353 * boost::asio::error::operation_aborted error.
355 * @param ec Set to indicate what error occurred, if any.
357 BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec)
359 this->get_service().close(this->get_implementation(), ec);
360 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
363 /// Get the native serial port representation.
365 * This function may be used to obtain the underlying representation of the
366 * serial port. This is intended to allow access to native serial port
367 * functionality that is not otherwise provided.
369 native_handle_type native_handle()
371 return this->get_service().native_handle(this->get_implementation());
374 /// Cancel all asynchronous operations associated with the serial port.
376 * This function causes all outstanding asynchronous read or write operations
377 * to finish immediately, and the handlers for cancelled operations will be
378 * passed the boost::asio::error::operation_aborted error.
380 * @throws boost::system::system_error Thrown on failure.
384 boost::system::error_code ec;
385 this->get_service().cancel(this->get_implementation(), ec);
386 boost::asio::detail::throw_error(ec, "cancel");
389 /// Cancel all asynchronous operations associated with the serial port.
391 * This function causes all outstanding asynchronous read or write operations
392 * to finish immediately, and the handlers for cancelled operations will be
393 * passed the boost::asio::error::operation_aborted error.
395 * @param ec Set to indicate what error occurred, if any.
397 BOOST_ASIO_SYNC_OP_VOID cancel(boost::system::error_code& ec)
399 this->get_service().cancel(this->get_implementation(), ec);
400 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
403 /// Send a break sequence to the serial port.
405 * This function causes a break sequence of platform-specific duration to be
406 * sent out the serial port.
408 * @throws boost::system::system_error Thrown on failure.
412 boost::system::error_code ec;
413 this->get_service().send_break(this->get_implementation(), ec);
414 boost::asio::detail::throw_error(ec, "send_break");
417 /// Send a break sequence to the serial port.
419 * This function causes a break sequence of platform-specific duration to be
420 * sent out the serial port.
422 * @param ec Set to indicate what error occurred, if any.
424 BOOST_ASIO_SYNC_OP_VOID send_break(boost::system::error_code& ec)
426 this->get_service().send_break(this->get_implementation(), ec);
427 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
430 /// Set an option on the serial port.
432 * This function is used to set an option on the serial port.
434 * @param option The option value to be set on the serial port.
436 * @throws boost::system::system_error Thrown on failure.
438 * @sa SettableSerialPortOption @n
439 * boost::asio::serial_port_base::baud_rate @n
440 * boost::asio::serial_port_base::flow_control @n
441 * boost::asio::serial_port_base::parity @n
442 * boost::asio::serial_port_base::stop_bits @n
443 * boost::asio::serial_port_base::character_size
445 template <typename SettableSerialPortOption>
446 void set_option(const SettableSerialPortOption& option)
448 boost::system::error_code ec;
449 this->get_service().set_option(this->get_implementation(), option, ec);
450 boost::asio::detail::throw_error(ec, "set_option");
453 /// Set an option on the serial port.
455 * This function is used to set an option on the serial port.
457 * @param option The option value to be set on the serial port.
459 * @param ec Set to indicate what error occurred, if any.
461 * @sa SettableSerialPortOption @n
462 * boost::asio::serial_port_base::baud_rate @n
463 * boost::asio::serial_port_base::flow_control @n
464 * boost::asio::serial_port_base::parity @n
465 * boost::asio::serial_port_base::stop_bits @n
466 * boost::asio::serial_port_base::character_size
468 template <typename SettableSerialPortOption>
469 BOOST_ASIO_SYNC_OP_VOID set_option(const SettableSerialPortOption& option,
470 boost::system::error_code& ec)
472 this->get_service().set_option(this->get_implementation(), option, ec);
473 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
476 /// Get an option from the serial port.
478 * This function is used to get the current value of an option on the serial
481 * @param option The option value to be obtained from the serial port.
483 * @throws boost::system::system_error Thrown on failure.
485 * @sa GettableSerialPortOption @n
486 * boost::asio::serial_port_base::baud_rate @n
487 * boost::asio::serial_port_base::flow_control @n
488 * boost::asio::serial_port_base::parity @n
489 * boost::asio::serial_port_base::stop_bits @n
490 * boost::asio::serial_port_base::character_size
492 template <typename GettableSerialPortOption>
493 void get_option(GettableSerialPortOption& option)
495 boost::system::error_code ec;
496 this->get_service().get_option(this->get_implementation(), option, ec);
497 boost::asio::detail::throw_error(ec, "get_option");
500 /// Get an option from the serial port.
502 * This function is used to get the current value of an option on the serial
505 * @param option The option value to be obtained from the serial port.
507 * @param ec Set to indicate what error occurred, if any.
509 * @sa GettableSerialPortOption @n
510 * boost::asio::serial_port_base::baud_rate @n
511 * boost::asio::serial_port_base::flow_control @n
512 * boost::asio::serial_port_base::parity @n
513 * boost::asio::serial_port_base::stop_bits @n
514 * boost::asio::serial_port_base::character_size
516 template <typename GettableSerialPortOption>
517 BOOST_ASIO_SYNC_OP_VOID get_option(GettableSerialPortOption& option,
518 boost::system::error_code& ec)
520 this->get_service().get_option(this->get_implementation(), option, ec);
521 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
524 /// Write some data to the serial port.
526 * This function is used to write data to the serial port. The function call
527 * will block until one or more bytes of the data has been written
528 * successfully, or until an error occurs.
530 * @param buffers One or more data buffers to be written to the serial port.
532 * @returns The number of bytes written.
534 * @throws boost::system::system_error Thrown on failure. An error code of
535 * boost::asio::error::eof indicates that the connection was closed by the
538 * @note The write_some operation may not transmit all of the data to the
539 * peer. Consider using the @ref write function if you need to ensure that
540 * all data is written before the blocking operation completes.
543 * To write a single data buffer use the @ref buffer function as follows:
545 * serial_port.write_some(boost::asio::buffer(data, size));
547 * See the @ref buffer documentation for information on writing multiple
548 * buffers in one go, and how to use it with arrays, boost::array or
551 template <typename ConstBufferSequence>
552 std::size_t write_some(const ConstBufferSequence& buffers)
554 boost::system::error_code ec;
555 std::size_t s = this->get_service().write_some(
556 this->get_implementation(), buffers, ec);
557 boost::asio::detail::throw_error(ec, "write_some");
561 /// Write some data to the serial port.
563 * This function is used to write data to the serial port. The function call
564 * will block until one or more bytes of the data has been written
565 * successfully, or until an error occurs.
567 * @param buffers One or more data buffers to be written to the serial port.
569 * @param ec Set to indicate what error occurred, if any.
571 * @returns The number of bytes written. Returns 0 if an error occurred.
573 * @note The write_some operation may not transmit all of the data to the
574 * peer. Consider using the @ref write function if you need to ensure that
575 * all data is written before the blocking operation completes.
577 template <typename ConstBufferSequence>
578 std::size_t write_some(const ConstBufferSequence& buffers,
579 boost::system::error_code& ec)
581 return this->get_service().write_some(
582 this->get_implementation(), buffers, ec);
585 /// Start an asynchronous write.
587 * This function is used to asynchronously write data to the serial port.
588 * The function call always returns immediately.
590 * @param buffers One or more data buffers to be written to the serial port.
591 * Although the buffers object may be copied as necessary, ownership of the
592 * underlying memory blocks is retained by the caller, which must guarantee
593 * that they remain valid until the handler is called.
595 * @param handler The handler to be called when the write operation completes.
596 * Copies will be made of the handler as required. The function signature of
597 * the handler must be:
598 * @code void handler(
599 * const boost::system::error_code& error, // Result of operation.
600 * std::size_t bytes_transferred // Number of bytes written.
602 * Regardless of whether the asynchronous operation completes immediately or
603 * not, the handler will not be invoked from within this function. Invocation
604 * of the handler will be performed in a manner equivalent to using
605 * boost::asio::io_context::post().
607 * @note The write operation may not transmit all of the data to the peer.
608 * Consider using the @ref async_write function if you need to ensure that all
609 * data is written before the asynchronous operation completes.
612 * To write a single data buffer use the @ref buffer function as follows:
614 * serial_port.async_write_some(boost::asio::buffer(data, size), handler);
616 * See the @ref buffer documentation for information on writing multiple
617 * buffers in one go, and how to use it with arrays, boost::array or
620 template <typename ConstBufferSequence, typename WriteHandler>
621 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
622 void (boost::system::error_code, std::size_t))
623 async_write_some(const ConstBufferSequence& buffers,
624 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
626 // If you get an error on the following line it means that your handler does
627 // not meet the documented type requirements for a WriteHandler.
628 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
630 async_completion<WriteHandler,
631 void (boost::system::error_code, std::size_t)> init(handler);
633 this->get_service().async_write_some(
634 this->get_implementation(), buffers, init.completion_handler);
636 return init.result.get();
639 /// Read some data from the serial port.
641 * This function is used to read data from the serial port. The function
642 * call will block until one or more bytes of data has been read successfully,
643 * or until an error occurs.
645 * @param buffers One or more buffers into which the data will be read.
647 * @returns The number of bytes read.
649 * @throws boost::system::system_error Thrown on failure. An error code of
650 * boost::asio::error::eof indicates that the connection was closed by the
653 * @note The read_some operation may not read all of the requested number of
654 * bytes. Consider using the @ref read function if you need to ensure that
655 * the requested amount of data is read before the blocking operation
659 * To read into a single data buffer use the @ref buffer function as follows:
661 * serial_port.read_some(boost::asio::buffer(data, size));
663 * See the @ref buffer documentation for information on reading into multiple
664 * buffers in one go, and how to use it with arrays, boost::array or
667 template <typename MutableBufferSequence>
668 std::size_t read_some(const MutableBufferSequence& buffers)
670 boost::system::error_code ec;
671 std::size_t s = this->get_service().read_some(
672 this->get_implementation(), buffers, ec);
673 boost::asio::detail::throw_error(ec, "read_some");
677 /// Read some data from the serial port.
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.
683 * @param buffers One or more buffers into which the data will be read.
685 * @param ec Set to indicate what error occurred, if any.
687 * @returns The number of bytes read. Returns 0 if an error occurred.
689 * @note The read_some operation may not read all of the requested number of
690 * bytes. Consider using the @ref read function if you need to ensure that
691 * the requested amount of data is read before the blocking operation
694 template <typename MutableBufferSequence>
695 std::size_t read_some(const MutableBufferSequence& buffers,
696 boost::system::error_code& ec)
698 return this->get_service().read_some(
699 this->get_implementation(), buffers, ec);
702 /// Start an asynchronous read.
704 * This function is used to asynchronously read data from the serial port.
705 * The function call always returns immediately.
707 * @param buffers One or more buffers into which the data will be read.
708 * Although the buffers object may be copied as necessary, ownership of the
709 * underlying memory blocks is retained by the caller, which must guarantee
710 * that they remain valid until the handler is called.
712 * @param handler The handler to be called when the read operation completes.
713 * Copies will be made of the handler as required. The function signature of
714 * the handler must be:
715 * @code void handler(
716 * const boost::system::error_code& error, // Result of operation.
717 * std::size_t bytes_transferred // Number of bytes read.
719 * Regardless of whether the asynchronous operation completes immediately or
720 * not, the handler will not be invoked from within this function. Invocation
721 * of the handler will be performed in a manner equivalent to using
722 * boost::asio::io_context::post().
724 * @note The read operation may not read all of the requested number of bytes.
725 * Consider using the @ref async_read function if you need to ensure that the
726 * requested amount of data is read before the asynchronous operation
730 * To read into a single data buffer use the @ref buffer function as follows:
732 * serial_port.async_read_some(boost::asio::buffer(data, size), handler);
734 * See the @ref buffer documentation for information on reading into multiple
735 * buffers in one go, and how to use it with arrays, boost::array or
738 template <typename MutableBufferSequence, typename ReadHandler>
739 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
740 void (boost::system::error_code, std::size_t))
741 async_read_some(const MutableBufferSequence& buffers,
742 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
744 // If you get an error on the following line it means that your handler does
745 // not meet the documented type requirements for a ReadHandler.
746 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
748 async_completion<ReadHandler,
749 void (boost::system::error_code, std::size_t)> init(handler);
751 this->get_service().async_read_some(
752 this->get_implementation(), buffers, init.completion_handler);
754 return init.result.get();
757 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
762 #include <boost/asio/detail/pop_options.hpp>
764 #if !defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
765 # undef BOOST_ASIO_SVC_T
766 #endif // !defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
768 #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT)
769 // || defined(GENERATING_DOCUMENTATION)
771 #endif // BOOST_ASIO_SERIAL_PORT_HPP