serial_port_base.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // serial_port_base.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 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)
  10. //
  11. #ifndef ASIO_SERIAL_PORT_BASE_HPP
  12. #define ASIO_SERIAL_PORT_BASE_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include "asio/detail/push_options.hpp"
  17. #include "asio/detail/push_options.hpp"
  18. #include <stdexcept>
  19. #include <boost/config.hpp>
  20. #include <boost/detail/workaround.hpp>
  21. #include "asio/detail/pop_options.hpp"
  22. #if !defined(ASIO_DISABLE_SERIAL_PORT)
  23. # if defined(ASIO_HAS_IOCP) \
  24. || !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
  25. # define ASIO_HAS_SERIAL_PORT 1
  26. # endif // defined(ASIO_HAS_IOCP)
  27. #endif // !defined(ASIO_DISABLE_STREAM_HANDLE)
  28. #if defined(ASIO_HAS_SERIAL_PORT) \
  29. || defined(GENERATING_DOCUMENTATION)
  30. #if !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
  31. # include "asio/detail/push_options.hpp"
  32. # include <termios.h>
  33. # include "asio/detail/pop_options.hpp"
  34. #endif // !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
  35. #include "asio/error_code.hpp"
  36. #include "asio/detail/socket_types.hpp"
  37. #if defined(GENERATING_DOCUMENTATION)
  38. # define ASIO_OPTION_STORAGE implementation_defined
  39. #elif defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  40. # define ASIO_OPTION_STORAGE DCB
  41. #else
  42. # define ASIO_OPTION_STORAGE termios
  43. #endif
  44. namespace asio {
  45. /// The serial_port_base class is used as a base for the basic_serial_port class
  46. /// template so that we have a common place to define the serial port options.
  47. class serial_port_base
  48. {
  49. public:
  50. /// Serial port option to permit changing the baud rate.
  51. /**
  52. * Implements changing the baud rate for a given serial port.
  53. */
  54. class baud_rate
  55. {
  56. public:
  57. explicit baud_rate(unsigned int rate = 0);
  58. unsigned int value() const;
  59. asio::error_code store(ASIO_OPTION_STORAGE& storage,
  60. asio::error_code& ec) const;
  61. asio::error_code load(const ASIO_OPTION_STORAGE& storage,
  62. asio::error_code& ec);
  63. private:
  64. unsigned int value_;
  65. };
  66. /// Serial port option to permit changing the flow control.
  67. /**
  68. * Implements changing the flow control for a given serial port.
  69. */
  70. class flow_control
  71. {
  72. public:
  73. enum type { none, software, hardware };
  74. explicit flow_control(type t = none);
  75. type value() const;
  76. asio::error_code store(ASIO_OPTION_STORAGE& storage,
  77. asio::error_code& ec) const;
  78. asio::error_code load(const ASIO_OPTION_STORAGE& storage,
  79. asio::error_code& ec);
  80. private:
  81. type value_;
  82. };
  83. /// Serial port option to permit changing the parity.
  84. /**
  85. * Implements changing the parity for a given serial port.
  86. */
  87. class parity
  88. {
  89. public:
  90. enum type { none, odd, even };
  91. explicit parity(type t = none);
  92. type value() const;
  93. asio::error_code store(ASIO_OPTION_STORAGE& storage,
  94. asio::error_code& ec) const;
  95. asio::error_code load(const ASIO_OPTION_STORAGE& storage,
  96. asio::error_code& ec);
  97. private:
  98. type value_;
  99. };
  100. /// Serial port option to permit changing the number of stop bits.
  101. /**
  102. * Implements changing the number of stop bits for a given serial port.
  103. */
  104. class stop_bits
  105. {
  106. public:
  107. enum type { one, onepointfive, two };
  108. explicit stop_bits(type t = one);
  109. type value() const;
  110. asio::error_code store(ASIO_OPTION_STORAGE& storage,
  111. asio::error_code& ec) const;
  112. asio::error_code load(const ASIO_OPTION_STORAGE& storage,
  113. asio::error_code& ec);
  114. private:
  115. type value_;
  116. };
  117. /// Serial port option to permit changing the character size.
  118. /**
  119. * Implements changing the character size for a given serial port.
  120. */
  121. class character_size
  122. {
  123. public:
  124. explicit character_size(unsigned int t = 8);
  125. unsigned int value() const;
  126. asio::error_code store(ASIO_OPTION_STORAGE& storage,
  127. asio::error_code& ec) const;
  128. asio::error_code load(const ASIO_OPTION_STORAGE& storage,
  129. asio::error_code& ec);
  130. private:
  131. unsigned int value_;
  132. };
  133. protected:
  134. /// Protected destructor to prevent deletion through this type.
  135. ~serial_port_base()
  136. {
  137. }
  138. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  139. private:
  140. // Workaround to enable the empty base optimisation with Borland C++.
  141. char dummy_;
  142. #endif
  143. };
  144. } // namespace asio
  145. #include "asio/impl/serial_port_base.ipp"
  146. #undef ASIO_OPTION_STORAGE
  147. #endif // defined(ASIO_HAS_SERIAL_PORT)
  148. // || defined(GENERATING_DOCUMENTATION)
  149. #include "asio/detail/pop_options.hpp"
  150. #endif // ASIO_SERIAL_PORT_BASE_HPP