From 985c3a51725b11d00d65ea2abef05c3fb7cb3ab8 Mon Sep 17 00:00:00 2001 From: William Woodall Date: Tue, 21 Apr 2015 20:18:22 -0700 Subject: [PATCH] fix handling of COM ports over 10 on Windows, fixes #84 --- src/impl/win.cc | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/impl/win.cc b/src/impl/win.cc index 545a773..7b1f1a6 100644 --- a/src/impl/win.cc +++ b/src/impl/win.cc @@ -20,6 +20,15 @@ using serial::SerialException; using serial::PortNotOpenedException; using serial::IOException; +inline wstring +_prefix_port_if_needed(const wstring &input) +{ + static wstring windows_com_port_prefix = L"\\\\.\\"; + if (input.compare(windows_com_port_prefix) != 0) + { + return windows_com_port_prefix + input; + } +} Serial::SerialImpl::SerialImpl (const string &port, unsigned long baudrate, bytesize_t bytesize, @@ -52,7 +61,9 @@ Serial::SerialImpl::open () throw SerialException ("Serial port already open."); } - LPCWSTR lp_port = port_.c_str(); + // See: https://github.com/wjwwood/serial/issues/84 + wstring port_with_prefix = _prefix_port_if_needed(port_); + LPCWSTR lp_port = port_with_prefix.c_str(); fd_ = CreateFileW(lp_port, GENERIC_READ | GENERIC_WRITE, 0,