Subject: [Boost-bugs] [Boost C++ Libraries] #11721: boost::asio::serial_port bug in win10
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2015-10-13 07:57:36
#11721: boost::asio::serial_port bug in win10
--------------------------+----------------------------
Reporter: admin@⦠| Owner: chris_kohlhoff
Type: Bugs | Status: new
Milestone: Boost 1.59.0 | Component: asio
Version: Boost 1.59.0 | Severity: Problem
Keywords: serial_port |
--------------------------+----------------------------
å¨æè¿åä¸äºä¸²å£æä½çç¨åºæ¶ä½¿ç¨äº boost::asio::serial_port æ¥æä½ä¸²å£
ä½å½å°è¯æå¼ä¸²å£çæ¶ååºç°äºé误ãä¸é¢æ¯æçæµè¯ä»£ç ï¼
{{{#!cpp
boost::asio::io_service _io_service;
const std::string devname = "COM3";
try
{
boost::asio::serial_port serial(_io_service);
serial.open(devname);// throw error every times.
if (serial.is_open()) {
std::cout << devname << " serial open successed." <<
std::endl;
}
else {
std::cout << devname << " serial open failed!" <<
std::endl;
}
}
catch (const std::exception& ex)
{
std::cout << ex.what() << std::endl;// GetLastError() == 87
}
}}}
æ¯æ¬¡é½ä¼åºç°é误87ãå³ GetLastError() çç»æ为 87
äºæ¯è·è¿ä»£ç éé¢è°è¯è¿½å°äº win_iocp_serial_port_service::open å½æ°é
win_iocp_serial_port_service::open å½æ°çå®ç°å¦ä¸ï¼
{{{#!cpp
boost::system::error_code win_iocp_serial_port_service::open(
win_iocp_serial_port_service::implementation_type& impl,
const std::string& device, boost::system::error_code& ec)
{
if (is_open(impl))
{
ec = boost::asio::error::already_open;
return ec;
}
std::string name = (device[0] == '\\') ? device : "\\\\.\\" +
device;
::HANDLE handle = ::CreateFileA(name.c_str(),
GENERIC_READ | GENERIC_WRITE, 0, 0,
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
if (handle == INVALID_HANDLE_VALUE)
{
DWORD last_error = ::GetLastError();
ec = boost::system::error_code(last_error,
boost::asio::error::get_system_category());
return ec;
}
using namespace std;
::DCB dcb;
memset(&dcb, 0, sizeof(DCB));
dcb.DCBlength = sizeof(DCB);
if (!::GetCommState(handle, &dcb))
{
DWORD last_error = ::GetLastError();
::CloseHandle(handle);
ec = boost::system::error_code(last_error,
boost::asio::error::get_system_category());
return ec;
}
dcb.fBinary = TRUE;
dcb.fDsrSensitivity = FALSE;
dcb.fNull = FALSE;
dcb.fAbortOnError = FALSE;
if (!::SetCommState(handle, &dcb))
{
DWORD last_error = ::GetLastError();// lee: error is
here!!!
::CloseHandle(handle);
ec = boost::system::error_code(last_error,
boost::asio::error::get_system_category());
return ec;
}
::COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = 1;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.ReadTotalTimeoutConstant = 0;
timeouts.WriteTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 0;
if (!::SetCommTimeouts(handle, &timeouts))
{
DWORD last_error = ::GetLastError();
::CloseHandle(handle);
ec = boost::system::error_code(last_error,
boost::asio::error::get_system_category());
return ec;
}
if (handle_service_.assign(impl, handle, ec))
::CloseHandle(handle);
return ec;
}
}}}
åç°æ¯æ¬¡å¨ç¬¬ä¸æ¬¡ SetCommState çæ¶åæ»æ¯ä¼æ¥é并离å¼ã
äºæ¯æ¥çäºåé¢éè¿ GetCommState è·åå°ç dcb çå¼ã
åç° dcb.BaudRate == 0 æ¶æ æ³ SetCommState æå
ä¹å°±æ¯è¯´å¨æäºè®¾å¤ä¸è·åä¸å° dcb.BaudRate è¿ä¸ªå¼ã
== ä¸é¢æ¯æèªå·±ç解å³åæ³ï¼ ==
å¨ win_iocp_serial_port_service.ipp æ件ç88è¡å·¦å³æ·»å
{{{#!cpp
if (dcb.BaudRate == 0) dcb.BaudRate = 115200;
}}}
ä¿®æ¹åç win_iocp_serial_port_service::open å½æ°å®æ´ä»£ç å¦ä¸ï¼
{{{#!cpp
boost::system::error_code win_iocp_serial_port_service::open(
win_iocp_serial_port_service::implementation_type& impl,
const std::string& device, boost::system::error_code& ec)
{
if (is_open(impl))
{
ec = boost::asio::error::already_open;
return ec;
}
std::string name = (device[0] == '\\') ? device : "\\\\.\\" +
device;
::HANDLE handle = ::CreateFileA(name.c_str(),
GENERIC_READ | GENERIC_WRITE, 0, 0,
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
if (handle == INVALID_HANDLE_VALUE)
{
DWORD last_error = ::GetLastError();
ec = boost::system::error_code(last_error,
boost::asio::error::get_system_category());
return ec;
}
using namespace std;
::DCB dcb;
memset(&dcb, 0, sizeof(DCB));
dcb.DCBlength = sizeof(DCB);
if (!::GetCommState(handle, &dcb))
{
DWORD last_error = ::GetLastError();
::CloseHandle(handle);
ec = boost::system::error_code(last_error,
boost::asio::error::get_system_category());
return ec;
}
dcb.fBinary = TRUE;
dcb.fDsrSensitivity = FALSE;
dcb.fNull = FALSE;
dcb.fAbortOnError = FALSE;
if (dcb.BaudRate == 0) dcb.BaudRate = 115200; // add lee
2015.10.10. 解å³dcb.BaudRate为0æ¶æ æ³æåSetCommStateçBUG
if (!::SetCommState(handle, &dcb))
{
DWORD last_error = ::GetLastError();
::CloseHandle(handle);
ec = boost::system::error_code(last_error,
boost::asio::error::get_system_category());
return ec;
}
::COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = 1;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.ReadTotalTimeoutConstant = 0;
timeouts.WriteTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 0;
if (!::SetCommTimeouts(handle, &timeouts))
{
DWORD last_error = ::GetLastError();
::CloseHandle(handle);
ec = boost::system::error_code(last_error,
boost::asio::error::get_system_category());
return ec;
}
if (handle_service_.assign(impl, handle, ec))
::CloseHandle(handle);
return ec;
}
}}}
== æµè¯ç¯å¢ ==
||= 说æ =||= åæ° =||
|| æä½ç³»ç» || Windows10 ä¸ä¸ç x64 ||
|| å¼åç¯å¢ || Microsoft Visual Studio Community 2013 Version 12.0.40629.00
Update 5 ||
|| Boostçæ¬ || boost_1.59.0 ||
== ç»æè¯ ==
以ä¸åªæ¯èªå·±ççæµï¼å¹¶ä¸ä¸å®æ¯å®å
¨æ£ç¡®çãå¦æä»»ä½é误ï¼è¯·è系并åè¯æãæå°å°½å¿«ä¿®æ¹ï¼ä¸èææ¿ã
== æçåæ ==
http://www.leelib.com/2015/10/10/win10-boost-asio-serial-port-bug.html
-- Ticket URL: <https://svn.boost.org/trac/boost/ticket/11721> Boost C++ Libraries <http://www.boost.org/> Boost provides free peer-reviewed portable C++ source libraries.
This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:19 UTC