[Mono-dev] Make System.IO.Ports work on UNIX systems other than Linux

Robert Nagy robert at openbsd.org
Thu Apr 22 12:45:17 EDT 2010


Can someone please have a look at this?

On (2010-04-20 23:26), Robert Nagy wrote:
> Hi
> 
> The attached diff makes SerialPort.GetPortNames() work on
> all Unix systems other than Linux too, because ttyS* and
> ttyUSB* is linux specific and on *BSD the serial ports are
> tty[0-9]+.
> (I've tested this code on Linux and it should also support
> ttySG0 (SGI running Linux (ia64)).
> 
> The other way would be to add a different platform id for 
> *BSDs.
> 
> Comments? (My C# is not good :)) 

> Index: class/System/System.IO.Ports/SerialPort.cs
> ===================================================================
> --- class/System/System.IO.Ports/SerialPort.cs	(revision 155801)
> +++ class/System/System.IO.Ports/SerialPort.cs	(working copy)
> @@ -24,6 +24,7 @@
>  using System.ComponentModel;
>  using System.Diagnostics;
>  using System.Text;
> +using System.Text.RegularExpressions;
>  using System.Runtime.InteropServices;
>  using Microsoft.Win32;
>  
> @@ -525,10 +526,18 @@
>  			// Are we on Unix?
>  			if (p == 4 || p == 128 || p == 6) {
>  				string[] ttys = Directory.GetFiles("/dev/", "tty*");
> +				Regex lnx = new Regex(@"^\/dev\/tty(S(G)?|USB)[0-9]+$");
> +				Regex rem = new Regex(@"^\/dev\/tty(U)?[0-9]+$");
> +
>  				foreach (string dev in ttys) {
> -					if (dev.StartsWith("/dev/ttyS") || dev.StartsWith("/dev/ttyUSB"))
> -						serial_ports.Add(dev);
> +					if (lnx.Match(dev).Success)
> +						rem = lnx;
> +					serial_ports.Add(dev);
>  				}
> +				for (int i = serial_ports.Count - 1; i >= 0; i--) {
> +					if (!rem.Match(serial_ports[i]).Success)
> +						serial_ports.RemoveAt(i);
> +				}
>  			} else {
>  				using (RegistryKey subkey = Registry.LocalMachine.OpenSubKey("HARDWARE\\DEVICEMAP\\SERIALCOMM"))
>  				{

> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list



More information about the Mono-devel-list mailing list