[Mono-dev] Make System.IO.Ports work on UNIX systems other than Linux
Andreas Färber
andreas.faerber at web.de
Thu Apr 22 13:48:22 EDT 2010
Hi,
Am 22.04.2010 um 18:45 schrieb Robert Nagy:
> Can someone please have a look at this?
>
> On (2010-04-20 23:26), Robert Nagy wrote:
>> 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]+$");
I'm not a Linux expert, but I think the regex still may be incomplete.
For instance, I've seen tty0 (virtio), ttyPZ0 (ppc), ttySC0 (sh4 R2D),
ttyAMA0 (arm RealView) as kernel parameters.
>> + 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);
>> + }
This seems overly complicated to me. Both the variable naming and
cross-assignment and the first-add-then-remove approach.
I think we should either try to find one universal regex like /dev/
tty[A-Z]*[0-9]+ or initialize the regex based on platform. Either way
we'd hopefully have one regex we could use to add to the list and
could drop the second loop.
Platform identification could be done by recognizing a non-Windows
platform and p/invoking uname.
Andreas
More information about the Mono-devel-list
mailing list