[Mono-dev] Make System.IO.Ports work on UNIX systems other than Linux
Robert Nagy
robert at openbsd.org
Tue Apr 20 17:26:13 EDT 2010
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 :))
-------------- next part --------------
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"))
{
More information about the Mono-devel-list
mailing list