$search
00001 // 00002 // basic_serial_port.hpp 00003 // ~~~~~~~~~~~~~~~~~~~~~ 00004 // 00005 // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com) 00006 // Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com) 00007 // 00008 // Distributed under the Boost Software License, Version 1.0. (See accompanying 00009 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 00010 // 00011 00012 #ifndef ASIO_BASIC_SERIAL_PORT_HPP 00013 #define ASIO_BASIC_SERIAL_PORT_HPP 00014 00015 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 00016 # pragma once 00017 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 00018 00019 #include "asio/detail/push_options.hpp" 00020 00021 #include "asio/detail/push_options.hpp" 00022 #include <string> 00023 #include <boost/config.hpp> 00024 #include "asio/detail/pop_options.hpp" 00025 00026 #include "asio/basic_io_object.hpp" 00027 #include "asio/error.hpp" 00028 #include "asio/serial_port_base.hpp" 00029 #include "asio/serial_port_service.hpp" 00030 #include "asio/detail/throw_error.hpp" 00031 00032 #if defined(ASIO_HAS_SERIAL_PORT) \ 00033 || defined(GENERATING_DOCUMENTATION) 00034 00035 namespace asio { 00036 00038 00046 template <typename SerialPortService = serial_port_service> 00047 class basic_serial_port 00048 : public basic_io_object<SerialPortService>, 00049 public serial_port_base 00050 { 00051 public: 00053 typedef typename SerialPortService::native_type native_type; 00054 00056 typedef basic_serial_port<SerialPortService> lowest_layer_type; 00057 00059 00065 explicit basic_serial_port(asio::io_service& io_service) 00066 : basic_io_object<SerialPortService>(io_service) 00067 { 00068 } 00069 00071 00081 explicit basic_serial_port(asio::io_service& io_service, 00082 const char* device) 00083 : basic_io_object<SerialPortService>(io_service) 00084 { 00085 asio::error_code ec; 00086 this->service.open(this->implementation, device, ec); 00087 asio::detail::throw_error(ec); 00088 } 00089 00091 00101 explicit basic_serial_port(asio::io_service& io_service, 00102 const std::string& device) 00103 : basic_io_object<SerialPortService>(io_service) 00104 { 00105 asio::error_code ec; 00106 this->service.open(this->implementation, device, ec); 00107 asio::detail::throw_error(ec); 00108 } 00109 00111 00122 basic_serial_port(asio::io_service& io_service, 00123 const native_type& native_serial_port) 00124 : basic_io_object<SerialPortService>(io_service) 00125 { 00126 asio::error_code ec; 00127 this->service.assign(this->implementation, native_serial_port, ec); 00128 asio::detail::throw_error(ec); 00129 } 00130 00132 00140 lowest_layer_type& lowest_layer() 00141 { 00142 return *this; 00143 } 00144 00146 00153 void open(const std::string& device) 00154 { 00155 asio::error_code ec; 00156 this->service.open(this->implementation, device, ec); 00157 asio::detail::throw_error(ec); 00158 } 00159 00161 00169 asio::error_code open(const std::string& device, 00170 asio::error_code& ec) 00171 { 00172 return this->service.open(this->implementation, device, ec); 00173 } 00174 00176 /* 00177 * This function opens the serial port to hold an existing native serial port. 00178 * 00179 * @param native_serial_port A native serial port. 00180 * 00181 * @throws asio::system_error Thrown on failure. 00182 */ 00183 void assign(const native_type& native_serial_port) 00184 { 00185 asio::error_code ec; 00186 this->service.assign(this->implementation, native_serial_port, ec); 00187 asio::detail::throw_error(ec); 00188 } 00189 00191 /* 00192 * This function opens the serial port to hold an existing native serial port. 00193 * 00194 * @param native_serial_port A native serial port. 00195 * 00196 * @param ec Set to indicate what error occurred, if any. 00197 */ 00198 asio::error_code assign(const native_type& native_serial_port, 00199 asio::error_code& ec) 00200 { 00201 return this->service.assign(this->implementation, native_serial_port, ec); 00202 } 00203 00205 bool is_open() const 00206 { 00207 return this->service.is_open(this->implementation); 00208 } 00209 00211 00218 void close() 00219 { 00220 asio::error_code ec; 00221 this->service.close(this->implementation, ec); 00222 asio::detail::throw_error(ec); 00223 } 00224 00226 00233 asio::error_code close(asio::error_code& ec) 00234 { 00235 return this->service.close(this->implementation, ec); 00236 } 00237 00239 00244 native_type native() 00245 { 00246 return this->service.native(this->implementation); 00247 } 00248 00250 00257 void cancel() 00258 { 00259 asio::error_code ec; 00260 this->service.cancel(this->implementation, ec); 00261 asio::detail::throw_error(ec); 00262 } 00263 00265 00272 asio::error_code cancel(asio::error_code& ec) 00273 { 00274 return this->service.cancel(this->implementation, ec); 00275 } 00276 00278 00284 void send_break() 00285 { 00286 asio::error_code ec; 00287 this->service.send_break(this->implementation, ec); 00288 asio::detail::throw_error(ec); 00289 } 00290 00292 00298 asio::error_code send_break(asio::error_code& ec) 00299 { 00300 return this->service.send_break(this->implementation, ec); 00301 } 00302 00304 00318 template <typename SettableSerialPortOption> 00319 void set_option(const SettableSerialPortOption& option) 00320 { 00321 asio::error_code ec; 00322 this->service.set_option(this->implementation, option, ec); 00323 asio::detail::throw_error(ec); 00324 } 00325 00327 00341 template <typename SettableSerialPortOption> 00342 asio::error_code set_option(const SettableSerialPortOption& option, 00343 asio::error_code& ec) 00344 { 00345 return this->service.set_option(this->implementation, option, ec); 00346 } 00347 00349 00364 template <typename GettableSerialPortOption> 00365 void get_option(GettableSerialPortOption& option) 00366 { 00367 asio::error_code ec; 00368 this->service.get_option(this->implementation, option, ec); 00369 asio::detail::throw_error(ec); 00370 } 00371 00373 00388 template <typename GettableSerialPortOption> 00389 asio::error_code get_option(GettableSerialPortOption& option, 00390 asio::error_code& ec) 00391 { 00392 return this->service.get_option(this->implementation, option, ec); 00393 } 00394 00396 00422 template <typename ConstBufferSequence> 00423 std::size_t write_some(const ConstBufferSequence& buffers) 00424 { 00425 asio::error_code ec; 00426 std::size_t s = this->service.write_some(this->implementation, buffers, ec); 00427 asio::detail::throw_error(ec); 00428 return s; 00429 } 00430 00432 00447 template <typename ConstBufferSequence> 00448 std::size_t write_some(const ConstBufferSequence& buffers, 00449 asio::error_code& ec) 00450 { 00451 return this->service.write_some(this->implementation, buffers, ec); 00452 } 00453 00455 00489 template <typename ConstBufferSequence, typename WriteHandler> 00490 void async_write_some(const ConstBufferSequence& buffers, 00491 WriteHandler handler) 00492 { 00493 this->service.async_write_some(this->implementation, buffers, handler); 00494 } 00495 00497 00524 template <typename MutableBufferSequence> 00525 std::size_t read_some(const MutableBufferSequence& buffers) 00526 { 00527 asio::error_code ec; 00528 std::size_t s = this->service.read_some(this->implementation, buffers, ec); 00529 asio::detail::throw_error(ec); 00530 return s; 00531 } 00532 00534 00550 template <typename MutableBufferSequence> 00551 std::size_t read_some(const MutableBufferSequence& buffers, 00552 asio::error_code& ec) 00553 { 00554 return this->service.read_some(this->implementation, buffers, ec); 00555 } 00556 00558 00593 template <typename MutableBufferSequence, typename ReadHandler> 00594 void async_read_some(const MutableBufferSequence& buffers, 00595 ReadHandler handler) 00596 { 00597 this->service.async_read_some(this->implementation, buffers, handler); 00598 } 00599 }; 00600 00601 } // namespace asio 00602 00603 #endif // defined(ASIO_HAS_SERIAL_PORT) 00604 // || defined(GENERATING_DOCUMENTATION) 00605 00606 #include "asio/detail/pop_options.hpp" 00607 00608 #endif // ASIO_BASIC_SERIAL_PORT_HPP