1// 2// detail/impl/win_iocp_serial_port_service.ipp 3// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4// 5// Copyright (c) 2003-2021 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 12#ifndef BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_SERIAL_PORT_SERVICE_IPP 13#define BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_SERIAL_PORT_SERVICE_IPP 14 15#if defined(_MSC_VER) && (_MSC_VER >= 1200) 16# pragma once 17#endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 18 19#include <boost/asio/detail/config.hpp> 20 21#if defined(BOOST_ASIO_HAS_IOCP) && defined(BOOST_ASIO_HAS_SERIAL_PORT) 22 23#include <cstring> 24#include <boost/asio/detail/win_iocp_serial_port_service.hpp> 25 26#include <boost/asio/detail/push_options.hpp> 27 28namespace boost { 29namespace asio { 30namespace detail { 31 32win_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) 36{ 37} 38 39void win_iocp_serial_port_service::shutdown() 40{ 41} 42 43boost::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) 46{ 47 if (is_open(impl)) 48 { 49 ec = boost::asio::error::already_open; 50 return ec; 51 } 52 53 // For convenience, add a leading \\.\ sequence if not already present. 54 std::string name = (device[0] == '\\') ? device : "\\\\.\\" + device; 55 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) 61 { 62 DWORD last_error = ::GetLastError(); 63 ec = boost::system::error_code(last_error, 64 boost::asio::error::get_system_category()); 65 return ec; 66 } 67 68 // Determine the initial serial port parameters. 69 using namespace std; // For memset. 70 ::DCB dcb; 71 memset(&dcb, 0, sizeof(DCB)); 72 dcb.DCBlength = sizeof(DCB); 73 if (!::GetCommState(handle, &dcb)) 74 { 75 DWORD last_error = ::GetLastError(); 76 ::CloseHandle(handle); 77 ec = boost::system::error_code(last_error, 78 boost::asio::error::get_system_category()); 79 return ec; 80 } 81 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; 93 dcb.fOutX = FALSE; 94 dcb.fInX = 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)) 100 { 101 DWORD last_error = ::GetLastError(); 102 ::CloseHandle(handle); 103 ec = boost::system::error_code(last_error, 104 boost::asio::error::get_system_category()); 105 return ec; 106 } 107 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)) 118 { 119 DWORD last_error = ::GetLastError(); 120 ::CloseHandle(handle); 121 ec = boost::system::error_code(last_error, 122 boost::asio::error::get_system_category()); 123 return ec; 124 } 125 126 // We're done. Take ownership of the serial port handle. 127 if (handle_service_.assign(impl, handle, ec)) 128 ::CloseHandle(handle); 129 return ec; 130} 131 132boost::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) 136{ 137 using namespace std; // For memcpy. 138 139 ::DCB dcb; 140 memset(&dcb, 0, sizeof(DCB)); 141 dcb.DCBlength = sizeof(DCB); 142 if (!::GetCommState(handle_service_.native_handle(impl), &dcb)) 143 { 144 DWORD last_error = ::GetLastError(); 145 ec = boost::system::error_code(last_error, 146 boost::asio::error::get_system_category()); 147 return ec; 148 } 149 150 if (store(option, dcb, ec)) 151 return ec; 152 153 if (!::SetCommState(handle_service_.native_handle(impl), &dcb)) 154 { 155 DWORD last_error = ::GetLastError(); 156 ec = boost::system::error_code(last_error, 157 boost::asio::error::get_system_category()); 158 return ec; 159 } 160 161 ec = boost::system::error_code(); 162 return ec; 163} 164 165boost::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 169{ 170 using namespace std; // For memset. 171 172 ::DCB dcb; 173 memset(&dcb, 0, sizeof(DCB)); 174 dcb.DCBlength = sizeof(DCB); 175 if (!::GetCommState(handle_service_.native_handle(impl), &dcb)) 176 { 177 DWORD last_error = ::GetLastError(); 178 ec = boost::system::error_code(last_error, 179 boost::asio::error::get_system_category()); 180 return ec; 181 } 182 183 return load(option, dcb, ec); 184} 185 186} // namespace detail 187} // namespace asio 188} // namespace boost 189 190#include <boost/asio/detail/pop_options.hpp> 191 192#endif // defined(BOOST_ASIO_HAS_IOCP) && defined(BOOST_ASIO_HAS_SERIAL_PORT) 193 194#endif // BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_SERIAL_PORT_SERVICE_IPP 195