diff options
author | Miguel de Icaza <miguel@gnome.org> | 2007-12-03 22:40:03 +0300 |
---|---|---|
committer | Miguel de Icaza <miguel@gnome.org> | 2007-12-03 22:40:03 +0300 |
commit | 564992162e465fffc51d2d17a1210bbecb0c60fe (patch) | |
tree | a6de9eb36245dccbcd0f1547769522d97abfd9f0 /mcs/class/System/System.IO.Ports/SerialPort.cs | |
parent | 1ee31fadfe5ae0f5b1cf6ea4428c09fa66dc3ed7 (diff) |
2007-12-03 Miguel de Icaza <miguel@novell.com>
* SerialPort.cs: Apply patch from Martin Green
<martin@martsoft.co.uk> to support Serial Ports enumeration on
Windows.
svn path=/trunk/mcs/; revision=90583
Diffstat (limited to 'mcs/class/System/System.IO.Ports/SerialPort.cs')
-rw-r--r-- | mcs/class/System/System.IO.Ports/SerialPort.cs | 48 |
1 files changed, 30 insertions, 18 deletions
diff --git a/mcs/class/System/System.IO.Ports/SerialPort.cs b/mcs/class/System/System.IO.Ports/SerialPort.cs index 7ae1da7fad9..1c7b3c9d5a2 100644 --- a/mcs/class/System/System.IO.Ports/SerialPort.cs +++ b/mcs/class/System/System.IO.Ports/SerialPort.cs @@ -25,6 +25,7 @@ using System.ComponentModel; using System.Diagnostics; using System.Text; using System.Runtime.InteropServices; +using Microsoft.Win32; namespace System.IO.Ports { @@ -59,8 +60,6 @@ namespace System.IO.Ports object error_received = new object (); object data_received = new object (); object pin_changed = new object (); - - static string default_port_name = "ttyS0"; public SerialPort () : this (GetDefaultPortName (), DefaultBaudRate, DefaultParity, DefaultDataBits, DefaultStopBits) @@ -103,7 +102,16 @@ namespace System.IO.Ports static string GetDefaultPortName () { - return default_port_name; + string[] ports = GetPortNames(); + if (ports.Length > 0) { + return ports[0]; + } else { + int p = (int)Environment.OSVersion.Platform; + if (p == 4 || p == 128) + return "ttyS0"; // Default for Unix + else + return "COM1"; // Default for Windows + } } [Browsable (false)] @@ -506,28 +514,32 @@ namespace System.IO.Ports stream.DiscardOutBuffer (); } - static Exception GetNotImplemented () - { - return new NotImplementedException ("Detection of ports is not implemented for this platform yet."); - } - public static string [] GetPortNames () { int p = (int) Environment.OSVersion.Platform; + List<string> serial_ports = new List<string>(); // Are we on Unix? - if (p == 4 || p == 128){ - string [] ttys = Directory.GetFiles ("/dev/", "tty*"); - List<string> serial_ports = new List<string> (); - - foreach (string dev in ttys){ - if (dev.StartsWith ("/dev/ttyS") || dev.StartsWith ("/dev/ttyUSB")) - serial_ports.Add (dev); - + if (p == 4 || p == 128) { + string[] ttys = Directory.GetFiles("/dev/", "tty*"); + foreach (string dev in ttys) { + if (dev.StartsWith("/dev/ttyS") || dev.StartsWith("/dev/ttyUSB")) + serial_ports.Add(dev); + } + } else { + using (RegistryKey subkey = Registry.LocalMachine.OpenSubKey("HARDWARE\\DEVICEMAP\\SERIALCOMM")) + { + if (subkey != null) { + string[] names = subkey.GetValueNames(); + foreach (string value in names) { + string port = subkey.GetValue(value, "").ToString(); + if (port != "") + serial_ports.Add(port); + } + } } - return serial_ports.ToArray (); } - throw GetNotImplemented (); + return serial_ports.ToArray(); } static bool IsWindows { |