win_iocp_serial_port_service.ipp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // detail/impl/win_iocp_serial_port_service.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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. boost::asio::io_context& io_context)
  26. : service_base<win_iocp_serial_port_service>(io_context),
  27. handle_service_(io_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 these, so they might as well be in a known state.
  70. dcb.fBinary = TRUE; // Win32 only supports binary mode.
  71. dcb.fDsrSensitivity = FALSE;
  72. dcb.fNull = FALSE; // Do not ignore NULL characters.
  73. dcb.fAbortOnError = FALSE; // Ignore serial framing errors.
  74. if (!::SetCommState(handle, &dcb))
  75. {
  76. DWORD last_error = ::GetLastError();
  77. ::CloseHandle(handle);
  78. ec = boost::system::error_code(last_error,
  79. boost::asio::error::get_system_category());
  80. return ec;
  81. }
  82. // Set up timeouts so that the serial port will behave similarly to a
  83. // network socket. Reads wait for at least one byte, then return with
  84. // whatever they have. Writes return once everything is out the door.
  85. ::COMMTIMEOUTS timeouts;
  86. timeouts.ReadIntervalTimeout = 1;
  87. timeouts.ReadTotalTimeoutMultiplier = 0;
  88. timeouts.ReadTotalTimeoutConstant = 0;
  89. timeouts.WriteTotalTimeoutMultiplier = 0;
  90. timeouts.WriteTotalTimeoutConstant = 0;
  91. if (!::SetCommTimeouts(handle, &timeouts))
  92. {
  93. DWORD last_error = ::GetLastError();
  94. ::CloseHandle(handle);
  95. ec = boost::system::error_code(last_error,
  96. boost::asio::error::get_system_category());
  97. return ec;
  98. }
  99. // We're done. Take ownership of the serial port handle.
  100. if (handle_service_.assign(impl, handle, ec))
  101. ::CloseHandle(handle);
  102. return ec;
  103. }
  104. boost::system::error_code win_iocp_serial_port_service::do_set_option(
  105. win_iocp_serial_port_service::implementation_type& impl,
  106. win_iocp_serial_port_service::store_function_type store,
  107. const void* option, boost::system::error_code& ec)
  108. {
  109. using namespace std; // For memcpy.
  110. ::DCB dcb;
  111. memset(&dcb, 0, sizeof(DCB));
  112. dcb.DCBlength = sizeof(DCB);
  113. if (!::GetCommState(handle_service_.native_handle(impl), &dcb))
  114. {
  115. DWORD last_error = ::GetLastError();
  116. ec = boost::system::error_code(last_error,
  117. boost::asio::error::get_system_category());
  118. return ec;
  119. }
  120. if (store(option, dcb, ec))
  121. return ec;
  122. if (!::SetCommState(handle_service_.native_handle(impl), &dcb))
  123. {
  124. DWORD last_error = ::GetLastError();
  125. ec = boost::system::error_code(last_error,
  126. boost::asio::error::get_system_category());
  127. return ec;
  128. }
  129. ec = boost::system::error_code();
  130. return ec;
  131. }
  132. boost::system::error_code win_iocp_serial_port_service::do_get_option(
  133. const win_iocp_serial_port_service::implementation_type& impl,
  134. win_iocp_serial_port_service::load_function_type load,
  135. void* option, boost::system::error_code& ec) const
  136. {
  137. using namespace std; // For memset.
  138. ::DCB dcb;
  139. memset(&dcb, 0, sizeof(DCB));
  140. dcb.DCBlength = sizeof(DCB);
  141. if (!::GetCommState(handle_service_.native_handle(impl), &dcb))
  142. {
  143. DWORD last_error = ::GetLastError();
  144. ec = boost::system::error_code(last_error,
  145. boost::asio::error::get_system_category());
  146. return ec;
  147. }
  148. return load(option, dcb, ec);
  149. }
  150. } // namespace detail
  151. } // namespace asio
  152. } // namespace boost
  153. #include <boost/asio/detail/pop_options.hpp>
  154. #endif // defined(BOOST_ASIO_HAS_IOCP) && defined(BOOST_ASIO_HAS_SERIAL_PORT)
  155. #endif // BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_SERIAL_PORT_SERVICE_IPP