Skip to content
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

Improved Linux SerialPort lookup #320

Merged
merged 1 commit into from
Sep 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 11 additions & 51 deletions nanoFramework.Tools.DebugLibrary.Shared/PortSerial/DeviceWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,63 +99,23 @@ public List<string> GetPortNames()

private List<string> GetPortNames_Linux()
{
const string sysTtyDir = "/sys/class/tty";
const string sysUsbDir = "/sys/bus/usb-serial/devices/";
const string devDir = "/dev/";
List<string> ports = new List<string>();

if (Directory.Exists(sysTtyDir))
string[] ttys = System.IO.Directory.GetFiles("/dev/", "tty*");
foreach (string dev in ttys)
{
bart-jansen marked this conversation as resolved.
Show resolved Hide resolved
// /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 (dev.StartsWith("/dev/ttyS")
|| dev.StartsWith("/dev/ttyUSB")
|| dev.StartsWith("/dev/ttyACM")
|| dev.StartsWith("/dev/ttyAMA")
|| dev.StartsWith("/dev/ttyPS")
|| dev.StartsWith("/dev/serial"))
{
// /sys/class/tty contains some bogus entries such as console, tty
// and a lot of bogus ttyS* entries mixed with correct ones.
// console and tty can be filtered out by checking for presence of device/tty
// ttyS entries pass this check but those can be filtered out
// by checking for presence of device/id or device/of_node
// checking for that for non-ttyS entries is incorrect as some uart
// devices are incorrectly filtered out
bool isTtyS = entry.Name.StartsWith("ttyS", StringComparison.Ordinal);
bool isTtyGS = !isTtyS && entry.Name.StartsWith("ttyGS", StringComparison.Ordinal);
if ((isTtyS &&
(File.Exists(entry.FullName + "/device/id") ||
Directory.Exists(entry.FullName + "/device/of_node"))) ||
(!isTtyS && Directory.Exists(entry.FullName + "/device/tty")) ||
Directory.Exists(sysUsbDir + entry.Name) ||
(isTtyGS && (File.Exists(entry.FullName + "/dev"))))
{
string deviceName = devDir + entry.Name;
if (File.Exists(deviceName))
{
ports.Add(deviceName);
}
}
ports.Add(dev);
}

return ports;
}
else
{
// Fallback to scanning /dev. That may have more devices then needed.
// This can also miss usb or serial devices with non-standard name.
var ports = new List<string>();
foreach (var portName in Directory.EnumerateFiles(devDir, "tty*"))
{
if (portName.StartsWith("/dev/ttyS", StringComparison.Ordinal) ||
portName.StartsWith("/dev/ttyUSB", StringComparison.Ordinal) ||
portName.StartsWith("/dev/ttyACM", StringComparison.Ordinal) ||
portName.StartsWith("/dev/ttyAMA", StringComparison.Ordinal) ||
portName.StartsWith("/dev/ttymxc", StringComparison.Ordinal))
{
ports.Add(portName);
}
}

return ports;
}
return ports;
}

private List<string> GetPortNames_OSX()
Expand Down