Hi Robert,
> it would even work on NT 3.1: http://support.microsoft.com/kb/102988
I thought it would, thanks for confirming.
> Please add a null check for "subkey" after
>
> RegistryKey subkey =
> Registry.LocalMachine.OpenSubKey("HARDWARE\\DEVICEMAP\\SERIALCOMM");
>
> and consider the "using" pattern.
Done, attached.
> Shouldn't be the default port name the first entry of this
> list? Your patch presets "COM1".
Yes i agree and have changed it to match. Anyone else got any thoughts on
this?
I checked on MSDN and it does say that COM1 should be default but it should
always be first on the list, if it exists. I left in the previous defaults if
GetPortNames doesn't return anything as i don't think it is supposed to throw
an exception at that stage.
Martin
Index: SerialPort.cs
===================================================================
--- SerialPort.cs (revision 90526)
+++ SerialPort.cs (working copy)
@@ -25,6 +25,7 @@
using System.Diagnostics;
using System.Text;
using System.Runtime.InteropServices;
+using Microsoft.Win32;
namespace System.IO.Ports
{
@@ -59,8 +60,6 @@
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 @@
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 @@
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);
}
- return serial_ports.ToArray ();
+ } 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);
+ }
+ }
+ }
}
- throw GetNotImplemented ();
+ return serial_ports.ToArray();
}
static bool IsWindows {
_______________________________________________
Mono-devel-list mailing list
[email protected]
http://lists.ximian.com/mailman/listinfo/mono-devel-list