Chris@16: // Chris@16: // basic_serial_port.hpp Chris@16: // ~~~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_BASIC_SERIAL_PORT_HPP Chris@16: #define BOOST_ASIO_BASIC_SERIAL_PORT_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_SERIAL_PORT) \ Chris@16: || defined(GENERATING_DOCUMENTATION) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: Chris@16: /// Provides serial port functionality. Chris@16: /** Chris@16: * The basic_serial_port class template provides functionality that is common Chris@16: * to all serial ports. Chris@16: * Chris@16: * @par Thread Safety Chris@16: * @e Distinct @e objects: Safe.@n Chris@16: * @e Shared @e objects: Unsafe. Chris@16: */ Chris@16: template Chris@16: class basic_serial_port Chris@16: : public basic_io_object, Chris@16: public serial_port_base Chris@16: { Chris@16: public: Chris@16: /// (Deprecated: Use native_handle_type.) The native representation of a Chris@16: /// serial port. Chris@16: typedef typename SerialPortService::native_handle_type native_type; Chris@16: Chris@16: /// The native representation of a serial port. Chris@16: typedef typename SerialPortService::native_handle_type native_handle_type; Chris@16: Chris@16: /// A basic_serial_port is always the lowest layer. Chris@16: typedef basic_serial_port lowest_layer_type; Chris@16: Chris@16: /// Construct a basic_serial_port without opening it. Chris@16: /** Chris@16: * This constructor creates a serial port without opening it. Chris@16: * Chris@16: * @param io_service The io_service object that the serial port will use to Chris@16: * dispatch handlers for any asynchronous operations performed on the port. Chris@16: */ Chris@16: explicit basic_serial_port(boost::asio::io_service& io_service) Chris@16: : basic_io_object(io_service) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Construct and open a basic_serial_port. Chris@16: /** Chris@16: * This constructor creates and opens a serial port for the specified device Chris@16: * name. Chris@16: * Chris@16: * @param io_service The io_service object that the serial port will use to Chris@16: * dispatch handlers for any asynchronous operations performed on the port. Chris@16: * Chris@16: * @param device The platform-specific device name for this serial Chris@16: * port. Chris@16: */ Chris@16: explicit basic_serial_port(boost::asio::io_service& io_service, Chris@16: const char* device) Chris@16: : basic_io_object(io_service) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().open(this->get_implementation(), device, ec); Chris@16: boost::asio::detail::throw_error(ec, "open"); Chris@16: } Chris@16: Chris@16: /// Construct and open a basic_serial_port. Chris@16: /** Chris@16: * This constructor creates and opens a serial port for the specified device Chris@16: * name. Chris@16: * Chris@16: * @param io_service The io_service object that the serial port will use to Chris@16: * dispatch handlers for any asynchronous operations performed on the port. Chris@16: * Chris@16: * @param device The platform-specific device name for this serial Chris@16: * port. Chris@16: */ Chris@16: explicit basic_serial_port(boost::asio::io_service& io_service, Chris@16: const std::string& device) Chris@16: : basic_io_object(io_service) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().open(this->get_implementation(), device, ec); Chris@16: boost::asio::detail::throw_error(ec, "open"); Chris@16: } Chris@16: Chris@16: /// Construct a basic_serial_port on an existing native serial port. Chris@16: /** Chris@16: * This constructor creates a serial port object to hold an existing native Chris@16: * serial port. Chris@16: * Chris@16: * @param io_service The io_service object that the serial port will use to Chris@16: * dispatch handlers for any asynchronous operations performed on the port. Chris@16: * Chris@16: * @param native_serial_port A native serial port. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: basic_serial_port(boost::asio::io_service& io_service, Chris@16: const native_handle_type& native_serial_port) Chris@16: : basic_io_object(io_service) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().assign(this->get_implementation(), Chris@16: native_serial_port, ec); Chris@16: boost::asio::detail::throw_error(ec, "assign"); Chris@16: } Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) Chris@16: /// Move-construct a basic_serial_port from another. Chris@16: /** Chris@16: * This constructor moves a serial port from one object to another. Chris@16: * Chris@16: * @param other The other basic_serial_port object from which the move will Chris@16: * occur. Chris@16: * Chris@16: * @note Following the move, the moved-from object is in the same state as if Chris@16: * constructed using the @c basic_serial_port(io_service&) constructor. Chris@16: */ Chris@16: basic_serial_port(basic_serial_port&& other) Chris@16: : basic_io_object( Chris@16: BOOST_ASIO_MOVE_CAST(basic_serial_port)(other)) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Move-assign a basic_serial_port from another. Chris@16: /** Chris@16: * This assignment operator moves a serial port from one object to another. Chris@16: * Chris@16: * @param other The other basic_serial_port object from which the move will Chris@16: * occur. Chris@16: * Chris@16: * @note Following the move, the moved-from object is in the same state as if Chris@16: * constructed using the @c basic_serial_port(io_service&) constructor. Chris@16: */ Chris@16: basic_serial_port& operator=(basic_serial_port&& other) Chris@16: { Chris@16: basic_io_object::operator=( Chris@16: BOOST_ASIO_MOVE_CAST(basic_serial_port)(other)); Chris@16: return *this; Chris@16: } Chris@16: #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) Chris@16: Chris@16: /// Get a reference to the lowest layer. Chris@16: /** Chris@16: * This function returns a reference to the lowest layer in a stack of Chris@16: * layers. Since a basic_serial_port cannot contain any further layers, it Chris@16: * simply returns a reference to itself. Chris@16: * Chris@16: * @return A reference to the lowest layer in the stack of layers. Ownership Chris@16: * is not transferred to the caller. Chris@16: */ Chris@16: lowest_layer_type& lowest_layer() Chris@16: { Chris@16: return *this; Chris@16: } Chris@16: Chris@16: /// Get a const reference to the lowest layer. Chris@16: /** Chris@16: * This function returns a const reference to the lowest layer in a stack of Chris@16: * layers. Since a basic_serial_port cannot contain any further layers, it Chris@16: * simply returns a reference to itself. Chris@16: * Chris@16: * @return A const reference to the lowest layer in the stack of layers. Chris@16: * Ownership is not transferred to the caller. Chris@16: */ Chris@16: const lowest_layer_type& lowest_layer() const Chris@16: { Chris@16: return *this; Chris@16: } Chris@16: Chris@16: /// Open the serial port using the specified device name. Chris@16: /** Chris@16: * This function opens the serial port for the specified device name. Chris@16: * Chris@16: * @param device The platform-specific device name. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: void open(const std::string& device) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().open(this->get_implementation(), device, ec); Chris@16: boost::asio::detail::throw_error(ec, "open"); Chris@16: } Chris@16: Chris@16: /// Open the serial port using the specified device name. Chris@16: /** Chris@16: * This function opens the serial port using the given platform-specific Chris@16: * device name. Chris@16: * Chris@16: * @param device The platform-specific device name. Chris@16: * Chris@16: * @param ec Set the indicate what error occurred, if any. Chris@16: */ Chris@16: boost::system::error_code open(const std::string& device, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().open(this->get_implementation(), device, ec); Chris@16: } Chris@16: Chris@16: /// Assign an existing native serial port to the serial port. Chris@16: /* Chris@16: * This function opens the serial port to hold an existing native serial port. Chris@16: * Chris@16: * @param native_serial_port A native serial port. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: void assign(const native_handle_type& native_serial_port) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().assign(this->get_implementation(), Chris@16: native_serial_port, ec); Chris@16: boost::asio::detail::throw_error(ec, "assign"); Chris@16: } Chris@16: Chris@16: /// Assign an existing native serial port to the serial port. Chris@16: /* Chris@16: * This function opens the serial port to hold an existing native serial port. Chris@16: * Chris@16: * @param native_serial_port A native serial port. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: */ Chris@16: boost::system::error_code assign(const native_handle_type& native_serial_port, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().assign(this->get_implementation(), Chris@16: native_serial_port, ec); Chris@16: } Chris@16: Chris@16: /// Determine whether the serial port is open. Chris@16: bool is_open() const Chris@16: { Chris@16: return this->get_service().is_open(this->get_implementation()); Chris@16: } Chris@16: Chris@16: /// Close the serial port. Chris@16: /** Chris@16: * This function is used to close the serial port. Any asynchronous read or Chris@16: * write operations will be cancelled immediately, and will complete with the Chris@16: * boost::asio::error::operation_aborted error. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: void close() Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().close(this->get_implementation(), ec); Chris@16: boost::asio::detail::throw_error(ec, "close"); Chris@16: } Chris@16: Chris@16: /// Close the serial port. Chris@16: /** Chris@16: * This function is used to close the serial port. Any asynchronous read or Chris@16: * write operations will be cancelled immediately, and will complete with the Chris@16: * boost::asio::error::operation_aborted error. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: */ Chris@16: boost::system::error_code close(boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().close(this->get_implementation(), ec); Chris@16: } Chris@16: Chris@16: /// (Deprecated: Use native_handle().) Get the native serial port Chris@16: /// representation. Chris@16: /** Chris@16: * This function may be used to obtain the underlying representation of the Chris@16: * serial port. This is intended to allow access to native serial port Chris@16: * functionality that is not otherwise provided. Chris@16: */ Chris@16: native_type native() Chris@16: { Chris@16: return this->get_service().native_handle(this->get_implementation()); Chris@16: } Chris@16: Chris@16: /// Get the native serial port representation. Chris@16: /** Chris@16: * This function may be used to obtain the underlying representation of the Chris@16: * serial port. This is intended to allow access to native serial port Chris@16: * functionality that is not otherwise provided. Chris@16: */ Chris@16: native_handle_type native_handle() Chris@16: { Chris@16: return this->get_service().native_handle(this->get_implementation()); Chris@16: } Chris@16: Chris@16: /// Cancel all asynchronous operations associated with the serial port. Chris@16: /** Chris@16: * This function causes all outstanding asynchronous read or write operations Chris@16: * to finish immediately, and the handlers for cancelled operations will be Chris@16: * passed the boost::asio::error::operation_aborted error. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: void cancel() Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().cancel(this->get_implementation(), ec); Chris@16: boost::asio::detail::throw_error(ec, "cancel"); Chris@16: } Chris@16: Chris@16: /// Cancel all asynchronous operations associated with the serial port. Chris@16: /** Chris@16: * This function causes all outstanding asynchronous read or write operations Chris@16: * to finish immediately, and the handlers for cancelled operations will be Chris@16: * passed the boost::asio::error::operation_aborted error. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: */ Chris@16: boost::system::error_code cancel(boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().cancel(this->get_implementation(), ec); Chris@16: } Chris@16: Chris@16: /// Send a break sequence to the serial port. Chris@16: /** Chris@16: * This function causes a break sequence of platform-specific duration to be Chris@16: * sent out the serial port. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: void send_break() Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().send_break(this->get_implementation(), ec); Chris@16: boost::asio::detail::throw_error(ec, "send_break"); Chris@16: } Chris@16: Chris@16: /// Send a break sequence to the serial port. Chris@16: /** Chris@16: * This function causes a break sequence of platform-specific duration to be Chris@16: * sent out the serial port. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: */ Chris@16: boost::system::error_code send_break(boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().send_break(this->get_implementation(), ec); Chris@16: } Chris@16: Chris@16: /// Set an option on the serial port. Chris@16: /** Chris@16: * This function is used to set an option on the serial port. Chris@16: * Chris@16: * @param option The option value to be set on the serial port. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: * Chris@16: * @sa SettableSerialPortOption @n Chris@16: * boost::asio::serial_port_base::baud_rate @n Chris@16: * boost::asio::serial_port_base::flow_control @n Chris@16: * boost::asio::serial_port_base::parity @n Chris@16: * boost::asio::serial_port_base::stop_bits @n Chris@16: * boost::asio::serial_port_base::character_size Chris@16: */ Chris@16: template Chris@16: void set_option(const SettableSerialPortOption& option) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().set_option(this->get_implementation(), option, ec); Chris@16: boost::asio::detail::throw_error(ec, "set_option"); Chris@16: } Chris@16: Chris@16: /// Set an option on the serial port. Chris@16: /** Chris@16: * This function is used to set an option on the serial port. Chris@16: * Chris@16: * @param option The option value to be set on the serial port. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: * Chris@16: * @sa SettableSerialPortOption @n Chris@16: * boost::asio::serial_port_base::baud_rate @n Chris@16: * boost::asio::serial_port_base::flow_control @n Chris@16: * boost::asio::serial_port_base::parity @n Chris@16: * boost::asio::serial_port_base::stop_bits @n Chris@16: * boost::asio::serial_port_base::character_size Chris@16: */ Chris@16: template Chris@16: boost::system::error_code set_option(const SettableSerialPortOption& option, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().set_option( Chris@16: this->get_implementation(), option, ec); Chris@16: } Chris@16: Chris@16: /// Get an option from the serial port. Chris@16: /** Chris@16: * This function is used to get the current value of an option on the serial Chris@16: * port. Chris@16: * Chris@16: * @param option The option value to be obtained from the serial port. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: * Chris@16: * @sa GettableSerialPortOption @n Chris@16: * boost::asio::serial_port_base::baud_rate @n Chris@16: * boost::asio::serial_port_base::flow_control @n Chris@16: * boost::asio::serial_port_base::parity @n Chris@16: * boost::asio::serial_port_base::stop_bits @n Chris@16: * boost::asio::serial_port_base::character_size Chris@16: */ Chris@16: template Chris@16: void get_option(GettableSerialPortOption& option) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->get_service().get_option(this->get_implementation(), option, ec); Chris@16: boost::asio::detail::throw_error(ec, "get_option"); Chris@16: } Chris@16: Chris@16: /// Get an option from the serial port. Chris@16: /** Chris@16: * This function is used to get the current value of an option on the serial Chris@16: * port. Chris@16: * Chris@16: * @param option The option value to be obtained from the serial port. Chris@16: * Chris@16: * @param ec Set to indicate what error occured, if any. Chris@16: * Chris@16: * @sa GettableSerialPortOption @n Chris@16: * boost::asio::serial_port_base::baud_rate @n Chris@16: * boost::asio::serial_port_base::flow_control @n Chris@16: * boost::asio::serial_port_base::parity @n Chris@16: * boost::asio::serial_port_base::stop_bits @n Chris@16: * boost::asio::serial_port_base::character_size Chris@16: */ Chris@16: template Chris@16: boost::system::error_code get_option(GettableSerialPortOption& option, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().get_option( Chris@16: this->get_implementation(), option, ec); Chris@16: } Chris@16: Chris@16: /// Write some data to the serial port. Chris@16: /** Chris@16: * This function is used to write data to the serial port. The function call Chris@16: * will block until one or more bytes of the data has been written Chris@16: * successfully, or until an error occurs. Chris@16: * Chris@16: * @param buffers One or more data buffers to be written to the serial port. Chris@16: * Chris@16: * @returns The number of bytes written. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. An error code of Chris@16: * boost::asio::error::eof indicates that the connection was closed by the Chris@16: * peer. Chris@16: * Chris@16: * @note The write_some operation may not transmit all of the data to the Chris@16: * peer. Consider using the @ref write function if you need to ensure that Chris@16: * all data is written before the blocking operation completes. Chris@16: * Chris@16: * @par Example Chris@16: * To write a single data buffer use the @ref buffer function as follows: Chris@16: * @code Chris@16: * serial_port.write_some(boost::asio::buffer(data, size)); Chris@16: * @endcode Chris@16: * See the @ref buffer documentation for information on writing multiple Chris@16: * buffers in one go, and how to use it with arrays, boost::array or Chris@16: * std::vector. Chris@16: */ Chris@16: template Chris@16: std::size_t write_some(const ConstBufferSequence& buffers) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: std::size_t s = this->get_service().write_some( Chris@16: this->get_implementation(), buffers, ec); Chris@16: boost::asio::detail::throw_error(ec, "write_some"); Chris@16: return s; Chris@16: } Chris@16: Chris@16: /// Write some data to the serial port. Chris@16: /** Chris@16: * This function is used to write data to the serial port. The function call Chris@16: * will block until one or more bytes of the data has been written Chris@16: * successfully, or until an error occurs. Chris@16: * Chris@16: * @param buffers One or more data buffers to be written to the serial port. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: * Chris@16: * @returns The number of bytes written. Returns 0 if an error occurred. Chris@16: * Chris@16: * @note The write_some operation may not transmit all of the data to the Chris@16: * peer. Consider using the @ref write function if you need to ensure that Chris@16: * all data is written before the blocking operation completes. Chris@16: */ Chris@16: template Chris@16: std::size_t write_some(const ConstBufferSequence& buffers, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().write_some( Chris@16: this->get_implementation(), buffers, ec); Chris@16: } Chris@16: Chris@16: /// Start an asynchronous write. Chris@16: /** Chris@16: * This function is used to asynchronously write data to the serial port. Chris@16: * The function call always returns immediately. Chris@16: * Chris@16: * @param buffers One or more data buffers to be written to the serial port. Chris@16: * Although the buffers object may be copied as necessary, ownership of the Chris@16: * underlying memory blocks is retained by the caller, which must guarantee Chris@16: * that they remain valid until the handler is called. Chris@16: * Chris@16: * @param handler The handler to be called when the write operation completes. Chris@16: * Copies will be made of the handler as required. The function signature of Chris@16: * the handler must be: Chris@16: * @code void handler( Chris@16: * const boost::system::error_code& error, // Result of operation. Chris@16: * std::size_t bytes_transferred // Number of bytes written. Chris@16: * ); @endcode Chris@16: * Regardless of whether the asynchronous operation completes immediately or Chris@16: * not, the handler will not be invoked from within this function. Invocation Chris@16: * of the handler will be performed in a manner equivalent to using Chris@16: * boost::asio::io_service::post(). Chris@16: * Chris@16: * @note The write operation may not transmit all of the data to the peer. Chris@16: * Consider using the @ref async_write function if you need to ensure that all Chris@16: * data is written before the asynchronous operation completes. Chris@16: * Chris@16: * @par Example Chris@16: * To write a single data buffer use the @ref buffer function as follows: Chris@16: * @code Chris@16: * serial_port.async_write_some(boost::asio::buffer(data, size), handler); Chris@16: * @endcode Chris@16: * See the @ref buffer documentation for information on writing multiple Chris@16: * buffers in one go, and how to use it with arrays, boost::array or Chris@16: * std::vector. Chris@16: */ Chris@16: template Chris@16: BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler, Chris@16: void (boost::system::error_code, std::size_t)) Chris@16: async_write_some(const ConstBufferSequence& buffers, Chris@16: BOOST_ASIO_MOVE_ARG(WriteHandler) handler) Chris@16: { Chris@16: // If you get an error on the following line it means that your handler does Chris@16: // not meet the documented type requirements for a WriteHandler. Chris@16: BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check; Chris@16: Chris@16: return this->get_service().async_write_some(this->get_implementation(), Chris@16: buffers, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler)); Chris@16: } Chris@16: Chris@16: /// Read some data from the serial port. Chris@16: /** Chris@16: * This function is used to read data from the serial port. The function Chris@16: * call will block until one or more bytes of data has been read successfully, Chris@16: * or until an error occurs. Chris@16: * Chris@16: * @param buffers One or more buffers into which the data will be read. Chris@16: * Chris@16: * @returns The number of bytes read. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. An error code of Chris@16: * boost::asio::error::eof indicates that the connection was closed by the Chris@16: * peer. Chris@16: * Chris@16: * @note The read_some operation may not read all of the requested number of Chris@16: * bytes. Consider using the @ref read function if you need to ensure that Chris@16: * the requested amount of data is read before the blocking operation Chris@16: * completes. Chris@16: * Chris@16: * @par Example Chris@16: * To read into a single data buffer use the @ref buffer function as follows: Chris@16: * @code Chris@16: * serial_port.read_some(boost::asio::buffer(data, size)); Chris@16: * @endcode Chris@16: * See the @ref buffer documentation for information on reading into multiple Chris@16: * buffers in one go, and how to use it with arrays, boost::array or Chris@16: * std::vector. Chris@16: */ Chris@16: template Chris@16: std::size_t read_some(const MutableBufferSequence& buffers) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: std::size_t s = this->get_service().read_some( Chris@16: this->get_implementation(), buffers, ec); Chris@16: boost::asio::detail::throw_error(ec, "read_some"); Chris@16: return s; Chris@16: } Chris@16: Chris@16: /// Read some data from the serial port. Chris@16: /** Chris@16: * This function is used to read data from the serial port. The function Chris@16: * call will block until one or more bytes of data has been read successfully, Chris@16: * or until an error occurs. Chris@16: * Chris@16: * @param buffers One or more buffers into which the data will be read. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: * Chris@16: * @returns The number of bytes read. Returns 0 if an error occurred. Chris@16: * Chris@16: * @note The read_some operation may not read all of the requested number of Chris@16: * bytes. Consider using the @ref read function if you need to ensure that Chris@16: * the requested amount of data is read before the blocking operation Chris@16: * completes. Chris@16: */ Chris@16: template Chris@16: std::size_t read_some(const MutableBufferSequence& buffers, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return this->get_service().read_some( Chris@16: this->get_implementation(), buffers, ec); Chris@16: } Chris@16: Chris@16: /// Start an asynchronous read. Chris@16: /** Chris@16: * This function is used to asynchronously read data from the serial port. Chris@16: * The function call always returns immediately. Chris@16: * Chris@16: * @param buffers One or more buffers into which the data will be read. Chris@16: * Although the buffers object may be copied as necessary, ownership of the Chris@16: * underlying memory blocks is retained by the caller, which must guarantee Chris@16: * that they remain valid until the handler is called. Chris@16: * Chris@16: * @param handler The handler to be called when the read operation completes. Chris@16: * Copies will be made of the handler as required. The function signature of Chris@16: * the handler must be: Chris@16: * @code void handler( Chris@16: * const boost::system::error_code& error, // Result of operation. Chris@16: * std::size_t bytes_transferred // Number of bytes read. Chris@16: * ); @endcode Chris@16: * Regardless of whether the asynchronous operation completes immediately or Chris@16: * not, the handler will not be invoked from within this function. Invocation Chris@16: * of the handler will be performed in a manner equivalent to using Chris@16: * boost::asio::io_service::post(). Chris@16: * Chris@16: * @note The read operation may not read all of the requested number of bytes. Chris@16: * Consider using the @ref async_read function if you need to ensure that the Chris@16: * requested amount of data is read before the asynchronous operation Chris@16: * completes. Chris@16: * Chris@16: * @par Example Chris@16: * To read into a single data buffer use the @ref buffer function as follows: Chris@16: * @code Chris@16: * serial_port.async_read_some(boost::asio::buffer(data, size), handler); Chris@16: * @endcode Chris@16: * See the @ref buffer documentation for information on reading into multiple Chris@16: * buffers in one go, and how to use it with arrays, boost::array or Chris@16: * std::vector. Chris@16: */ Chris@16: template Chris@16: BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, Chris@16: void (boost::system::error_code, std::size_t)) Chris@16: async_read_some(const MutableBufferSequence& buffers, Chris@16: BOOST_ASIO_MOVE_ARG(ReadHandler) handler) Chris@16: { Chris@16: // If you get an error on the following line it means that your handler does Chris@16: // not meet the documented type requirements for a ReadHandler. Chris@16: BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check; Chris@16: Chris@16: return this->get_service().async_read_some(this->get_implementation(), Chris@16: buffers, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler)); Chris@16: } Chris@16: }; Chris@16: Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT) Chris@16: // || defined(GENERATING_DOCUMENTATION) Chris@16: Chris@16: #endif // BOOST_ASIO_BASIC_SERIAL_PORT_HPP