2 // basic_serial_port.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~
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_BASIC_SERIAL_PORT_HPP
13 #define BOOST_ASIO_BASIC_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_ENABLE_OLD_SERVICES)
23 #if defined(BOOST_ASIO_HAS_SERIAL_PORT) \
24 || defined(GENERATING_DOCUMENTATION)
27 #include <boost/asio/basic_io_object.hpp>
28 #include <boost/asio/detail/handler_type_requirements.hpp>
29 #include <boost/asio/detail/throw_error.hpp>
30 #include <boost/asio/error.hpp>
31 #include <boost/asio/serial_port_base.hpp>
32 #include <boost/asio/serial_port_service.hpp>
34 #include <boost/asio/detail/push_options.hpp>
39 /// Provides serial port functionality.
41 * The basic_serial_port class template provides functionality that is common
42 * to all serial ports.
45 * @e Distinct @e objects: Safe.@n
46 * @e Shared @e objects: Unsafe.
48 template <typename SerialPortService = serial_port_service>
49 class basic_serial_port
50 : public basic_io_object<SerialPortService>,
51 public serial_port_base
54 /// The native representation of a serial port.
55 typedef typename SerialPortService::native_handle_type native_handle_type;
57 /// A basic_serial_port is always the lowest layer.
58 typedef basic_serial_port<SerialPortService> lowest_layer_type;
60 /// Construct a basic_serial_port without opening it.
62 * This constructor creates a serial port without opening it.
64 * @param io_context The io_context object that the serial port will use to
65 * dispatch handlers for any asynchronous operations performed on the port.
67 explicit basic_serial_port(boost::asio::io_context& io_context)
68 : basic_io_object<SerialPortService>(io_context)
72 /// Construct and open a basic_serial_port.
74 * This constructor creates and opens a serial port for the specified device
77 * @param io_context The io_context object that the serial port will use to
78 * dispatch handlers for any asynchronous operations performed on the port.
80 * @param device The platform-specific device name for this serial
83 explicit basic_serial_port(boost::asio::io_context& io_context,
85 : basic_io_object<SerialPortService>(io_context)
87 boost::system::error_code ec;
88 this->get_service().open(this->get_implementation(), device, ec);
89 boost::asio::detail::throw_error(ec, "open");
92 /// Construct and open a basic_serial_port.
94 * This constructor creates and opens a serial port for the specified device
97 * @param io_context The io_context object that the serial port will use to
98 * dispatch handlers for any asynchronous operations performed on the port.
100 * @param device The platform-specific device name for this serial
103 explicit basic_serial_port(boost::asio::io_context& io_context,
104 const std::string& device)
105 : basic_io_object<SerialPortService>(io_context)
107 boost::system::error_code ec;
108 this->get_service().open(this->get_implementation(), device, ec);
109 boost::asio::detail::throw_error(ec, "open");
112 /// Construct a basic_serial_port on an existing native serial port.
114 * This constructor creates a serial port object to hold an existing native
117 * @param io_context The io_context object that the serial port will use to
118 * dispatch handlers for any asynchronous operations performed on the port.
120 * @param native_serial_port A native serial port.
122 * @throws boost::system::system_error Thrown on failure.
124 basic_serial_port(boost::asio::io_context& io_context,
125 const native_handle_type& native_serial_port)
126 : basic_io_object<SerialPortService>(io_context)
128 boost::system::error_code ec;
129 this->get_service().assign(this->get_implementation(),
130 native_serial_port, ec);
131 boost::asio::detail::throw_error(ec, "assign");
134 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
135 /// Move-construct a basic_serial_port from another.
137 * This constructor moves a serial port from one object to another.
139 * @param other The other basic_serial_port object from which the move will
142 * @note Following the move, the moved-from object is in the same state as if
143 * constructed using the @c basic_serial_port(io_context&) constructor.
145 basic_serial_port(basic_serial_port&& other)
146 : basic_io_object<SerialPortService>(
147 BOOST_ASIO_MOVE_CAST(basic_serial_port)(other))
151 /// Move-assign a basic_serial_port from another.
153 * This assignment operator moves a serial port from one object to another.
155 * @param other The other basic_serial_port object from which the move will
158 * @note Following the move, the moved-from object is in the same state as if
159 * constructed using the @c basic_serial_port(io_context&) constructor.
161 basic_serial_port& operator=(basic_serial_port&& other)
163 basic_io_object<SerialPortService>::operator=(
164 BOOST_ASIO_MOVE_CAST(basic_serial_port)(other));
167 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
169 /// Get a reference to the lowest layer.
171 * This function returns a reference to the lowest layer in a stack of
172 * layers. Since a basic_serial_port cannot contain any further layers, it
173 * simply returns a reference to itself.
175 * @return A reference to the lowest layer in the stack of layers. Ownership
176 * is not transferred to the caller.
178 lowest_layer_type& lowest_layer()
183 /// Get a const reference to the lowest layer.
185 * This function returns a const reference to the lowest layer in a stack of
186 * layers. Since a basic_serial_port cannot contain any further layers, it
187 * simply returns a reference to itself.
189 * @return A const reference to the lowest layer in the stack of layers.
190 * Ownership is not transferred to the caller.
192 const lowest_layer_type& lowest_layer() const
197 /// Open the serial port using the specified device name.
199 * This function opens the serial port for the specified device name.
201 * @param device The platform-specific device name.
203 * @throws boost::system::system_error Thrown on failure.
205 void open(const std::string& device)
207 boost::system::error_code ec;
208 this->get_service().open(this->get_implementation(), device, ec);
209 boost::asio::detail::throw_error(ec, "open");
212 /// Open the serial port using the specified device name.
214 * This function opens the serial port using the given platform-specific
217 * @param device The platform-specific device name.
219 * @param ec Set the indicate what error occurred, if any.
221 BOOST_ASIO_SYNC_OP_VOID open(const std::string& device,
222 boost::system::error_code& ec)
224 this->get_service().open(this->get_implementation(), device, ec);
225 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
228 /// Assign an existing native serial port to the serial port.
230 * This function opens the serial port to hold an existing native serial port.
232 * @param native_serial_port A native serial port.
234 * @throws boost::system::system_error Thrown on failure.
236 void assign(const native_handle_type& native_serial_port)
238 boost::system::error_code ec;
239 this->get_service().assign(this->get_implementation(),
240 native_serial_port, ec);
241 boost::asio::detail::throw_error(ec, "assign");
244 /// Assign an existing native serial port to the serial port.
246 * This function opens the serial port to hold an existing native serial port.
248 * @param native_serial_port A native serial port.
250 * @param ec Set to indicate what error occurred, if any.
252 BOOST_ASIO_SYNC_OP_VOID assign(const native_handle_type& native_serial_port,
253 boost::system::error_code& ec)
255 this->get_service().assign(this->get_implementation(),
256 native_serial_port, ec);
257 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
260 /// Determine whether the serial port is open.
263 return this->get_service().is_open(this->get_implementation());
266 /// Close the serial port.
268 * This function is used to close the serial port. Any asynchronous read or
269 * write operations will be cancelled immediately, and will complete with the
270 * boost::asio::error::operation_aborted error.
272 * @throws boost::system::system_error Thrown on failure.
276 boost::system::error_code ec;
277 this->get_service().close(this->get_implementation(), ec);
278 boost::asio::detail::throw_error(ec, "close");
281 /// Close the serial port.
283 * This function is used to close the serial port. Any asynchronous read or
284 * write operations will be cancelled immediately, and will complete with the
285 * boost::asio::error::operation_aborted error.
287 * @param ec Set to indicate what error occurred, if any.
289 BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec)
291 this->get_service().close(this->get_implementation(), ec);
292 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
295 /// Get the native serial port representation.
297 * This function may be used to obtain the underlying representation of the
298 * serial port. This is intended to allow access to native serial port
299 * functionality that is not otherwise provided.
301 native_handle_type native_handle()
303 return this->get_service().native_handle(this->get_implementation());
306 /// Cancel all asynchronous operations associated with the serial port.
308 * This function causes all outstanding asynchronous read or write operations
309 * to finish immediately, and the handlers for cancelled operations will be
310 * passed the boost::asio::error::operation_aborted error.
312 * @throws boost::system::system_error Thrown on failure.
316 boost::system::error_code ec;
317 this->get_service().cancel(this->get_implementation(), ec);
318 boost::asio::detail::throw_error(ec, "cancel");
321 /// Cancel all asynchronous operations associated with the serial port.
323 * This function causes all outstanding asynchronous read or write operations
324 * to finish immediately, and the handlers for cancelled operations will be
325 * passed the boost::asio::error::operation_aborted error.
327 * @param ec Set to indicate what error occurred, if any.
329 BOOST_ASIO_SYNC_OP_VOID cancel(boost::system::error_code& ec)
331 this->get_service().cancel(this->get_implementation(), ec);
332 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
335 /// Send a break sequence to the serial port.
337 * This function causes a break sequence of platform-specific duration to be
338 * sent out the serial port.
340 * @throws boost::system::system_error Thrown on failure.
344 boost::system::error_code ec;
345 this->get_service().send_break(this->get_implementation(), ec);
346 boost::asio::detail::throw_error(ec, "send_break");
349 /// Send a break sequence to the serial port.
351 * This function causes a break sequence of platform-specific duration to be
352 * sent out the serial port.
354 * @param ec Set to indicate what error occurred, if any.
356 BOOST_ASIO_SYNC_OP_VOID send_break(boost::system::error_code& ec)
358 this->get_service().send_break(this->get_implementation(), ec);
359 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
362 /// Set an option on the serial port.
364 * This function is used to set an option on the serial port.
366 * @param option The option value to be set on the serial port.
368 * @throws boost::system::system_error Thrown on failure.
370 * @sa SettableSerialPortOption @n
371 * boost::asio::serial_port_base::baud_rate @n
372 * boost::asio::serial_port_base::flow_control @n
373 * boost::asio::serial_port_base::parity @n
374 * boost::asio::serial_port_base::stop_bits @n
375 * boost::asio::serial_port_base::character_size
377 template <typename SettableSerialPortOption>
378 void set_option(const SettableSerialPortOption& option)
380 boost::system::error_code ec;
381 this->get_service().set_option(this->get_implementation(), option, ec);
382 boost::asio::detail::throw_error(ec, "set_option");
385 /// Set an option on the serial port.
387 * This function is used to set an option on the serial port.
389 * @param option The option value to be set on the serial port.
391 * @param ec Set to indicate what error occurred, if any.
393 * @sa SettableSerialPortOption @n
394 * boost::asio::serial_port_base::baud_rate @n
395 * boost::asio::serial_port_base::flow_control @n
396 * boost::asio::serial_port_base::parity @n
397 * boost::asio::serial_port_base::stop_bits @n
398 * boost::asio::serial_port_base::character_size
400 template <typename SettableSerialPortOption>
401 BOOST_ASIO_SYNC_OP_VOID set_option(const SettableSerialPortOption& option,
402 boost::system::error_code& ec)
404 this->get_service().set_option(this->get_implementation(), option, ec);
405 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
408 /// Get an option from the serial port.
410 * This function is used to get the current value of an option on the serial
413 * @param option The option value to be obtained from the serial port.
415 * @throws boost::system::system_error Thrown on failure.
417 * @sa GettableSerialPortOption @n
418 * boost::asio::serial_port_base::baud_rate @n
419 * boost::asio::serial_port_base::flow_control @n
420 * boost::asio::serial_port_base::parity @n
421 * boost::asio::serial_port_base::stop_bits @n
422 * boost::asio::serial_port_base::character_size
424 template <typename GettableSerialPortOption>
425 void get_option(GettableSerialPortOption& option)
427 boost::system::error_code ec;
428 this->get_service().get_option(this->get_implementation(), option, ec);
429 boost::asio::detail::throw_error(ec, "get_option");
432 /// Get an option from the serial port.
434 * This function is used to get the current value of an option on the serial
437 * @param option The option value to be obtained from the serial port.
439 * @param ec Set to indicate what error occurred, if any.
441 * @sa GettableSerialPortOption @n
442 * boost::asio::serial_port_base::baud_rate @n
443 * boost::asio::serial_port_base::flow_control @n
444 * boost::asio::serial_port_base::parity @n
445 * boost::asio::serial_port_base::stop_bits @n
446 * boost::asio::serial_port_base::character_size
448 template <typename GettableSerialPortOption>
449 BOOST_ASIO_SYNC_OP_VOID get_option(GettableSerialPortOption& option,
450 boost::system::error_code& ec)
452 this->get_service().get_option(this->get_implementation(), option, ec);
453 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
456 /// Write some data to the serial port.
458 * This function is used to write data to the serial port. The function call
459 * will block until one or more bytes of the data has been written
460 * successfully, or until an error occurs.
462 * @param buffers One or more data buffers to be written to the serial port.
464 * @returns The number of bytes written.
466 * @throws boost::system::system_error Thrown on failure. An error code of
467 * boost::asio::error::eof indicates that the connection was closed by the
470 * @note The write_some operation may not transmit all of the data to the
471 * peer. Consider using the @ref write function if you need to ensure that
472 * all data is written before the blocking operation completes.
475 * To write a single data buffer use the @ref buffer function as follows:
477 * serial_port.write_some(boost::asio::buffer(data, size));
479 * See the @ref buffer documentation for information on writing multiple
480 * buffers in one go, and how to use it with arrays, boost::array or
483 template <typename ConstBufferSequence>
484 std::size_t write_some(const ConstBufferSequence& buffers)
486 boost::system::error_code ec;
487 std::size_t s = this->get_service().write_some(
488 this->get_implementation(), buffers, ec);
489 boost::asio::detail::throw_error(ec, "write_some");
493 /// Write some data to the serial port.
495 * This function is used to write data to the serial port. The function call
496 * will block until one or more bytes of the data has been written
497 * successfully, or until an error occurs.
499 * @param buffers One or more data buffers to be written to the serial port.
501 * @param ec Set to indicate what error occurred, if any.
503 * @returns The number of bytes written. Returns 0 if an error occurred.
505 * @note The write_some operation may not transmit all of the data to the
506 * peer. Consider using the @ref write function if you need to ensure that
507 * all data is written before the blocking operation completes.
509 template <typename ConstBufferSequence>
510 std::size_t write_some(const ConstBufferSequence& buffers,
511 boost::system::error_code& ec)
513 return this->get_service().write_some(
514 this->get_implementation(), buffers, ec);
517 /// Start an asynchronous write.
519 * This function is used to asynchronously write data to the serial port.
520 * The function call always returns immediately.
522 * @param buffers One or more data buffers to be written to the serial port.
523 * Although the buffers object may be copied as necessary, ownership of the
524 * underlying memory blocks is retained by the caller, which must guarantee
525 * that they remain valid until the handler is called.
527 * @param handler The handler to be called when the write operation completes.
528 * Copies will be made of the handler as required. The function signature of
529 * the handler must be:
530 * @code void handler(
531 * const boost::system::error_code& error, // Result of operation.
532 * std::size_t bytes_transferred // Number of bytes written.
534 * Regardless of whether the asynchronous operation completes immediately or
535 * not, the handler will not be invoked from within this function. Invocation
536 * of the handler will be performed in a manner equivalent to using
537 * boost::asio::io_context::post().
539 * @note The write operation may not transmit all of the data to the peer.
540 * Consider using the @ref async_write function if you need to ensure that all
541 * data is written before the asynchronous operation completes.
544 * To write a single data buffer use the @ref buffer function as follows:
546 * serial_port.async_write_some(boost::asio::buffer(data, size), handler);
548 * See the @ref buffer documentation for information on writing multiple
549 * buffers in one go, and how to use it with arrays, boost::array or
552 template <typename ConstBufferSequence, typename WriteHandler>
553 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
554 void (boost::system::error_code, std::size_t))
555 async_write_some(const ConstBufferSequence& buffers,
556 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
558 // If you get an error on the following line it means that your handler does
559 // not meet the documented type requirements for a WriteHandler.
560 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
562 return this->get_service().async_write_some(this->get_implementation(),
563 buffers, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
566 /// Read some data from the serial port.
568 * This function is used to read data from the serial port. The function
569 * call will block until one or more bytes of data has been read successfully,
570 * or until an error occurs.
572 * @param buffers One or more buffers into which the data will be read.
574 * @returns The number of bytes read.
576 * @throws boost::system::system_error Thrown on failure. An error code of
577 * boost::asio::error::eof indicates that the connection was closed by the
580 * @note The read_some operation may not read all of the requested number of
581 * bytes. Consider using the @ref read function if you need to ensure that
582 * the requested amount of data is read before the blocking operation
586 * To read into a single data buffer use the @ref buffer function as follows:
588 * serial_port.read_some(boost::asio::buffer(data, size));
590 * See the @ref buffer documentation for information on reading into multiple
591 * buffers in one go, and how to use it with arrays, boost::array or
594 template <typename MutableBufferSequence>
595 std::size_t read_some(const MutableBufferSequence& buffers)
597 boost::system::error_code ec;
598 std::size_t s = this->get_service().read_some(
599 this->get_implementation(), buffers, ec);
600 boost::asio::detail::throw_error(ec, "read_some");
604 /// Read some data from the serial port.
606 * This function is used to read data from the serial port. The function
607 * call will block until one or more bytes of data has been read successfully,
608 * or until an error occurs.
610 * @param buffers One or more buffers into which the data will be read.
612 * @param ec Set to indicate what error occurred, if any.
614 * @returns The number of bytes read. Returns 0 if an error occurred.
616 * @note The read_some operation may not read all of the requested number of
617 * bytes. Consider using the @ref read function if you need to ensure that
618 * the requested amount of data is read before the blocking operation
621 template <typename MutableBufferSequence>
622 std::size_t read_some(const MutableBufferSequence& buffers,
623 boost::system::error_code& ec)
625 return this->get_service().read_some(
626 this->get_implementation(), buffers, ec);
629 /// Start an asynchronous read.
631 * This function is used to asynchronously read data from the serial port.
632 * The function call always returns immediately.
634 * @param buffers One or more buffers into which the data will be read.
635 * Although the buffers object may be copied as necessary, ownership of the
636 * underlying memory blocks is retained by the caller, which must guarantee
637 * that they remain valid until the handler is called.
639 * @param handler The handler to be called when the read operation completes.
640 * Copies will be made of the handler as required. The function signature of
641 * the handler must be:
642 * @code void handler(
643 * const boost::system::error_code& error, // Result of operation.
644 * std::size_t bytes_transferred // Number of bytes read.
646 * Regardless of whether the asynchronous operation completes immediately or
647 * not, the handler will not be invoked from within this function. Invocation
648 * of the handler will be performed in a manner equivalent to using
649 * boost::asio::io_context::post().
651 * @note The read operation may not read all of the requested number of bytes.
652 * Consider using the @ref async_read function if you need to ensure that the
653 * requested amount of data is read before the asynchronous operation
657 * To read into a single data buffer use the @ref buffer function as follows:
659 * serial_port.async_read_some(boost::asio::buffer(data, size), handler);
661 * See the @ref buffer documentation for information on reading into multiple
662 * buffers in one go, and how to use it with arrays, boost::array or
665 template <typename MutableBufferSequence, typename ReadHandler>
666 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
667 void (boost::system::error_code, std::size_t))
668 async_read_some(const MutableBufferSequence& buffers,
669 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
671 // If you get an error on the following line it means that your handler does
672 // not meet the documented type requirements for a ReadHandler.
673 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
675 return this->get_service().async_read_some(this->get_implementation(),
676 buffers, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
683 #include <boost/asio/detail/pop_options.hpp>
685 #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT)
686 // || defined(GENERATING_DOCUMENTATION)
688 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
690 #endif // BOOST_ASIO_BASIC_SERIAL_PORT_HPP