Skip to content

Conversation

@jarnedemeulemeester
Copy link

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.

…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.
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Nov 8, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Nov 8, 2025
@jarnedemeulemeester
Copy link
Author

@dotnet-policy-service agree

@huoyaoyuan huoyaoyuan added area-System.IO.Ports and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Nov 8, 2025
Comment on lines 637 to 641
try
{
DtrEnable = dtrEnable;
}
catch (IOException) when (dtrEnable == false)
Copy link
Member

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.

Copy link
Author

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?

Copy link
Member

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.

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.

@jarnedemeulemeester jarnedemeulemeester marked this pull request as ready for review November 11, 2025 14:39
Copilot AI review requested due to automatic review settings November 11, 2025 14:39
Copilot finished reviewing on behalf of jarnedemeulemeester November 11, 2025 14:41
Copy link
Contributor

Copilot AI left a 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 DtrEnable property assignment in a try-catch block that swallows IOException when setting to false (default value)
  • Wrapped RTS-related operations (querying and setting RtsEnable) in a try-catch block that swallows IOException when setting to false (default value)
  • Added explanatory comments matching the Unix implementation's pattern

Comment on lines +643 to +644
// 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
Copy link

Copilot AI Nov 11, 2025

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'.

Copilot uses AI. Check for mistakes.
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
Copy link

Copilot AI Nov 11, 2025

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'.

Suggested change
// 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

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.IO.Ports community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SerialPort Open fails with 0x8007001f

2 participants