Catch exception in SerialStream constructor if DTR or RTS is not available (Windows)#121468
Catch exception in SerialStream constructor if DTR or RTS is not available (Windows)#121468jarnedemeulemeester wants to merge 2 commits intodotnet:mainfrom
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.
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.
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.
It's up to the area owners for decision. Following existing pattern is also acceptable.
There was a problem hiding this comment.
I'll leave it up to the area owners then.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
|
i was trying this out on a system with COM ports without DTR/DSR, DtrEnable is also set in SerialStream.Dispose() (from port.Close() ) to clear DTR that will throw an exception. |
|
@jarnedemeulemeester @huoyaoyuan @Benkol003 thanks for the engagement here. Current approach given it follows existing patterns is fine:
so I'm OK with this as is but in general I agree with the comments that better approach is to either do feather presence check or detect error on the WinAPI level. I'll leave it for you to decide if you want to pursuit better approach. |
krwq
left a comment
There was a problem hiding this comment.
Please write a comment if you'll be pursuing better approach or leaving as is.
|
See #122454 . I'd also be willing to extend this to check the remaining device capabilities |
|
@jarnedemeulemeester @Benkol003 thank you both for contribution. I just caught up after holidays break. Does it make sense to merge this PR given #122454 seems like an extended version of this? I do prefer capabilities check if that's reliable enough (not talking about single device driver which might report this incorrectly but if most of them does misreport we could do both capability check and try catch). I'd appreciate if @jarnedemeulemeester could test @Benkol003's solution once ready if that's feasible. |
| // 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 |
There was a problem hiding this comment.
The comment mentions "termios command" which is Unix-specific terminology. This should reference Windows-specific APIs instead. For Windows, the IOException is thrown when SetCommState or EscapeCommFunction fails for devices that don't support RTS control.
|
This pull request has been automatically marked |
|
This pull request will now be closed since it had been marked |
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.