-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Catch exception in SerialStream constructor if DTR or RTS is not available (Windows) #121468
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -634,17 +634,37 @@ | |||||
| // 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); | ||||||
| try | ||||||
| { | ||||||
| DtrEnable = dtrEnable; | ||||||
| } | ||||||
| catch (IOException) when (dtrEnable == false) | ||||||
|
Check failure on line 641 in src/libraries/System.IO.Ports/src/System/IO/Ports/SerialStream.Windows.cs
|
||||||
| { | ||||||
| // An IOException can be thrown when using a port which doesn't implement | ||||||
| // the required termios command for setting DtrEnable, but it still works without setting the value | ||||||
|
Comment on lines
+643
to
+644
|
||||||
| // so we ignore this error in the constructor only if being set to false (which is the default). | ||||||
| // When the property is set manually the exception is still thrown. | ||||||
| } | ||||||
|
|
||||||
| // 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; | ||||||
| try | ||||||
| { | ||||||
| // 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; | ||||||
| } | ||||||
| catch (IOException) when (rtsEnable == false) | ||||||
|
Check failure on line 661 in src/libraries/System.IO.Ports/src/System/IO/Ports/SerialStream.Windows.cs
|
||||||
| { | ||||||
| // An IOException can be thrown when using a port which doesn't implement | ||||||
| // the required termios command for setting RtsEnable, but it still works without setting the value | ||||||
|
||||||
| // the required termios command for setting RtsEnable, but it still works without setting the value | |
| // the required Windows API calls (such as SetCommState or EscapeCommFunction) for setting RtsEnable, but it still works without setting the value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not a good approach to throw and immediately catch the exception.
Instead, the setter of
DtrEnable/RtsEnablecan be refactored into methods ofTryEnable, then the callers here can call theTryEnablemethods directly and ignores the failure.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I will try to refactor the setters into methods.
I just took this approach from the unix implementation.
Do you want me to also refactor the unix implementation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's up to the area owners for decision. Following existing pattern is also acceptable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll leave it up to the area owners then.