Skip to content

Commit

Permalink
Windows: Fallback to checking registry if port is not CHAR/UNKNOWN
Browse files Browse the repository at this point in the history
Some devices, e.g. a uBlox is incorrectly registered in Windows as a file device. This prevents the port from being opened. In this case, check as a last resort that the port exists in the registry. If so, then open it anyway.

Issue: jcurl#64, DOTNET-169
  • Loading branch information
jcurl committed Oct 14, 2018
1 parent 1310a5d commit 8fb8f09
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions code/Native/WinNativeSerial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -843,10 +843,27 @@ public void Open()
if (m_ComPortHandle.IsInvalid) WinIOError();

NativeMethods.FileType t = UnsafeNativeMethods.GetFileType(m_ComPortHandle);
bool validOverride = false;
if (t != NativeMethods.FileType.FILE_TYPE_CHAR && t != NativeMethods.FileType.FILE_TYPE_UNKNOWN) {
m_ComPortHandle.Dispose();
m_ComPortHandle = null;
throw new IOException("Wrong file type: " + PortName);
foreach (string port in GetPortNames()) {
#if NETSTANDARD15
if (port.Equals(PortName, StringComparison.OrdinalIgnoreCase)) {
validOverride = true;
break;
}
#else
if (port.Equals(PortName, StringComparison.InvariantCultureIgnoreCase)) {
validOverride = true;
break;
}
#endif
}

if (!validOverride) {
m_ComPortHandle.Dispose();
m_ComPortHandle = null;
throw new IOException("Wrong file type: " + PortName);
}
}

// Set the default parameters
Expand Down

0 comments on commit 8fb8f09

Please sign in to comment.