2 // detail/impl/win_iocp_serial_port_service.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 // Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com)
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)
12 #ifndef BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_SERIAL_PORT_SERVICE_IPP
13 #define BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_SERIAL_PORT_SERVICE_IPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
19 #include <boost/asio/detail/config.hpp>
21 #if defined(BOOST_ASIO_HAS_IOCP) && defined(BOOST_ASIO_HAS_SERIAL_PORT)
24 #include <boost/asio/detail/win_iocp_serial_port_service.hpp>
26 #include <boost/asio/detail/push_options.hpp>
32 win_iocp_serial_port_service::win_iocp_serial_port_service(
33 execution_context& context)
34 : execution_context_service_base<win_iocp_serial_port_service>(context),
35 handle_service_(context)
39 void win_iocp_serial_port_service::shutdown()
43 boost::system::error_code win_iocp_serial_port_service::open(
44 win_iocp_serial_port_service::implementation_type& impl,
45 const std::string& device, boost::system::error_code& ec)
49 ec = boost::asio::error::already_open;
53 // For convenience, add a leading \\.\ sequence if not already present.
54 std::string name = (device[0] == '\\') ? device : "\\\\.\\" + device;
56 // Open a handle to the serial port.
57 ::HANDLE handle = ::CreateFileA(name.c_str(),
58 GENERIC_READ | GENERIC_WRITE, 0, 0,
59 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
60 if (handle == INVALID_HANDLE_VALUE)
62 DWORD last_error = ::GetLastError();
63 ec = boost::system::error_code(last_error,
64 boost::asio::error::get_system_category());
68 // Determine the initial serial port parameters.
69 using namespace std; // For memset.
71 memset(&dcb, 0, sizeof(DCB));
72 dcb.DCBlength = sizeof(DCB);
73 if (!::GetCommState(handle, &dcb))
75 DWORD last_error = ::GetLastError();
76 ::CloseHandle(handle);
77 ec = boost::system::error_code(last_error,
78 boost::asio::error::get_system_category());
82 // Set some default serial port parameters. This implementation does not
83 // support changing all of these, so they might as well be in a known state.
84 dcb.fBinary = TRUE; // Win32 only supports binary mode.
85 dcb.fNull = FALSE; // Do not ignore NULL characters.
86 dcb.fAbortOnError = FALSE; // Ignore serial framing errors.
87 dcb.BaudRate = CBR_9600; // 9600 baud by default
88 dcb.ByteSize = 8; // 8 bit bytes
89 dcb.fOutxCtsFlow = FALSE; // No flow control
90 dcb.fOutxDsrFlow = FALSE;
91 dcb.fDtrControl = DTR_CONTROL_DISABLE;
92 dcb.fDsrSensitivity = FALSE;
95 dcb.fRtsControl = RTS_CONTROL_DISABLE;
96 dcb.fParity = FALSE; // No parity
97 dcb.Parity = NOPARITY;
98 dcb.StopBits = ONESTOPBIT; // One stop bit
99 if (!::SetCommState(handle, &dcb))
101 DWORD last_error = ::GetLastError();
102 ::CloseHandle(handle);
103 ec = boost::system::error_code(last_error,
104 boost::asio::error::get_system_category());
108 // Set up timeouts so that the serial port will behave similarly to a
109 // network socket. Reads wait for at least one byte, then return with
110 // whatever they have. Writes return once everything is out the door.
111 ::COMMTIMEOUTS timeouts;
112 timeouts.ReadIntervalTimeout = 1;
113 timeouts.ReadTotalTimeoutMultiplier = 0;
114 timeouts.ReadTotalTimeoutConstant = 0;
115 timeouts.WriteTotalTimeoutMultiplier = 0;
116 timeouts.WriteTotalTimeoutConstant = 0;
117 if (!::SetCommTimeouts(handle, &timeouts))
119 DWORD last_error = ::GetLastError();
120 ::CloseHandle(handle);
121 ec = boost::system::error_code(last_error,
122 boost::asio::error::get_system_category());
126 // We're done. Take ownership of the serial port handle.
127 if (handle_service_.assign(impl, handle, ec))
128 ::CloseHandle(handle);
132 boost::system::error_code win_iocp_serial_port_service::do_set_option(
133 win_iocp_serial_port_service::implementation_type& impl,
134 win_iocp_serial_port_service::store_function_type store,
135 const void* option, boost::system::error_code& ec)
137 using namespace std; // For memcpy.
140 memset(&dcb, 0, sizeof(DCB));
141 dcb.DCBlength = sizeof(DCB);
142 if (!::GetCommState(handle_service_.native_handle(impl), &dcb))
144 DWORD last_error = ::GetLastError();
145 ec = boost::system::error_code(last_error,
146 boost::asio::error::get_system_category());
150 if (store(option, dcb, ec))
153 if (!::SetCommState(handle_service_.native_handle(impl), &dcb))
155 DWORD last_error = ::GetLastError();
156 ec = boost::system::error_code(last_error,
157 boost::asio::error::get_system_category());
161 ec = boost::system::error_code();
165 boost::system::error_code win_iocp_serial_port_service::do_get_option(
166 const win_iocp_serial_port_service::implementation_type& impl,
167 win_iocp_serial_port_service::load_function_type load,
168 void* option, boost::system::error_code& ec) const
170 using namespace std; // For memset.
173 memset(&dcb, 0, sizeof(DCB));
174 dcb.DCBlength = sizeof(DCB);
175 if (!::GetCommState(handle_service_.native_handle(impl), &dcb))
177 DWORD last_error = ::GetLastError();
178 ec = boost::system::error_code(last_error,
179 boost::asio::error::get_system_category());
183 return load(option, dcb, ec);
186 } // namespace detail
190 #include <boost/asio/detail/pop_options.hpp>
192 #endif // defined(BOOST_ASIO_HAS_IOCP) && defined(BOOST_ASIO_HAS_SERIAL_PORT)
194 #endif // BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_SERIAL_PORT_SERVICE_IPP