serial_port_base.hpp 4.9 KB

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