serial_port_base.hpp 5.1 KB

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