fix doc example typo
[boost.git] / boost / asio / basic_serial_port.hpp
blob339d5df38a16b07f378815d5deb9b6f68e3aab98
1 //
2 // basic_serial_port.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2008 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)
12 #ifndef BOOST_ASIO_BASIC_SERIAL_PORT_HPP
13 #define BOOST_ASIO_BASIC_SERIAL_PORT_HPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 # pragma once
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
19 #include <boost/asio/detail/push_options.hpp>
21 #include <boost/asio/detail/push_options.hpp>
22 #include <string>
23 #include <boost/config.hpp>
24 #include <boost/asio/detail/pop_options.hpp>
26 #include <boost/asio/basic_io_object.hpp>
27 #include <boost/asio/error.hpp>
28 #include <boost/asio/serial_port_base.hpp>
29 #include <boost/asio/serial_port_service.hpp>
30 #include <boost/asio/detail/throw_error.hpp>
32 #if defined(BOOST_ASIO_HAS_SERIAL_PORT) \
33 || defined(GENERATING_DOCUMENTATION)
35 namespace boost {
36 namespace asio {
38 /// Provides serial port functionality.
39 /**
40 * The basic_serial_port class template provides functionality that is common
41 * to all serial ports.
43 * @par Thread Safety
44 * @e Distinct @e objects: Safe.@n
45 * @e Shared @e objects: Unsafe.
47 template <typename SerialPortService = serial_port_service>
48 class basic_serial_port
49 : public basic_io_object<SerialPortService>,
50 public serial_port_base
52 public:
53 /// The native representation of a serial port.
54 typedef typename SerialPortService::native_type native_type;
56 /// A basic_serial_port is always the lowest layer.
57 typedef basic_serial_port<SerialPortService> lowest_layer_type;
59 /// Construct a basic_serial_port without opening it.
60 /**
61 * This constructor creates a serial port without opening it.
63 * @param io_service The io_service object that the serial port will use to
64 * dispatch handlers for any asynchronous operations performed on the port.
66 explicit basic_serial_port(boost::asio::io_service& io_service)
67 : basic_io_object<SerialPortService>(io_service)
71 /// Construct and open a basic_serial_port.
72 /**
73 * This constructor creates and opens a serial port for the specified device
74 * name.
76 * @param io_service The io_service object that the serial port will use to
77 * dispatch handlers for any asynchronous operations performed on the port.
79 * @param device The platform-specific device name for this serial
80 * port.
82 explicit basic_serial_port(boost::asio::io_service& io_service,
83 const char* device)
84 : basic_io_object<SerialPortService>(io_service)
86 boost::system::error_code ec;
87 this->service.open(this->implementation, device, ec);
88 boost::asio::detail::throw_error(ec);
91 /// Construct and open a basic_serial_port.
92 /**
93 * This constructor creates and opens a serial port for the specified device
94 * name.
96 * @param io_service The io_service object that the serial port will use to
97 * dispatch handlers for any asynchronous operations performed on the port.
99 * @param device The platform-specific device name for this serial
100 * port.
102 explicit basic_serial_port(boost::asio::io_service& io_service,
103 const std::string& device)
104 : basic_io_object<SerialPortService>(io_service)
106 boost::system::error_code ec;
107 this->service.open(this->implementation, device, ec);
108 boost::asio::detail::throw_error(ec);
111 /// Construct a basic_serial_port on an existing native serial port.
113 * This constructor creates a serial port object to hold an existing native
114 * serial port.
116 * @param io_service The io_service object that the serial port will use to
117 * dispatch handlers for any asynchronous operations performed on the port.
119 * @param native_serial_port A native serial port.
121 * @throws boost::system::system_error Thrown on failure.
123 basic_serial_port(boost::asio::io_service& io_service,
124 const native_type& native_serial_port)
125 : basic_io_object<SerialPortService>(io_service)
127 boost::system::error_code ec;
128 this->service.assign(this->implementation, native_serial_port, ec);
129 boost::asio::detail::throw_error(ec);
132 /// Get a reference to the lowest layer.
134 * This function returns a reference to the lowest layer in a stack of
135 * layers. Since a basic_serial_port cannot contain any further layers, it
136 * simply returns a reference to itself.
138 * @return A reference to the lowest layer in the stack of layers. Ownership
139 * is not transferred to the caller.
141 lowest_layer_type& lowest_layer()
143 return *this;
146 /// Get a const reference to the lowest layer.
148 * This function returns a const reference to the lowest layer in a stack of
149 * layers. Since a basic_serial_port cannot contain any further layers, it
150 * simply returns a reference to itself.
152 * @return A const reference to the lowest layer in the stack of layers.
153 * Ownership is not transferred to the caller.
155 const lowest_layer_type& lowest_layer() const
157 return *this;
160 /// Open the serial port using the specified device name.
162 * This function opens the serial port for the specified device name.
164 * @param device The platform-specific device name.
166 * @throws boost::system::system_error Thrown on failure.
168 void open(const std::string& device)
170 boost::system::error_code ec;
171 this->service.open(this->implementation, device, ec);
172 boost::asio::detail::throw_error(ec);
175 /// Open the serial port using the specified device name.
177 * This function opens the serial port using the given platform-specific
178 * device name.
180 * @param device The platform-specific device name.
182 * @param ec Set the indicate what error occurred, if any.
184 boost::system::error_code open(const std::string& device,
185 boost::system::error_code& ec)
187 return this->service.open(this->implementation, device, ec);
190 /// Assign an existing native serial port to the serial port.
192 * This function opens the serial port to hold an existing native serial port.
194 * @param native_serial_port A native serial port.
196 * @throws boost::system::system_error Thrown on failure.
198 void assign(const native_type& native_serial_port)
200 boost::system::error_code ec;
201 this->service.assign(this->implementation, native_serial_port, ec);
202 boost::asio::detail::throw_error(ec);
205 /// Assign an existing native serial port to the serial port.
207 * This function opens the serial port to hold an existing native serial port.
209 * @param native_serial_port A native serial port.
211 * @param ec Set to indicate what error occurred, if any.
213 boost::system::error_code assign(const native_type& native_serial_port,
214 boost::system::error_code& ec)
216 return this->service.assign(this->implementation, native_serial_port, ec);
219 /// Determine whether the serial port is open.
220 bool is_open() const
222 return this->service.is_open(this->implementation);
225 /// Close the serial port.
227 * This function is used to close the serial port. Any asynchronous read or
228 * write operations will be cancelled immediately, and will complete with the
229 * boost::asio::error::operation_aborted error.
231 * @throws boost::system::system_error Thrown on failure.
233 void close()
235 boost::system::error_code ec;
236 this->service.close(this->implementation, ec);
237 boost::asio::detail::throw_error(ec);
240 /// Close the serial port.
242 * This function is used to close the serial port. Any asynchronous read or
243 * write operations will be cancelled immediately, and will complete with the
244 * boost::asio::error::operation_aborted error.
246 * @param ec Set to indicate what error occurred, if any.
248 boost::system::error_code close(boost::system::error_code& ec)
250 return this->service.close(this->implementation, ec);
253 /// Get the native serial port representation.
255 * This function may be used to obtain the underlying representation of the
256 * serial port. This is intended to allow access to native serial port
257 * functionality that is not otherwise provided.
259 native_type native()
261 return this->service.native(this->implementation);
264 /// Cancel all asynchronous operations associated with the serial port.
266 * This function causes all outstanding asynchronous read or write operations
267 * to finish immediately, and the handlers for cancelled operations will be
268 * passed the boost::asio::error::operation_aborted error.
270 * @throws boost::system::system_error Thrown on failure.
272 void cancel()
274 boost::system::error_code ec;
275 this->service.cancel(this->implementation, ec);
276 boost::asio::detail::throw_error(ec);
279 /// Cancel all asynchronous operations associated with the serial port.
281 * This function causes all outstanding asynchronous read or write operations
282 * to finish immediately, and the handlers for cancelled operations will be
283 * passed the boost::asio::error::operation_aborted error.
285 * @param ec Set to indicate what error occurred, if any.
287 boost::system::error_code cancel(boost::system::error_code& ec)
289 return this->service.cancel(this->implementation, ec);
292 /// Send a break sequence to the serial port.
294 * This function causes a break sequence of platform-specific duration to be
295 * sent out the serial port.
297 * @throws boost::system::system_error Thrown on failure.
299 void send_break()
301 boost::system::error_code ec;
302 this->service.send_break(this->implementation, ec);
303 boost::asio::detail::throw_error(ec);
306 /// Send a break sequence to the serial port.
308 * This function causes a break sequence of platform-specific duration to be
309 * sent out the serial port.
311 * @param ec Set to indicate what error occurred, if any.
313 boost::system::error_code send_break(boost::system::error_code& ec)
315 return this->service.send_break(this->implementation, ec);
318 /// Set an option on the serial port.
320 * This function is used to set an option on the serial port.
322 * @param option The option value to be set on the serial port.
324 * @throws boost::system::system_error Thrown on failure.
326 * @sa SettableSerialPortOption @n
327 * boost::asio::serial_port_base::baud_rate @n
328 * boost::asio::serial_port_base::flow_control @n
329 * boost::asio::serial_port_base::parity @n
330 * boost::asio::serial_port_base::stop_bits @n
331 * boost::asio::serial_port_base::character_size
333 template <typename SettableSerialPortOption>
334 void set_option(const SettableSerialPortOption& option)
336 boost::system::error_code ec;
337 this->service.set_option(this->implementation, option, ec);
338 boost::asio::detail::throw_error(ec);
341 /// Set an option on the serial port.
343 * This function is used to set an option on the serial port.
345 * @param option The option value to be set on the serial port.
347 * @param ec Set to indicate what error occurred, if any.
349 * @sa SettableSerialPortOption @n
350 * boost::asio::serial_port_base::baud_rate @n
351 * boost::asio::serial_port_base::flow_control @n
352 * boost::asio::serial_port_base::parity @n
353 * boost::asio::serial_port_base::stop_bits @n
354 * boost::asio::serial_port_base::character_size
356 template <typename SettableSerialPortOption>
357 boost::system::error_code set_option(const SettableSerialPortOption& option,
358 boost::system::error_code& ec)
360 return this->service.set_option(this->implementation, option, ec);
363 /// Get an option from the serial port.
365 * This function is used to get the current value of an option on the serial
366 * port.
368 * @param option The option value to be obtained from the serial port.
370 * @throws boost::system::system_error Thrown on failure.
372 * @sa GettableSerialPortOption @n
373 * boost::asio::serial_port_base::baud_rate @n
374 * boost::asio::serial_port_base::flow_control @n
375 * boost::asio::serial_port_base::parity @n
376 * boost::asio::serial_port_base::stop_bits @n
377 * boost::asio::serial_port_base::character_size
379 template <typename GettableSerialPortOption>
380 void get_option(GettableSerialPortOption& option)
382 boost::system::error_code ec;
383 this->service.get_option(this->implementation, option, ec);
384 boost::asio::detail::throw_error(ec);
387 /// Get an option from the serial port.
389 * This function is used to get the current value of an option on the serial
390 * port.
392 * @param option The option value to be obtained from the serial port.
394 * @param ec Set to indicate what error occured, if any.
396 * @sa GettableSerialPortOption @n
397 * boost::asio::serial_port_base::baud_rate @n
398 * boost::asio::serial_port_base::flow_control @n
399 * boost::asio::serial_port_base::parity @n
400 * boost::asio::serial_port_base::stop_bits @n
401 * boost::asio::serial_port_base::character_size
403 template <typename GettableSerialPortOption>
404 boost::system::error_code get_option(GettableSerialPortOption& option,
405 boost::system::error_code& ec)
407 return this->service.get_option(this->implementation, option, ec);
410 /// Write some data to the serial port.
412 * This function is used to write data to the serial port. The function call
413 * will block until one or more bytes of the data has been written
414 * successfully, or until an error occurs.
416 * @param buffers One or more data buffers to be written to the serial port.
418 * @returns The number of bytes written.
420 * @throws boost::system::system_error Thrown on failure. An error code of
421 * boost::asio::error::eof indicates that the connection was closed by the
422 * peer.
424 * @note The write_some operation may not transmit all of the data to the
425 * peer. Consider using the @ref write function if you need to ensure that
426 * all data is written before the blocking operation completes.
428 * @par Example
429 * To write a single data buffer use the @ref buffer function as follows:
430 * @code
431 * serial_port.write_some(boost::asio::buffer(data, size));
432 * @endcode
433 * See the @ref buffer documentation for information on writing multiple
434 * buffers in one go, and how to use it with arrays, boost::array or
435 * std::vector.
437 template <typename ConstBufferSequence>
438 std::size_t write_some(const ConstBufferSequence& buffers)
440 boost::system::error_code ec;
441 std::size_t s = this->service.write_some(this->implementation, buffers, ec);
442 boost::asio::detail::throw_error(ec);
443 return s;
446 /// Write some data to the serial port.
448 * This function is used to write data to the serial port. The function call
449 * will block until one or more bytes of the data has been written
450 * successfully, or until an error occurs.
452 * @param buffers One or more data buffers to be written to the serial port.
454 * @param ec Set to indicate what error occurred, if any.
456 * @returns The number of bytes written. Returns 0 if an error occurred.
458 * @note The write_some operation may not transmit all of the data to the
459 * peer. Consider using the @ref write function if you need to ensure that
460 * all data is written before the blocking operation completes.
462 template <typename ConstBufferSequence>
463 std::size_t write_some(const ConstBufferSequence& buffers,
464 boost::system::error_code& ec)
466 return this->service.write_some(this->implementation, buffers, ec);
469 /// Start an asynchronous write.
471 * This function is used to asynchronously write data to the serial port.
472 * The function call always returns immediately.
474 * @param buffers One or more data buffers to be written to the serial port.
475 * Although the buffers object may be copied as necessary, ownership of the
476 * underlying memory blocks is retained by the caller, which must guarantee
477 * that they remain valid until the handler is called.
479 * @param handler The handler to be called when the write operation completes.
480 * Copies will be made of the handler as required. The function signature of
481 * the handler must be:
482 * @code void handler(
483 * const boost::system::error_code& error, // Result of operation.
484 * std::size_t bytes_transferred // Number of bytes written.
485 * ); @endcode
486 * Regardless of whether the asynchronous operation completes immediately or
487 * not, the handler will not be invoked from within this function. Invocation
488 * of the handler will be performed in a manner equivalent to using
489 * boost::asio::io_service::post().
491 * @note The write operation may not transmit all of the data to the peer.
492 * Consider using the @ref async_write function if you need to ensure that all
493 * data is written before the asynchronous operation completes.
495 * @par Example
496 * To write a single data buffer use the @ref buffer function as follows:
497 * @code
498 * serial_port.async_write_some(boost::asio::buffer(data, size), handler);
499 * @endcode
500 * See the @ref buffer documentation for information on writing multiple
501 * buffers in one go, and how to use it with arrays, boost::array or
502 * std::vector.
504 template <typename ConstBufferSequence, typename WriteHandler>
505 void async_write_some(const ConstBufferSequence& buffers,
506 WriteHandler handler)
508 this->service.async_write_some(this->implementation, buffers, handler);
511 /// Read some data from the serial port.
513 * This function is used to read data from the serial port. The function
514 * call will block until one or more bytes of data has been read successfully,
515 * or until an error occurs.
517 * @param buffers One or more buffers into which the data will be read.
519 * @returns The number of bytes read.
521 * @throws boost::system::system_error Thrown on failure. An error code of
522 * boost::asio::error::eof indicates that the connection was closed by the
523 * peer.
525 * @note The read_some operation may not read all of the requested number of
526 * bytes. Consider using the @ref read function if you need to ensure that
527 * the requested amount of data is read before the blocking operation
528 * completes.
530 * @par Example
531 * To read into a single data buffer use the @ref buffer function as follows:
532 * @code
533 * serial_port.read_some(boost::asio::buffer(data, size));
534 * @endcode
535 * See the @ref buffer documentation for information on reading into multiple
536 * buffers in one go, and how to use it with arrays, boost::array or
537 * std::vector.
539 template <typename MutableBufferSequence>
540 std::size_t read_some(const MutableBufferSequence& buffers)
542 boost::system::error_code ec;
543 std::size_t s = this->service.read_some(this->implementation, buffers, ec);
544 boost::asio::detail::throw_error(ec);
545 return s;
548 /// Read some data from the serial port.
550 * This function is used to read data from the serial port. The function
551 * call will block until one or more bytes of data has been read successfully,
552 * or until an error occurs.
554 * @param buffers One or more buffers into which the data will be read.
556 * @param ec Set to indicate what error occurred, if any.
558 * @returns The number of bytes read. Returns 0 if an error occurred.
560 * @note The read_some operation may not read all of the requested number of
561 * bytes. Consider using the @ref read function if you need to ensure that
562 * the requested amount of data is read before the blocking operation
563 * completes.
565 template <typename MutableBufferSequence>
566 std::size_t read_some(const MutableBufferSequence& buffers,
567 boost::system::error_code& ec)
569 return this->service.read_some(this->implementation, buffers, ec);
572 /// Start an asynchronous read.
574 * This function is used to asynchronously read data from the serial port.
575 * The function call always returns immediately.
577 * @param buffers One or more buffers into which the data will be read.
578 * Although the buffers object may be copied as necessary, ownership of the
579 * underlying memory blocks is retained by the caller, which must guarantee
580 * that they remain valid until the handler is called.
582 * @param handler The handler to be called when the read operation completes.
583 * Copies will be made of the handler as required. The function signature of
584 * the handler must be:
585 * @code void handler(
586 * const boost::system::error_code& error, // Result of operation.
587 * std::size_t bytes_transferred // Number of bytes read.
588 * ); @endcode
589 * Regardless of whether the asynchronous operation completes immediately or
590 * not, the handler will not be invoked from within this function. Invocation
591 * of the handler will be performed in a manner equivalent to using
592 * boost::asio::io_service::post().
594 * @note The read operation may not read all of the requested number of bytes.
595 * Consider using the @ref async_read function if you need to ensure that the
596 * requested amount of data is read before the asynchronous operation
597 * completes.
599 * @par Example
600 * To read into a single data buffer use the @ref buffer function as follows:
601 * @code
602 * serial_port.async_read_some(boost::asio::buffer(data, size), handler);
603 * @endcode
604 * See the @ref buffer documentation for information on reading into multiple
605 * buffers in one go, and how to use it with arrays, boost::array or
606 * std::vector.
608 template <typename MutableBufferSequence, typename ReadHandler>
609 void async_read_some(const MutableBufferSequence& buffers,
610 ReadHandler handler)
612 this->service.async_read_some(this->implementation, buffers, handler);
616 } // namespace asio
617 } // namespace boost
619 #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT)
620 // || defined(GENERATING_DOCUMENTATION)
622 #include <boost/asio/detail/pop_options.hpp>
624 #endif // BOOST_ASIO_BASIC_SERIAL_PORT_HPP