win_iocp_serial_port_service.ipp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // detail/impl/win_iocp_serial_port_service.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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_DETAIL_IMPL_WIN_IOCP_SERIAL_PORT_SERVICE_IPP
  12. #define BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_SERIAL_PORT_SERVICE_IPP
  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_IOCP) && defined(BOOST_ASIO_HAS_SERIAL_PORT)
  18. #include <cstring>
  19. #include <boost/asio/detail/win_iocp_serial_port_service.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. namespace detail {
  24. win_iocp_serial_port_service::win_iocp_serial_port_service(
  25. execution_context& context)
  26. : execution_context_service_base<win_iocp_serial_port_service>(context),
  27. handle_service_(context)
  28. {
  29. }
  30. void win_iocp_serial_port_service::shutdown()
  31. {
  32. }
  33. boost::system::error_code win_iocp_serial_port_service::open(
  34. win_iocp_serial_port_service::implementation_type& impl,
  35. const std::string& device, boost::system::error_code& ec)
  36. {
  37. if (is_open(impl))
  38. {
  39. ec = boost::asio::error::already_open;
  40. return ec;
  41. }
  42. // For convenience, add a leading \\.\ sequence if not already present.
  43. std::string name = (device[0] == '\\') ? device : "\\\\.\\" + device;
  44. // Open a handle to the serial port.
  45. ::HANDLE handle = ::CreateFileA(name.c_str(),
  46. GENERIC_READ | GENERIC_WRITE, 0, 0,
  47. OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
  48. if (handle == INVALID_HANDLE_VALUE)
  49. {
  50. DWORD last_error = ::GetLastError();
  51. ec = boost::system::error_code(last_error,
  52. boost::asio::error::get_system_category());
  53. return ec;
  54. }
  55. // Determine the initial serial port parameters.
  56. using namespace std; // For memset.
  57. ::DCB dcb;
  58. memset(&dcb, 0, sizeof(DCB));
  59. dcb.DCBlength = sizeof(DCB);
  60. if (!::GetCommState(handle, &dcb))
  61. {
  62. DWORD last_error = ::GetLastError();
  63. ::CloseHandle(handle);
  64. ec = boost::system::error_code(last_error,
  65. boost::asio::error::get_system_category());
  66. return ec;
  67. }
  68. // Set some default serial port parameters. This implementation does not
  69. // support changing all of these, so they might as well be in a known state.
  70. dcb.fBinary = TRUE; // Win32 only supports binary mode.
  71. dcb.fNull = FALSE; // Do not ignore NULL characters.
  72. dcb.fAbortOnError = FALSE; // Ignore serial framing errors.
  73. dcb.BaudRate = CBR_9600; // 9600 baud by default
  74. dcb.ByteSize = 8; // 8 bit bytes
  75. dcb.fOutxCtsFlow = FALSE; // No flow control
  76. dcb.fOutxDsrFlow = FALSE;
  77. dcb.fDtrControl = DTR_CONTROL_DISABLE;
  78. dcb.fDsrSensitivity = FALSE;
  79. dcb.fOutX = FALSE;
  80. dcb.fInX = FALSE;
  81. dcb.fRtsControl = RTS_CONTROL_DISABLE;
  82. dcb.fParity = FALSE; // No parity
  83. dcb.Parity = NOPARITY;
  84. dcb.StopBits = ONESTOPBIT; // One stop bit
  85. if (!::SetCommState(handle, &dcb))
  86. {
  87. DWORD last_error = ::GetLastError();
  88. ::CloseHandle(handle);
  89. ec = boost::system::error_code(last_error,
  90. boost::asio::error::get_system_category());
  91. return ec;
  92. }
  93. // Set up timeouts so that the serial port will behave similarly to a
  94. // network socket. Reads wait for at least one byte, then return with
  95. // whatever they have. Writes return once everything is out the door.
  96. ::COMMTIMEOUTS timeouts;
  97. timeouts.ReadIntervalTimeout = 1;
  98. timeouts.ReadTotalTimeoutMultiplier = 0;
  99. timeouts.ReadTotalTimeoutConstant = 0;
  100. timeouts.WriteTotalTimeoutMultiplier = 0;
  101. timeouts.WriteTotalTimeoutConstant = 0;
  102. if (!::SetCommTimeouts(handle, &timeouts))
  103. {
  104. DWORD last_error = ::GetLastError();
  105. ::CloseHandle(handle);
  106. ec = boost::system::error_code(last_error,
  107. boost::asio::error::get_system_category());
  108. return ec;
  109. }
  110. // We're done. Take ownership of the serial port handle.
  111. if (handle_service_.assign(impl, handle, ec))
  112. ::CloseHandle(handle);
  113. return ec;
  114. }
  115. boost::system::error_code win_iocp_serial_port_service::do_set_option(
  116. win_iocp_serial_port_service::implementation_type& impl,
  117. win_iocp_serial_port_service::store_function_type store,
  118. const void* option, boost::system::error_code& ec)
  119. {
  120. using namespace std; // For memcpy.
  121. ::DCB dcb;
  122. memset(&dcb, 0, sizeof(DCB));
  123. dcb.DCBlength = sizeof(DCB);
  124. if (!::GetCommState(handle_service_.native_handle(impl), &dcb))
  125. {
  126. DWORD last_error = ::GetLastError();
  127. ec = boost::system::error_code(last_error,
  128. boost::asio::error::get_system_category());
  129. return ec;
  130. }
  131. if (store(option, dcb, ec))
  132. return ec;
  133. if (!::SetCommState(handle_service_.native_handle(impl), &dcb))
  134. {
  135. DWORD last_error = ::GetLastError();
  136. ec = boost::system::error_code(last_error,
  137. boost::asio::error::get_system_category());
  138. return ec;
  139. }
  140. ec = boost::system::error_code();
  141. return ec;
  142. }
  143. boost::system::error_code win_iocp_serial_port_service::do_get_option(
  144. const win_iocp_serial_port_service::implementation_type& impl,
  145. win_iocp_serial_port_service::load_function_type load,
  146. void* option, boost::system::error_code& ec) const
  147. {
  148. using namespace std; // For memset.
  149. ::DCB dcb;
  150. memset(&dcb, 0, sizeof(DCB));
  151. dcb.DCBlength = sizeof(DCB);
  152. if (!::GetCommState(handle_service_.native_handle(impl), &dcb))
  153. {
  154. DWORD last_error = ::GetLastError();
  155. ec = boost::system::error_code(last_error,
  156. boost::asio::error::get_system_category());
  157. return ec;
  158. }
  159. return load(option, dcb, ec);
  160. }
  161. } // namespace detail
  162. } // namespace asio
  163. } // namespace boost
  164. #include <boost/asio/detail/pop_options.hpp>
  165. #endif // defined(BOOST_ASIO_HAS_IOCP) && defined(BOOST_ASIO_HAS_SERIAL_PORT)
  166. #endif // BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_SERIAL_PORT_SERVICE_IPP