From eab26ddfbf4ca7a7a8f99c0f97dd9593d96c1255 Mon Sep 17 00:00:00 2001 From: Bart Jansen Date: Fri, 24 Sep 2021 15:26:18 +0200 Subject: [PATCH] Fix Linux SerialPort lookup --- .../PortSerial/DeviceWatcher.cs | 62 ++++--------------- 1 file changed, 11 insertions(+), 51 deletions(-) diff --git a/nanoFramework.Tools.DebugLibrary.Shared/PortSerial/DeviceWatcher.cs b/nanoFramework.Tools.DebugLibrary.Shared/PortSerial/DeviceWatcher.cs index a619d84b..2963a7f4 100644 --- a/nanoFramework.Tools.DebugLibrary.Shared/PortSerial/DeviceWatcher.cs +++ b/nanoFramework.Tools.DebugLibrary.Shared/PortSerial/DeviceWatcher.cs @@ -99,63 +99,23 @@ public List GetPortNames() private List GetPortNames_Linux() { - const string sysTtyDir = "/sys/class/tty"; - const string sysUsbDir = "/sys/bus/usb-serial/devices/"; - const string devDir = "/dev/"; + List ports = new List(); - if (Directory.Exists(sysTtyDir)) + string[] ttys = System.IO.Directory.GetFiles("/dev/", "tty*"); + foreach (string dev in ttys) { - // /sys is mounted. Let's explore tty class and pick active nodes. - List ports = new List(); - 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(); - 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 GetPortNames_OSX()