-
Notifications
You must be signed in to change notification settings - Fork 5.3k
System.IO.Ports: check device capabilities for DTR/DSR in SerialStream Constructor/dispose (Windows) #122454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Benkol003
wants to merge
2
commits into
dotnet:main
Choose a base branch
from
Benkol003:System.IO.Ports/check-device-capabilities
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+40
−32
Open
System.IO.Ports: check device capabilities for DTR/DSR in SerialStream Constructor/dispose (Windows) #122454
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -634,17 +634,20 @@ internal SerialStream(string portName, int baudRate, Parity parity, int dataBits | |||||||
| // set constant properties of the DCB | ||||||||
| InitializeDCB(baudRate, parity, dataBits, stopBits, discardNull); | ||||||||
|
|
||||||||
| DtrEnable = dtrEnable; | ||||||||
|
|
||||||||
| // query and cache the initial RtsEnable value | ||||||||
| // so that set_RtsEnable can do the (value != rtsEnable) optimization | ||||||||
| _rtsEnable = (GetDcbFlag(Interop.Kernel32.DCBFlags.FRTSCONTROL) == Interop.Kernel32.DCBRTSFlowControl.RTS_CONTROL_ENABLE); | ||||||||
|
|
||||||||
| // now set this.RtsEnable to the specified value. | ||||||||
| // Handshake takes precedence, this will be a nop if | ||||||||
| // handshake is either RequestToSend or RequestToSendXOnXOff | ||||||||
| if ((handshake != Handshake.RequestToSend && handshake != Handshake.RequestToSendXOnXOff)) | ||||||||
| RtsEnable = rtsEnable; | ||||||||
| //if device doesnt support DTR and DTR is disabled, then dont try to set DTR | ||||||||
| if (DtrEnable || ((_commProp.dwProvCapabilities & Interop.Kernel32.COMMPROP.PCF_DTRDSR) != 0)) { | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| DtrEnable = dtrEnable; | ||||||||
|
|
||||||||
| // query and cache the initial RtsEnable value | ||||||||
| // so that set_RtsEnable can do the (value != rtsEnable) optimization | ||||||||
| _rtsEnable = (GetDcbFlag(Interop.Kernel32.DCBFlags.FRTSCONTROL) == Interop.Kernel32.DCBRTSFlowControl.RTS_CONTROL_ENABLE); | ||||||||
|
|
||||||||
| // now set this.RtsEnable to the specified value. | ||||||||
| // Handshake takes precedence, this will be a nop if | ||||||||
| // handshake is either RequestToSend or RequestToSendXOnXOff | ||||||||
| if ((handshake != Handshake.RequestToSend && handshake != Handshake.RequestToSendXOnXOff)) | ||||||||
| RtsEnable = rtsEnable; | ||||||||
| } | ||||||||
|
|
||||||||
| // NOTE: this logic should match what is in the ReadTimeout property | ||||||||
| if (readTimeout == 0) | ||||||||
|
|
@@ -717,29 +720,33 @@ protected override void Dispose(bool disposing) | |||||||
|
|
||||||||
| // turn off all events and signal WaitCommEvent | ||||||||
| Interop.Kernel32.SetCommMask(_handle, 0); | ||||||||
| if (!Interop.Kernel32.EscapeCommFunction(_handle, Interop.Kernel32.CommFunctions.CLRDTR)) | ||||||||
| { | ||||||||
| int hr = Marshal.GetLastPInvokeError(); | ||||||||
|
|
||||||||
| // access denied can happen if USB is yanked out. If that happens, we | ||||||||
| // want to at least allow finalize to succeed and clean up everything | ||||||||
| // we can. To achieve this, we need to avoid further attempts to access | ||||||||
| // the SerialPort. A customer also reported seeing ERROR_BAD_COMMAND here. | ||||||||
| // Do not throw an exception on the finalizer thread - that's just rude, | ||||||||
| // since apps can't catch it and we may tear down the app. | ||||||||
| const int ERROR_DEVICE_REMOVED = 1617; | ||||||||
| if ((hr == Interop.Errors.ERROR_ACCESS_DENIED || hr == Interop.Errors.ERROR_BAD_COMMAND || hr == ERROR_DEVICE_REMOVED) && !disposing) | ||||||||
|
|
||||||||
Benkol003 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
| //if device supports DTR then clear | ||||||||
| if ((_commProp.dwProvCapabilities & Interop.Kernel32.COMMPROP.PCF_DTRDSR) != 0) { | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| if (!Interop.Kernel32.EscapeCommFunction(_handle, Interop.Kernel32.CommFunctions.CLRDTR)) | ||||||||
| { | ||||||||
| skipSPAccess = true; | ||||||||
| } | ||||||||
| else | ||||||||
| { | ||||||||
| // should not happen | ||||||||
| Debug.Fail($"Unexpected error code from EscapeCommFunction in SerialPort.Dispose(bool) Error code: 0x{(uint)hr:x}"); | ||||||||
| int hr = Marshal.GetLastPInvokeError(); | ||||||||
|
|
||||||||
| // Do not throw an exception from the finalizer here. | ||||||||
| if (disposing) | ||||||||
| throw Win32Marshal.GetExceptionForLastWin32Error(); | ||||||||
| // access denied can happen if USB is yanked out. If that happens, we | ||||||||
| // want to at least allow finalize to succeed and clean up everything | ||||||||
| // we can. To achieve this, we need to avoid further attempts to access | ||||||||
| // the SerialPort. A customer also reported seeing ERROR_BAD_COMMAND here. | ||||||||
| // Do not throw an exception on the finalizer thread - that's just rude, | ||||||||
| // since apps can't catch it and we may tear down the app. | ||||||||
| const int ERROR_DEVICE_REMOVED = 1617; | ||||||||
| if ((hr == Interop.Errors.ERROR_ACCESS_DENIED || hr == Interop.Errors.ERROR_BAD_COMMAND || hr == ERROR_DEVICE_REMOVED) && !disposing) | ||||||||
| { | ||||||||
| skipSPAccess = true; | ||||||||
| } | ||||||||
| else | ||||||||
| { | ||||||||
| // should not happen | ||||||||
| Debug.Fail($"Unexpected error code from EscapeCommFunction in SerialPort.Dispose(bool) Error code: 0x{(uint)hr:x}"); | ||||||||
|
|
||||||||
| // Do not throw an exception from the finalizer here. | ||||||||
| if (disposing) | ||||||||
| throw Win32Marshal.GetExceptionForLastWin32Error(); | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
|
|
||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.