This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
minimal support for serial port on Linux. part 2. #30903
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
86659bd
minimal support for serial port on Linux
854bb5c
feedback from review
ea9751a
rename SerialStream -> SerialStream.Windows
3807c29
Merge branch 'master' of https://github.com/dotnet/corefx into serial3
wfurt 1c79103
some fixes and improvements
8f3a66d
Merge branch 'serial3' of https://github.com/wfurt/corefx into serial3
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
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
43 changes: 43 additions & 0 deletions
43
src/System.IO.Ports/src/System/IO/Ports/SerialPort.Linux.cs
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 |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using System.ComponentModel; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
|
|
||
| namespace System.IO.Ports | ||
| { | ||
| public partial class SerialPort : Component | ||
| { | ||
| public static string[] GetPortNames() | ||
| { | ||
| const string sysTtyDir = "/sys/class/tty"; | ||
| const string sysUsbDir = "/sys/bus/usb-serial/devices/"; | ||
|
|
||
| if (Directory.Exists(sysTtyDir)) | ||
| { | ||
| // /sys is mounted. Let's explore tty class and pick active nodes. | ||
| List<string> ports = new List<string>(); | ||
| DirectoryInfo di = new DirectoryInfo(sysTtyDir); | ||
| var entries = di.EnumerateFileSystemInfos(@"*", SearchOption.TopDirectoryOnly); | ||
| foreach (var entry in entries) | ||
| { | ||
| if (Directory.Exists(sysUsbDir + entry.Name) || File.Exists(entry.FullName + "/device/id")) | ||
| { | ||
| ports.Add("/dev/" + entry.Name); | ||
| } | ||
| } | ||
|
|
||
| return ports.ToArray(); | ||
| } | ||
| else | ||
| { | ||
| // Fallback to scanning /dev. That may have more devices then needed. | ||
| // This can also miss usb or serial devices with non-standard name. | ||
| return Directory.GetFiles("/dev", "ttyS*"); | ||
| } | ||
| } | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using System.ComponentModel; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
|
|
||
| namespace System.IO.Ports | ||
| { | ||
| public partial class SerialPort : Component | ||
| { | ||
| public static string[] GetPortNames() | ||
| { | ||
| List<string> ports = new List<string>(); | ||
|
|
||
| foreach (string name in Directory.GetFiles("/dev", "tty.*")) | ||
|
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. nit: not sure if it is worth but since |
||
| { | ||
| // GetFiles can return unexpected results because of 8.3 matching. | ||
|
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. @JeremyKuhne does this applies to macOS? |
||
| // Like /dev/tty | ||
| if (name.StartsWith("/dev/tty.")) | ||
| { | ||
| ports.Add(name); | ||
| } | ||
| } | ||
|
|
||
| foreach (string name in Directory.GetFiles("/dev", "cu.*")) | ||
| { | ||
| if (name.StartsWith("/dev/cu.")) | ||
| { | ||
| ports.Add(name); | ||
| } | ||
| } | ||
|
|
||
| return ports.ToArray(); | ||
| } | ||
| } | ||
| } | ||
File renamed without changes.
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.
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.
This file is not being referenced in the csproj. Are planning to include support to macOS? If yes the csproj need a few updates besides including this file.
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.
yes. This is part I tested so far so I was not comfortable enable full build.
That will probably come when Linux is stable.