Chris@16
|
1 //
|
Chris@16
|
2 // basic_serial_port.hpp
|
Chris@16
|
3 // ~~~~~~~~~~~~~~~~~~~~~
|
Chris@16
|
4 //
|
Chris@101
|
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
Chris@16
|
6 // Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com)
|
Chris@16
|
7 //
|
Chris@16
|
8 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
9 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
10 //
|
Chris@16
|
11
|
Chris@16
|
12 #ifndef BOOST_ASIO_BASIC_SERIAL_PORT_HPP
|
Chris@16
|
13 #define BOOST_ASIO_BASIC_SERIAL_PORT_HPP
|
Chris@16
|
14
|
Chris@16
|
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
16 # pragma once
|
Chris@16
|
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
18
|
Chris@16
|
19 #include <boost/asio/detail/config.hpp>
|
Chris@16
|
20
|
Chris@16
|
21 #if defined(BOOST_ASIO_HAS_SERIAL_PORT) \
|
Chris@16
|
22 || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
23
|
Chris@16
|
24 #include <string>
|
Chris@16
|
25 #include <boost/asio/basic_io_object.hpp>
|
Chris@16
|
26 #include <boost/asio/detail/handler_type_requirements.hpp>
|
Chris@16
|
27 #include <boost/asio/detail/throw_error.hpp>
|
Chris@16
|
28 #include <boost/asio/error.hpp>
|
Chris@16
|
29 #include <boost/asio/serial_port_base.hpp>
|
Chris@16
|
30 #include <boost/asio/serial_port_service.hpp>
|
Chris@16
|
31
|
Chris@16
|
32 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
33
|
Chris@16
|
34 namespace boost {
|
Chris@16
|
35 namespace asio {
|
Chris@16
|
36
|
Chris@16
|
37 /// Provides serial port functionality.
|
Chris@16
|
38 /**
|
Chris@16
|
39 * The basic_serial_port class template provides functionality that is common
|
Chris@16
|
40 * to all serial ports.
|
Chris@16
|
41 *
|
Chris@16
|
42 * @par Thread Safety
|
Chris@16
|
43 * @e Distinct @e objects: Safe.@n
|
Chris@16
|
44 * @e Shared @e objects: Unsafe.
|
Chris@16
|
45 */
|
Chris@16
|
46 template <typename SerialPortService = serial_port_service>
|
Chris@16
|
47 class basic_serial_port
|
Chris@16
|
48 : public basic_io_object<SerialPortService>,
|
Chris@16
|
49 public serial_port_base
|
Chris@16
|
50 {
|
Chris@16
|
51 public:
|
Chris@16
|
52 /// (Deprecated: Use native_handle_type.) The native representation of a
|
Chris@16
|
53 /// serial port.
|
Chris@16
|
54 typedef typename SerialPortService::native_handle_type native_type;
|
Chris@16
|
55
|
Chris@16
|
56 /// The native representation of a serial port.
|
Chris@16
|
57 typedef typename SerialPortService::native_handle_type native_handle_type;
|
Chris@16
|
58
|
Chris@16
|
59 /// A basic_serial_port is always the lowest layer.
|
Chris@16
|
60 typedef basic_serial_port<SerialPortService> lowest_layer_type;
|
Chris@16
|
61
|
Chris@16
|
62 /// Construct a basic_serial_port without opening it.
|
Chris@16
|
63 /**
|
Chris@16
|
64 * This constructor creates a serial port without opening it.
|
Chris@16
|
65 *
|
Chris@16
|
66 * @param io_service The io_service object that the serial port will use to
|
Chris@16
|
67 * dispatch handlers for any asynchronous operations performed on the port.
|
Chris@16
|
68 */
|
Chris@16
|
69 explicit basic_serial_port(boost::asio::io_service& io_service)
|
Chris@16
|
70 : basic_io_object<SerialPortService>(io_service)
|
Chris@16
|
71 {
|
Chris@16
|
72 }
|
Chris@16
|
73
|
Chris@16
|
74 /// Construct and open a basic_serial_port.
|
Chris@16
|
75 /**
|
Chris@16
|
76 * This constructor creates and opens a serial port for the specified device
|
Chris@16
|
77 * name.
|
Chris@16
|
78 *
|
Chris@16
|
79 * @param io_service The io_service object that the serial port will use to
|
Chris@16
|
80 * dispatch handlers for any asynchronous operations performed on the port.
|
Chris@16
|
81 *
|
Chris@16
|
82 * @param device The platform-specific device name for this serial
|
Chris@16
|
83 * port.
|
Chris@16
|
84 */
|
Chris@16
|
85 explicit basic_serial_port(boost::asio::io_service& io_service,
|
Chris@16
|
86 const char* device)
|
Chris@16
|
87 : basic_io_object<SerialPortService>(io_service)
|
Chris@16
|
88 {
|
Chris@16
|
89 boost::system::error_code ec;
|
Chris@16
|
90 this->get_service().open(this->get_implementation(), device, ec);
|
Chris@16
|
91 boost::asio::detail::throw_error(ec, "open");
|
Chris@16
|
92 }
|
Chris@16
|
93
|
Chris@16
|
94 /// Construct and open a basic_serial_port.
|
Chris@16
|
95 /**
|
Chris@16
|
96 * This constructor creates and opens a serial port for the specified device
|
Chris@16
|
97 * name.
|
Chris@16
|
98 *
|
Chris@16
|
99 * @param io_service The io_service object that the serial port will use to
|
Chris@16
|
100 * dispatch handlers for any asynchronous operations performed on the port.
|
Chris@16
|
101 *
|
Chris@16
|
102 * @param device The platform-specific device name for this serial
|
Chris@16
|
103 * port.
|
Chris@16
|
104 */
|
Chris@16
|
105 explicit basic_serial_port(boost::asio::io_service& io_service,
|
Chris@16
|
106 const std::string& device)
|
Chris@16
|
107 : basic_io_object<SerialPortService>(io_service)
|
Chris@16
|
108 {
|
Chris@16
|
109 boost::system::error_code ec;
|
Chris@16
|
110 this->get_service().open(this->get_implementation(), device, ec);
|
Chris@16
|
111 boost::asio::detail::throw_error(ec, "open");
|
Chris@16
|
112 }
|
Chris@16
|
113
|
Chris@16
|
114 /// Construct a basic_serial_port on an existing native serial port.
|
Chris@16
|
115 /**
|
Chris@16
|
116 * This constructor creates a serial port object to hold an existing native
|
Chris@16
|
117 * serial port.
|
Chris@16
|
118 *
|
Chris@16
|
119 * @param io_service The io_service object that the serial port will use to
|
Chris@16
|
120 * dispatch handlers for any asynchronous operations performed on the port.
|
Chris@16
|
121 *
|
Chris@16
|
122 * @param native_serial_port A native serial port.
|
Chris@16
|
123 *
|
Chris@16
|
124 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
125 */
|
Chris@16
|
126 basic_serial_port(boost::asio::io_service& io_service,
|
Chris@16
|
127 const native_handle_type& native_serial_port)
|
Chris@16
|
128 : basic_io_object<SerialPortService>(io_service)
|
Chris@16
|
129 {
|
Chris@16
|
130 boost::system::error_code ec;
|
Chris@16
|
131 this->get_service().assign(this->get_implementation(),
|
Chris@16
|
132 native_serial_port, ec);
|
Chris@16
|
133 boost::asio::detail::throw_error(ec, "assign");
|
Chris@16
|
134 }
|
Chris@16
|
135
|
Chris@16
|
136 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
137 /// Move-construct a basic_serial_port from another.
|
Chris@16
|
138 /**
|
Chris@16
|
139 * This constructor moves a serial port from one object to another.
|
Chris@16
|
140 *
|
Chris@16
|
141 * @param other The other basic_serial_port object from which the move will
|
Chris@16
|
142 * occur.
|
Chris@16
|
143 *
|
Chris@16
|
144 * @note Following the move, the moved-from object is in the same state as if
|
Chris@16
|
145 * constructed using the @c basic_serial_port(io_service&) constructor.
|
Chris@16
|
146 */
|
Chris@16
|
147 basic_serial_port(basic_serial_port&& other)
|
Chris@16
|
148 : basic_io_object<SerialPortService>(
|
Chris@16
|
149 BOOST_ASIO_MOVE_CAST(basic_serial_port)(other))
|
Chris@16
|
150 {
|
Chris@16
|
151 }
|
Chris@16
|
152
|
Chris@16
|
153 /// Move-assign a basic_serial_port from another.
|
Chris@16
|
154 /**
|
Chris@16
|
155 * This assignment operator moves a serial port from one object to another.
|
Chris@16
|
156 *
|
Chris@16
|
157 * @param other The other basic_serial_port object from which the move will
|
Chris@16
|
158 * occur.
|
Chris@16
|
159 *
|
Chris@16
|
160 * @note Following the move, the moved-from object is in the same state as if
|
Chris@16
|
161 * constructed using the @c basic_serial_port(io_service&) constructor.
|
Chris@16
|
162 */
|
Chris@16
|
163 basic_serial_port& operator=(basic_serial_port&& other)
|
Chris@16
|
164 {
|
Chris@16
|
165 basic_io_object<SerialPortService>::operator=(
|
Chris@16
|
166 BOOST_ASIO_MOVE_CAST(basic_serial_port)(other));
|
Chris@16
|
167 return *this;
|
Chris@16
|
168 }
|
Chris@16
|
169 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
170
|
Chris@16
|
171 /// Get a reference to the lowest layer.
|
Chris@16
|
172 /**
|
Chris@16
|
173 * This function returns a reference to the lowest layer in a stack of
|
Chris@16
|
174 * layers. Since a basic_serial_port cannot contain any further layers, it
|
Chris@16
|
175 * simply returns a reference to itself.
|
Chris@16
|
176 *
|
Chris@16
|
177 * @return A reference to the lowest layer in the stack of layers. Ownership
|
Chris@16
|
178 * is not transferred to the caller.
|
Chris@16
|
179 */
|
Chris@16
|
180 lowest_layer_type& lowest_layer()
|
Chris@16
|
181 {
|
Chris@16
|
182 return *this;
|
Chris@16
|
183 }
|
Chris@16
|
184
|
Chris@16
|
185 /// Get a const reference to the lowest layer.
|
Chris@16
|
186 /**
|
Chris@16
|
187 * This function returns a const reference to the lowest layer in a stack of
|
Chris@16
|
188 * layers. Since a basic_serial_port cannot contain any further layers, it
|
Chris@16
|
189 * simply returns a reference to itself.
|
Chris@16
|
190 *
|
Chris@16
|
191 * @return A const reference to the lowest layer in the stack of layers.
|
Chris@16
|
192 * Ownership is not transferred to the caller.
|
Chris@16
|
193 */
|
Chris@16
|
194 const lowest_layer_type& lowest_layer() const
|
Chris@16
|
195 {
|
Chris@16
|
196 return *this;
|
Chris@16
|
197 }
|
Chris@16
|
198
|
Chris@16
|
199 /// Open the serial port using the specified device name.
|
Chris@16
|
200 /**
|
Chris@16
|
201 * This function opens the serial port for the specified device name.
|
Chris@16
|
202 *
|
Chris@16
|
203 * @param device The platform-specific device name.
|
Chris@16
|
204 *
|
Chris@16
|
205 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
206 */
|
Chris@16
|
207 void open(const std::string& device)
|
Chris@16
|
208 {
|
Chris@16
|
209 boost::system::error_code ec;
|
Chris@16
|
210 this->get_service().open(this->get_implementation(), device, ec);
|
Chris@16
|
211 boost::asio::detail::throw_error(ec, "open");
|
Chris@16
|
212 }
|
Chris@16
|
213
|
Chris@16
|
214 /// Open the serial port using the specified device name.
|
Chris@16
|
215 /**
|
Chris@16
|
216 * This function opens the serial port using the given platform-specific
|
Chris@16
|
217 * device name.
|
Chris@16
|
218 *
|
Chris@16
|
219 * @param device The platform-specific device name.
|
Chris@16
|
220 *
|
Chris@16
|
221 * @param ec Set the indicate what error occurred, if any.
|
Chris@16
|
222 */
|
Chris@16
|
223 boost::system::error_code open(const std::string& device,
|
Chris@16
|
224 boost::system::error_code& ec)
|
Chris@16
|
225 {
|
Chris@16
|
226 return this->get_service().open(this->get_implementation(), device, ec);
|
Chris@16
|
227 }
|
Chris@16
|
228
|
Chris@16
|
229 /// Assign an existing native serial port to the serial port.
|
Chris@16
|
230 /*
|
Chris@16
|
231 * This function opens the serial port to hold an existing native serial port.
|
Chris@16
|
232 *
|
Chris@16
|
233 * @param native_serial_port A native serial port.
|
Chris@16
|
234 *
|
Chris@16
|
235 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
236 */
|
Chris@16
|
237 void assign(const native_handle_type& native_serial_port)
|
Chris@16
|
238 {
|
Chris@16
|
239 boost::system::error_code ec;
|
Chris@16
|
240 this->get_service().assign(this->get_implementation(),
|
Chris@16
|
241 native_serial_port, ec);
|
Chris@16
|
242 boost::asio::detail::throw_error(ec, "assign");
|
Chris@16
|
243 }
|
Chris@16
|
244
|
Chris@16
|
245 /// Assign an existing native serial port to the serial port.
|
Chris@16
|
246 /*
|
Chris@16
|
247 * This function opens the serial port to hold an existing native serial port.
|
Chris@16
|
248 *
|
Chris@16
|
249 * @param native_serial_port A native serial port.
|
Chris@16
|
250 *
|
Chris@16
|
251 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
252 */
|
Chris@16
|
253 boost::system::error_code assign(const native_handle_type& native_serial_port,
|
Chris@16
|
254 boost::system::error_code& ec)
|
Chris@16
|
255 {
|
Chris@16
|
256 return this->get_service().assign(this->get_implementation(),
|
Chris@16
|
257 native_serial_port, ec);
|
Chris@16
|
258 }
|
Chris@16
|
259
|
Chris@16
|
260 /// Determine whether the serial port is open.
|
Chris@16
|
261 bool is_open() const
|
Chris@16
|
262 {
|
Chris@16
|
263 return this->get_service().is_open(this->get_implementation());
|
Chris@16
|
264 }
|
Chris@16
|
265
|
Chris@16
|
266 /// Close the serial port.
|
Chris@16
|
267 /**
|
Chris@16
|
268 * This function is used to close the serial port. Any asynchronous read or
|
Chris@16
|
269 * write operations will be cancelled immediately, and will complete with the
|
Chris@16
|
270 * boost::asio::error::operation_aborted error.
|
Chris@16
|
271 *
|
Chris@16
|
272 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
273 */
|
Chris@16
|
274 void close()
|
Chris@16
|
275 {
|
Chris@16
|
276 boost::system::error_code ec;
|
Chris@16
|
277 this->get_service().close(this->get_implementation(), ec);
|
Chris@16
|
278 boost::asio::detail::throw_error(ec, "close");
|
Chris@16
|
279 }
|
Chris@16
|
280
|
Chris@16
|
281 /// Close the serial port.
|
Chris@16
|
282 /**
|
Chris@16
|
283 * This function is used to close the serial port. Any asynchronous read or
|
Chris@16
|
284 * write operations will be cancelled immediately, and will complete with the
|
Chris@16
|
285 * boost::asio::error::operation_aborted error.
|
Chris@16
|
286 *
|
Chris@16
|
287 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
288 */
|
Chris@16
|
289 boost::system::error_code close(boost::system::error_code& ec)
|
Chris@16
|
290 {
|
Chris@16
|
291 return this->get_service().close(this->get_implementation(), ec);
|
Chris@16
|
292 }
|
Chris@16
|
293
|
Chris@16
|
294 /// (Deprecated: Use native_handle().) Get the native serial port
|
Chris@16
|
295 /// representation.
|
Chris@16
|
296 /**
|
Chris@16
|
297 * This function may be used to obtain the underlying representation of the
|
Chris@16
|
298 * serial port. This is intended to allow access to native serial port
|
Chris@16
|
299 * functionality that is not otherwise provided.
|
Chris@16
|
300 */
|
Chris@16
|
301 native_type native()
|
Chris@16
|
302 {
|
Chris@16
|
303 return this->get_service().native_handle(this->get_implementation());
|
Chris@16
|
304 }
|
Chris@16
|
305
|
Chris@16
|
306 /// Get the native serial port representation.
|
Chris@16
|
307 /**
|
Chris@16
|
308 * This function may be used to obtain the underlying representation of the
|
Chris@16
|
309 * serial port. This is intended to allow access to native serial port
|
Chris@16
|
310 * functionality that is not otherwise provided.
|
Chris@16
|
311 */
|
Chris@16
|
312 native_handle_type native_handle()
|
Chris@16
|
313 {
|
Chris@16
|
314 return this->get_service().native_handle(this->get_implementation());
|
Chris@16
|
315 }
|
Chris@16
|
316
|
Chris@16
|
317 /// Cancel all asynchronous operations associated with the serial port.
|
Chris@16
|
318 /**
|
Chris@16
|
319 * This function causes all outstanding asynchronous read or write operations
|
Chris@16
|
320 * to finish immediately, and the handlers for cancelled operations will be
|
Chris@16
|
321 * passed the boost::asio::error::operation_aborted error.
|
Chris@16
|
322 *
|
Chris@16
|
323 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
324 */
|
Chris@16
|
325 void cancel()
|
Chris@16
|
326 {
|
Chris@16
|
327 boost::system::error_code ec;
|
Chris@16
|
328 this->get_service().cancel(this->get_implementation(), ec);
|
Chris@16
|
329 boost::asio::detail::throw_error(ec, "cancel");
|
Chris@16
|
330 }
|
Chris@16
|
331
|
Chris@16
|
332 /// Cancel all asynchronous operations associated with the serial port.
|
Chris@16
|
333 /**
|
Chris@16
|
334 * This function causes all outstanding asynchronous read or write operations
|
Chris@16
|
335 * to finish immediately, and the handlers for cancelled operations will be
|
Chris@16
|
336 * passed the boost::asio::error::operation_aborted error.
|
Chris@16
|
337 *
|
Chris@16
|
338 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
339 */
|
Chris@16
|
340 boost::system::error_code cancel(boost::system::error_code& ec)
|
Chris@16
|
341 {
|
Chris@16
|
342 return this->get_service().cancel(this->get_implementation(), ec);
|
Chris@16
|
343 }
|
Chris@16
|
344
|
Chris@16
|
345 /// Send a break sequence to the serial port.
|
Chris@16
|
346 /**
|
Chris@16
|
347 * This function causes a break sequence of platform-specific duration to be
|
Chris@16
|
348 * sent out the serial port.
|
Chris@16
|
349 *
|
Chris@16
|
350 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
351 */
|
Chris@16
|
352 void send_break()
|
Chris@16
|
353 {
|
Chris@16
|
354 boost::system::error_code ec;
|
Chris@16
|
355 this->get_service().send_break(this->get_implementation(), ec);
|
Chris@16
|
356 boost::asio::detail::throw_error(ec, "send_break");
|
Chris@16
|
357 }
|
Chris@16
|
358
|
Chris@16
|
359 /// Send a break sequence to the serial port.
|
Chris@16
|
360 /**
|
Chris@16
|
361 * This function causes a break sequence of platform-specific duration to be
|
Chris@16
|
362 * sent out the serial port.
|
Chris@16
|
363 *
|
Chris@16
|
364 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
365 */
|
Chris@16
|
366 boost::system::error_code send_break(boost::system::error_code& ec)
|
Chris@16
|
367 {
|
Chris@16
|
368 return this->get_service().send_break(this->get_implementation(), ec);
|
Chris@16
|
369 }
|
Chris@16
|
370
|
Chris@16
|
371 /// Set an option on the serial port.
|
Chris@16
|
372 /**
|
Chris@16
|
373 * This function is used to set an option on the serial port.
|
Chris@16
|
374 *
|
Chris@16
|
375 * @param option The option value to be set on the serial port.
|
Chris@16
|
376 *
|
Chris@16
|
377 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
378 *
|
Chris@16
|
379 * @sa SettableSerialPortOption @n
|
Chris@16
|
380 * boost::asio::serial_port_base::baud_rate @n
|
Chris@16
|
381 * boost::asio::serial_port_base::flow_control @n
|
Chris@16
|
382 * boost::asio::serial_port_base::parity @n
|
Chris@16
|
383 * boost::asio::serial_port_base::stop_bits @n
|
Chris@16
|
384 * boost::asio::serial_port_base::character_size
|
Chris@16
|
385 */
|
Chris@16
|
386 template <typename SettableSerialPortOption>
|
Chris@16
|
387 void set_option(const SettableSerialPortOption& option)
|
Chris@16
|
388 {
|
Chris@16
|
389 boost::system::error_code ec;
|
Chris@16
|
390 this->get_service().set_option(this->get_implementation(), option, ec);
|
Chris@16
|
391 boost::asio::detail::throw_error(ec, "set_option");
|
Chris@16
|
392 }
|
Chris@16
|
393
|
Chris@16
|
394 /// Set an option on the serial port.
|
Chris@16
|
395 /**
|
Chris@16
|
396 * This function is used to set an option on the serial port.
|
Chris@16
|
397 *
|
Chris@16
|
398 * @param option The option value to be set on the serial port.
|
Chris@16
|
399 *
|
Chris@16
|
400 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
401 *
|
Chris@16
|
402 * @sa SettableSerialPortOption @n
|
Chris@16
|
403 * boost::asio::serial_port_base::baud_rate @n
|
Chris@16
|
404 * boost::asio::serial_port_base::flow_control @n
|
Chris@16
|
405 * boost::asio::serial_port_base::parity @n
|
Chris@16
|
406 * boost::asio::serial_port_base::stop_bits @n
|
Chris@16
|
407 * boost::asio::serial_port_base::character_size
|
Chris@16
|
408 */
|
Chris@16
|
409 template <typename SettableSerialPortOption>
|
Chris@16
|
410 boost::system::error_code set_option(const SettableSerialPortOption& option,
|
Chris@16
|
411 boost::system::error_code& ec)
|
Chris@16
|
412 {
|
Chris@16
|
413 return this->get_service().set_option(
|
Chris@16
|
414 this->get_implementation(), option, ec);
|
Chris@16
|
415 }
|
Chris@16
|
416
|
Chris@16
|
417 /// Get an option from the serial port.
|
Chris@16
|
418 /**
|
Chris@16
|
419 * This function is used to get the current value of an option on the serial
|
Chris@16
|
420 * port.
|
Chris@16
|
421 *
|
Chris@16
|
422 * @param option The option value to be obtained from the serial port.
|
Chris@16
|
423 *
|
Chris@16
|
424 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
425 *
|
Chris@16
|
426 * @sa GettableSerialPortOption @n
|
Chris@16
|
427 * boost::asio::serial_port_base::baud_rate @n
|
Chris@16
|
428 * boost::asio::serial_port_base::flow_control @n
|
Chris@16
|
429 * boost::asio::serial_port_base::parity @n
|
Chris@16
|
430 * boost::asio::serial_port_base::stop_bits @n
|
Chris@16
|
431 * boost::asio::serial_port_base::character_size
|
Chris@16
|
432 */
|
Chris@16
|
433 template <typename GettableSerialPortOption>
|
Chris@16
|
434 void get_option(GettableSerialPortOption& option)
|
Chris@16
|
435 {
|
Chris@16
|
436 boost::system::error_code ec;
|
Chris@16
|
437 this->get_service().get_option(this->get_implementation(), option, ec);
|
Chris@16
|
438 boost::asio::detail::throw_error(ec, "get_option");
|
Chris@16
|
439 }
|
Chris@16
|
440
|
Chris@16
|
441 /// Get an option from the serial port.
|
Chris@16
|
442 /**
|
Chris@16
|
443 * This function is used to get the current value of an option on the serial
|
Chris@16
|
444 * port.
|
Chris@16
|
445 *
|
Chris@16
|
446 * @param option The option value to be obtained from the serial port.
|
Chris@16
|
447 *
|
Chris@16
|
448 * @param ec Set to indicate what error occured, if any.
|
Chris@16
|
449 *
|
Chris@16
|
450 * @sa GettableSerialPortOption @n
|
Chris@16
|
451 * boost::asio::serial_port_base::baud_rate @n
|
Chris@16
|
452 * boost::asio::serial_port_base::flow_control @n
|
Chris@16
|
453 * boost::asio::serial_port_base::parity @n
|
Chris@16
|
454 * boost::asio::serial_port_base::stop_bits @n
|
Chris@16
|
455 * boost::asio::serial_port_base::character_size
|
Chris@16
|
456 */
|
Chris@16
|
457 template <typename GettableSerialPortOption>
|
Chris@16
|
458 boost::system::error_code get_option(GettableSerialPortOption& option,
|
Chris@16
|
459 boost::system::error_code& ec)
|
Chris@16
|
460 {
|
Chris@16
|
461 return this->get_service().get_option(
|
Chris@16
|
462 this->get_implementation(), option, ec);
|
Chris@16
|
463 }
|
Chris@16
|
464
|
Chris@16
|
465 /// Write some data to the serial port.
|
Chris@16
|
466 /**
|
Chris@16
|
467 * This function is used to write data to the serial port. The function call
|
Chris@16
|
468 * will block until one or more bytes of the data has been written
|
Chris@16
|
469 * successfully, or until an error occurs.
|
Chris@16
|
470 *
|
Chris@16
|
471 * @param buffers One or more data buffers to be written to the serial port.
|
Chris@16
|
472 *
|
Chris@16
|
473 * @returns The number of bytes written.
|
Chris@16
|
474 *
|
Chris@16
|
475 * @throws boost::system::system_error Thrown on failure. An error code of
|
Chris@16
|
476 * boost::asio::error::eof indicates that the connection was closed by the
|
Chris@16
|
477 * peer.
|
Chris@16
|
478 *
|
Chris@16
|
479 * @note The write_some operation may not transmit all of the data to the
|
Chris@16
|
480 * peer. Consider using the @ref write function if you need to ensure that
|
Chris@16
|
481 * all data is written before the blocking operation completes.
|
Chris@16
|
482 *
|
Chris@16
|
483 * @par Example
|
Chris@16
|
484 * To write a single data buffer use the @ref buffer function as follows:
|
Chris@16
|
485 * @code
|
Chris@16
|
486 * serial_port.write_some(boost::asio::buffer(data, size));
|
Chris@16
|
487 * @endcode
|
Chris@16
|
488 * See the @ref buffer documentation for information on writing multiple
|
Chris@16
|
489 * buffers in one go, and how to use it with arrays, boost::array or
|
Chris@16
|
490 * std::vector.
|
Chris@16
|
491 */
|
Chris@16
|
492 template <typename ConstBufferSequence>
|
Chris@16
|
493 std::size_t write_some(const ConstBufferSequence& buffers)
|
Chris@16
|
494 {
|
Chris@16
|
495 boost::system::error_code ec;
|
Chris@16
|
496 std::size_t s = this->get_service().write_some(
|
Chris@16
|
497 this->get_implementation(), buffers, ec);
|
Chris@16
|
498 boost::asio::detail::throw_error(ec, "write_some");
|
Chris@16
|
499 return s;
|
Chris@16
|
500 }
|
Chris@16
|
501
|
Chris@16
|
502 /// Write some data to the serial port.
|
Chris@16
|
503 /**
|
Chris@16
|
504 * This function is used to write data to the serial port. The function call
|
Chris@16
|
505 * will block until one or more bytes of the data has been written
|
Chris@16
|
506 * successfully, or until an error occurs.
|
Chris@16
|
507 *
|
Chris@16
|
508 * @param buffers One or more data buffers to be written to the serial port.
|
Chris@16
|
509 *
|
Chris@16
|
510 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
511 *
|
Chris@16
|
512 * @returns The number of bytes written. Returns 0 if an error occurred.
|
Chris@16
|
513 *
|
Chris@16
|
514 * @note The write_some operation may not transmit all of the data to the
|
Chris@16
|
515 * peer. Consider using the @ref write function if you need to ensure that
|
Chris@16
|
516 * all data is written before the blocking operation completes.
|
Chris@16
|
517 */
|
Chris@16
|
518 template <typename ConstBufferSequence>
|
Chris@16
|
519 std::size_t write_some(const ConstBufferSequence& buffers,
|
Chris@16
|
520 boost::system::error_code& ec)
|
Chris@16
|
521 {
|
Chris@16
|
522 return this->get_service().write_some(
|
Chris@16
|
523 this->get_implementation(), buffers, ec);
|
Chris@16
|
524 }
|
Chris@16
|
525
|
Chris@16
|
526 /// Start an asynchronous write.
|
Chris@16
|
527 /**
|
Chris@16
|
528 * This function is used to asynchronously write data to the serial port.
|
Chris@16
|
529 * The function call always returns immediately.
|
Chris@16
|
530 *
|
Chris@16
|
531 * @param buffers One or more data buffers to be written to the serial port.
|
Chris@16
|
532 * Although the buffers object may be copied as necessary, ownership of the
|
Chris@16
|
533 * underlying memory blocks is retained by the caller, which must guarantee
|
Chris@16
|
534 * that they remain valid until the handler is called.
|
Chris@16
|
535 *
|
Chris@16
|
536 * @param handler The handler to be called when the write operation completes.
|
Chris@16
|
537 * Copies will be made of the handler as required. The function signature of
|
Chris@16
|
538 * the handler must be:
|
Chris@16
|
539 * @code void handler(
|
Chris@16
|
540 * const boost::system::error_code& error, // Result of operation.
|
Chris@16
|
541 * std::size_t bytes_transferred // Number of bytes written.
|
Chris@16
|
542 * ); @endcode
|
Chris@16
|
543 * Regardless of whether the asynchronous operation completes immediately or
|
Chris@16
|
544 * not, the handler will not be invoked from within this function. Invocation
|
Chris@16
|
545 * of the handler will be performed in a manner equivalent to using
|
Chris@16
|
546 * boost::asio::io_service::post().
|
Chris@16
|
547 *
|
Chris@16
|
548 * @note The write operation may not transmit all of the data to the peer.
|
Chris@16
|
549 * Consider using the @ref async_write function if you need to ensure that all
|
Chris@16
|
550 * data is written before the asynchronous operation completes.
|
Chris@16
|
551 *
|
Chris@16
|
552 * @par Example
|
Chris@16
|
553 * To write a single data buffer use the @ref buffer function as follows:
|
Chris@16
|
554 * @code
|
Chris@16
|
555 * serial_port.async_write_some(boost::asio::buffer(data, size), handler);
|
Chris@16
|
556 * @endcode
|
Chris@16
|
557 * See the @ref buffer documentation for information on writing multiple
|
Chris@16
|
558 * buffers in one go, and how to use it with arrays, boost::array or
|
Chris@16
|
559 * std::vector.
|
Chris@16
|
560 */
|
Chris@16
|
561 template <typename ConstBufferSequence, typename WriteHandler>
|
Chris@16
|
562 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
Chris@16
|
563 void (boost::system::error_code, std::size_t))
|
Chris@16
|
564 async_write_some(const ConstBufferSequence& buffers,
|
Chris@16
|
565 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
|
Chris@16
|
566 {
|
Chris@16
|
567 // If you get an error on the following line it means that your handler does
|
Chris@16
|
568 // not meet the documented type requirements for a WriteHandler.
|
Chris@16
|
569 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
|
Chris@16
|
570
|
Chris@16
|
571 return this->get_service().async_write_some(this->get_implementation(),
|
Chris@16
|
572 buffers, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
|
Chris@16
|
573 }
|
Chris@16
|
574
|
Chris@16
|
575 /// Read some data from the serial port.
|
Chris@16
|
576 /**
|
Chris@16
|
577 * This function is used to read data from the serial port. The function
|
Chris@16
|
578 * call will block until one or more bytes of data has been read successfully,
|
Chris@16
|
579 * or until an error occurs.
|
Chris@16
|
580 *
|
Chris@16
|
581 * @param buffers One or more buffers into which the data will be read.
|
Chris@16
|
582 *
|
Chris@16
|
583 * @returns The number of bytes read.
|
Chris@16
|
584 *
|
Chris@16
|
585 * @throws boost::system::system_error Thrown on failure. An error code of
|
Chris@16
|
586 * boost::asio::error::eof indicates that the connection was closed by the
|
Chris@16
|
587 * peer.
|
Chris@16
|
588 *
|
Chris@16
|
589 * @note The read_some operation may not read all of the requested number of
|
Chris@16
|
590 * bytes. Consider using the @ref read function if you need to ensure that
|
Chris@16
|
591 * the requested amount of data is read before the blocking operation
|
Chris@16
|
592 * completes.
|
Chris@16
|
593 *
|
Chris@16
|
594 * @par Example
|
Chris@16
|
595 * To read into a single data buffer use the @ref buffer function as follows:
|
Chris@16
|
596 * @code
|
Chris@16
|
597 * serial_port.read_some(boost::asio::buffer(data, size));
|
Chris@16
|
598 * @endcode
|
Chris@16
|
599 * See the @ref buffer documentation for information on reading into multiple
|
Chris@16
|
600 * buffers in one go, and how to use it with arrays, boost::array or
|
Chris@16
|
601 * std::vector.
|
Chris@16
|
602 */
|
Chris@16
|
603 template <typename MutableBufferSequence>
|
Chris@16
|
604 std::size_t read_some(const MutableBufferSequence& buffers)
|
Chris@16
|
605 {
|
Chris@16
|
606 boost::system::error_code ec;
|
Chris@16
|
607 std::size_t s = this->get_service().read_some(
|
Chris@16
|
608 this->get_implementation(), buffers, ec);
|
Chris@16
|
609 boost::asio::detail::throw_error(ec, "read_some");
|
Chris@16
|
610 return s;
|
Chris@16
|
611 }
|
Chris@16
|
612
|
Chris@16
|
613 /// Read some data from the serial port.
|
Chris@16
|
614 /**
|
Chris@16
|
615 * This function is used to read data from the serial port. The function
|
Chris@16
|
616 * call will block until one or more bytes of data has been read successfully,
|
Chris@16
|
617 * or until an error occurs.
|
Chris@16
|
618 *
|
Chris@16
|
619 * @param buffers One or more buffers into which the data will be read.
|
Chris@16
|
620 *
|
Chris@16
|
621 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
622 *
|
Chris@16
|
623 * @returns The number of bytes read. Returns 0 if an error occurred.
|
Chris@16
|
624 *
|
Chris@16
|
625 * @note The read_some operation may not read all of the requested number of
|
Chris@16
|
626 * bytes. Consider using the @ref read function if you need to ensure that
|
Chris@16
|
627 * the requested amount of data is read before the blocking operation
|
Chris@16
|
628 * completes.
|
Chris@16
|
629 */
|
Chris@16
|
630 template <typename MutableBufferSequence>
|
Chris@16
|
631 std::size_t read_some(const MutableBufferSequence& buffers,
|
Chris@16
|
632 boost::system::error_code& ec)
|
Chris@16
|
633 {
|
Chris@16
|
634 return this->get_service().read_some(
|
Chris@16
|
635 this->get_implementation(), buffers, ec);
|
Chris@16
|
636 }
|
Chris@16
|
637
|
Chris@16
|
638 /// Start an asynchronous read.
|
Chris@16
|
639 /**
|
Chris@16
|
640 * This function is used to asynchronously read data from the serial port.
|
Chris@16
|
641 * The function call always returns immediately.
|
Chris@16
|
642 *
|
Chris@16
|
643 * @param buffers One or more buffers into which the data will be read.
|
Chris@16
|
644 * Although the buffers object may be copied as necessary, ownership of the
|
Chris@16
|
645 * underlying memory blocks is retained by the caller, which must guarantee
|
Chris@16
|
646 * that they remain valid until the handler is called.
|
Chris@16
|
647 *
|
Chris@16
|
648 * @param handler The handler to be called when the read operation completes.
|
Chris@16
|
649 * Copies will be made of the handler as required. The function signature of
|
Chris@16
|
650 * the handler must be:
|
Chris@16
|
651 * @code void handler(
|
Chris@16
|
652 * const boost::system::error_code& error, // Result of operation.
|
Chris@16
|
653 * std::size_t bytes_transferred // Number of bytes read.
|
Chris@16
|
654 * ); @endcode
|
Chris@16
|
655 * Regardless of whether the asynchronous operation completes immediately or
|
Chris@16
|
656 * not, the handler will not be invoked from within this function. Invocation
|
Chris@16
|
657 * of the handler will be performed in a manner equivalent to using
|
Chris@16
|
658 * boost::asio::io_service::post().
|
Chris@16
|
659 *
|
Chris@16
|
660 * @note The read operation may not read all of the requested number of bytes.
|
Chris@16
|
661 * Consider using the @ref async_read function if you need to ensure that the
|
Chris@16
|
662 * requested amount of data is read before the asynchronous operation
|
Chris@16
|
663 * completes.
|
Chris@16
|
664 *
|
Chris@16
|
665 * @par Example
|
Chris@16
|
666 * To read into a single data buffer use the @ref buffer function as follows:
|
Chris@16
|
667 * @code
|
Chris@16
|
668 * serial_port.async_read_some(boost::asio::buffer(data, size), handler);
|
Chris@16
|
669 * @endcode
|
Chris@16
|
670 * See the @ref buffer documentation for information on reading into multiple
|
Chris@16
|
671 * buffers in one go, and how to use it with arrays, boost::array or
|
Chris@16
|
672 * std::vector.
|
Chris@16
|
673 */
|
Chris@16
|
674 template <typename MutableBufferSequence, typename ReadHandler>
|
Chris@16
|
675 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
|
Chris@16
|
676 void (boost::system::error_code, std::size_t))
|
Chris@16
|
677 async_read_some(const MutableBufferSequence& buffers,
|
Chris@16
|
678 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
|
Chris@16
|
679 {
|
Chris@16
|
680 // If you get an error on the following line it means that your handler does
|
Chris@16
|
681 // not meet the documented type requirements for a ReadHandler.
|
Chris@16
|
682 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
|
Chris@16
|
683
|
Chris@16
|
684 return this->get_service().async_read_some(this->get_implementation(),
|
Chris@16
|
685 buffers, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
|
Chris@16
|
686 }
|
Chris@16
|
687 };
|
Chris@16
|
688
|
Chris@16
|
689 } // namespace asio
|
Chris@16
|
690 } // namespace boost
|
Chris@16
|
691
|
Chris@16
|
692 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
693
|
Chris@16
|
694 #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT)
|
Chris@16
|
695 // || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
696
|
Chris@16
|
697 #endif // BOOST_ASIO_BASIC_SERIAL_PORT_HPP
|