[System] Removal of the NET_2_0 in the source code
[mono.git] / mcs / class / System / System.IO.Ports / SerialPort.cs
index 8a2f2b6db8246ebb57e1843bde81e0d906ae0755..4dff4ca7540f87eed14ad5be1a3b5fee2f26e528 100644 (file)
 //     reads 
 //
 
-#if NET_2_0
-
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Diagnostics;
 using System.Text;
 using System.Runtime.InteropServices;
+using Microsoft.Win32;
 
 namespace System.IO.Ports
 {
@@ -59,8 +58,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,16 +100,23 @@ 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 || p == 6)
+                                       return "ttyS0"; // Default for Unix
+                               else
+                                       return "COM1"; // Default for Windows
+                       }
                }
 
                [Browsable (false)]
                [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
                public Stream BaseStream {
                        get {
-                               if (!is_open)
-                                       throw new InvalidOperationException ();
-
+                               CheckOpen ();
                                return (Stream) stream;
                        }
                }
@@ -211,11 +215,12 @@ namespace System.IO.Ports
                [DefaultValue (false)]
                public bool DiscardNull {
                        get {
-                               CheckOpen ();
                                throw new NotImplementedException ();
                        }
                        set {
-                               CheckOpen ();
+                               // LAMESPEC: Msdn states that an InvalidOperationException exception
+                               // is fired if the port is not open, which is *not* happening.
+
                                throw new NotImplementedException ();
                        }
                }
@@ -296,6 +301,8 @@ namespace System.IO.Ports
                        set {
                                if (value == null)
                                        throw new ArgumentNullException ("value");
+                               if (value.Length == 0)
+                                       throw new ArgumentException ("NewLine cannot be null or empty.", "value");
                                
                                new_line = value;
                        }
@@ -322,6 +329,7 @@ namespace System.IO.Ports
                [MonoTODO("Not implemented")]
                [Browsable (true)]
                [MonitoringDescription ("")]
+               [DefaultValue (63)]
                public byte ParityReplace {
                        get {
                                throw new NotImplementedException ();
@@ -334,6 +342,7 @@ namespace System.IO.Ports
                
                [Browsable (true)]
                [MonitoringDescription ("")]
+               [DefaultValue ("COM1")] // silly Windows-ism. We should ignore it.
                public string PortName {
                        get {
                                return port_name;
@@ -377,7 +386,7 @@ namespace System.IO.Ports
                                return read_timeout;
                        }
                        set {
-                               if (value <= 0 && value != InfiniteTimeout)
+                               if (value < 0 && value != InfiniteTimeout)
                                        throw new ArgumentOutOfRangeException ("value");
 
                                if (is_open)
@@ -465,7 +474,7 @@ namespace System.IO.Ports
                                return write_timeout;
                        }
                        set {
-                               if (value <= 0 && value != InfiniteTimeout)
+                               if (value < 0 && value != InfiniteTimeout)
                                        throw new ArgumentOutOfRangeException ("value");
 
                                if (is_open)
@@ -479,7 +488,7 @@ namespace System.IO.Ports
 
                public void Close ()
                {
-                       Dispose (false);
+                       Dispose (true);
                }
 
                protected override void Dispose (bool disposing)
@@ -488,7 +497,9 @@ namespace System.IO.Ports
                                return;
                        
                        is_open = false;
-                       stream.Close ();
+                       // Do not close the base stream when the finalizer is run; the managed code can still hold a reference to it.
+                       if (disposing)
+                               stream.Close ();
                        stream = null;
                }
 
@@ -504,28 +515,49 @@ 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 || p == 6) {
+                               string[] ttys = Directory.GetFiles("/dev/", "tty*");
+                               bool linux_style = false;
+
+                               //
+                               // Probe for Linux-styled devices: /dev/ttyS* or /dev/ttyUSB*
+                               // 
+                               foreach (string dev in ttys) {
+                                       if (dev.StartsWith("/dev/ttyS") || dev.StartsWith("/dev/ttyUSB")){
+                                               linux_style = true;
+                                               break;
+                                       }
+                               }
+
+                               foreach (string dev in ttys) {
+                                       if (linux_style){
+                                               if (dev.StartsWith("/dev/ttyS") || dev.StartsWith("/dev/ttyUSB"))
+                                                       serial_ports.Add (dev);
+                                       } else {
+                                               if (dev != "/dev/tty" && dev.StartsWith ("/dev/tty") && !dev.StartsWith ("/dev/ttyC"))
+                                                       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 {
@@ -542,8 +574,8 @@ namespace System.IO.Ports
                        
 #if !TARGET_JVM
                        if (IsWindows) // Use windows kernel32 backend
-                               stream = new WinSerialStream (port_name, baud_rate, data_bits, parity, stop_bits,
-                                               handshake, read_timeout, write_timeout, readBufferSize, writeBufferSize);
+                               stream = new WinSerialStream (port_name, baud_rate, data_bits, parity, stop_bits, dtr_enable,
+                                       rts_enable, handshake, read_timeout, write_timeout, readBufferSize, writeBufferSize);
                        else // Use standard unix backend
 #endif
                                stream = new SerialPortStream (port_name, baud_rate, data_bits, parity, stop_bits, dtr_enable,
@@ -567,8 +599,7 @@ namespace System.IO.Ports
                        return stream.Read (buffer, offset, count);
                }
 
-               [Obsolete("Read of char buffers is currently broken")]
-               [MonoTODO("This is broken")]
+               [MonoTODO("Read of char buffers is currently broken")]
                public int Read (char[] buffer, int offset, int count)
                {
                        CheckOpen ();
@@ -635,17 +666,7 @@ namespace System.IO.Ports
 
                public string ReadLine ()
                {
-                       CheckOpen ();
-                       List<byte> bytes_read = new List<byte>();
-                       byte [] buff = new byte [1];
-                       
-                       while (true){
-                               int n = stream.Read (buff, 0, 1);
-                               if (n == -1 || buff [0] == '\n')
-                                       break;
-                               bytes_read.Add (buff [0]);
-                       } 
-                       return new String (encoding.GetChars (bytes_read.ToArray ()));
+                       return ReadTo (new_line);
                }
 
                public string ReadTo (string value)
@@ -708,12 +729,13 @@ namespace System.IO.Ports
                        CheckOpen ();
                        if (buffer == null)
                                throw new ArgumentNullException ("buffer");
-                       if (offset < 0 || offset >= buffer.Length)
-                               throw new ArgumentOutOfRangeException ("offset");
-                       if (count < 0 || count > buffer.Length)
-                               throw new ArgumentOutOfRangeException ("count");
-                       if (count > buffer.Length - offset)
-                               throw new ArgumentException ("count > buffer.Length - offset");
+
+                       if (offset < 0 || count < 0)
+                               throw new ArgumentOutOfRangeException ();
+
+                       if (buffer.Length - offset < count)
+                               throw new ArgumentException ("offset+count",
+                                                            "The size of the buffer is less than offset + count.");
 
                        byte [] bytes = encoding.GetBytes (buffer, offset, count);
                        stream.Write (bytes, 0, bytes.Length);
@@ -783,4 +805,3 @@ namespace System.IO.Ports
 
 }
 
-#endif