-
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?
Catch exception in SerialStream constructor if DTR or RTS is not available (Windows) #121468
Conversation
…fault values Some ports do not support setting DTR or RTS. By catching the exeption when setting the value to their default value we can allow these ports to function.
|
@dotnet-policy-service agree |
| try | ||
| { | ||
| DtrEnable = dtrEnable; | ||
| } | ||
| catch (IOException) when (dtrEnable == false) |
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/RtsEnable can be refactored into methods of TryEnable, then the callers here can call the TryEnable methods 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.
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.
Pull Request Overview
This PR adds exception handling in the SerialStream constructor for Windows to gracefully handle IOException when DTR or RTS control signals are not supported by the serial port hardware. This brings Windows implementation in line with the existing Unix behavior.
Key Changes:
- Wrapped
DtrEnableproperty assignment in a try-catch block that swallowsIOExceptionwhen setting tofalse(default value) - Wrapped RTS-related operations (querying and setting
RtsEnable) in a try-catch block that swallowsIOExceptionwhen setting tofalse(default value) - Added explanatory comments matching the Unix implementation's pattern
| // 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 |
Copilot
AI
Nov 11, 2025
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.
The comment refers to 'termios command' which is Unix-specific terminology. On Windows, this should refer to Windows API calls like SetCommState or EscapeCommFunction instead. Consider updating to: 'the required Windows API calls for setting DtrEnable'.
| catch (IOException) when (!rtsEnable) | ||
| { | ||
| // 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 |
Copilot
AI
Nov 11, 2025
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.
The comment refers to 'termios command' which is Unix-specific terminology. On Windows, this should refer to Windows API calls like SetCommState or EscapeCommFunction instead. Consider updating to: 'the required Windows API calls for setting RtsEnable'.
| // 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 |
Catch IOException in SerialStream constructor when setting DTR or RTS is not possible. This will allow ports that do not support setting DTR or RTS to function normally.
This is the Windows equivalent of #48577.
Fixes #27729.