I am planning on re-writing a Win32 application (native C++) in .NET - most likely using mono so I can run it on Win32, Linux and mac. The problem I am trying to solve (having only really developed for Win32) is an issue with the serial port definition. How does one typically identify differences in platform when there is only supposed to be one executable. Specifically, the COM port is identified in windows as COM1 or something like that (\.\COM) but on linux they are specified as something like /dev/ttyS0.
Does one check the platform at runtime for this information?
I think the only difference would be in the opening and closing of the port. The reading and writing is the same.
Perhaps this is a more generic question in that it applies to any platform-specific stuff in mono/.NET.
How do you handle this? In some string resource or config file, or hard-coded and switch based on runtime platform?
Any code sample for this? Note am a C++ developer and not familiar with all the classes available in .NET. is there a way to get either the serial port naming scheme from the CLR or is there a way to get the OS/Platform from the CLR?
it would have been nice for the CLR to have been able to present the serial ports in a more independent manner. Maybe that is not possible.
Thanks,
Tim
EDIT
Given the one response so far I guess I should just try the
SerialPort.GetPortNames() enumeration first to see if it works. (on both platforms)
In win32 for the higher port numbers and ofr the USB dongle - it is not as simple as the basic COM port enumeration.
I will report the findings here.
Just discovered this thread, and thought I aught to just add my discoveries: randomly, I am also worried about this on a mac. In Windows and Linux (in VS and Mono alike) SerialPort.GetPortNames() return a list, with the following rules:
1) Windows returns a list of strings like Com1, Com4, missing out any which don't exist (USB Serial adapters seem to take a COM number based on the plug they are plugged into, consistantly) From my serial port scanner:
Scanning COM1
Scanning COM4
Scanning Complete
2) Linux returns all possibly tty serial ports that the compiler of the linux distro has enabled. This seems to be about 8 ports, which if you try to open, will throw an exception (from my serial port scanner:
Scanning /dev/ttyS0
Scanning /dev/ttyS1 Port FailedSystem.IO.IOException: I/O Error
at System.IO.Ports.SerialPortStream..ctor (System.String portName, Int32 baudRate, Int32 dataBits, Parity parity, StopBits stopBits, Boolean dtrEnable, Boolean rtsEnable, Handshake handshake, Int32 readTimeout, Int32 writeTimeout, Int32 readBufferSize, Int32 writeBufferSize) [0x00000]
at (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPortStream:.ctor (string,int,int,System.IO.Ports.Parity,System.IO.Ports.StopBits,bool,bool,System.IO.Ports.Handshake,int,int,int,int)
at System.IO.Ports.SerialPort.Open () [0x00000]
at (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:Open ()
at HSMScanner.Program.Main (System.String[] args) [0x00000]
Scanning /dev/ttyS2 Port FailedSystem.IO.IOException: I/O Error
at System.IO.Ports.SerialPortStream..ctor (System.String portName, Int32 baudRate, Int32 dataBits, Parity parity, StopBits stopBits, Boolean dtrEnable, Boolean rtsEnable, Handshake handshake, Int32 readTimeout, Int32 writeTimeout, Int32 readBufferSize, Int32 writeBufferSize) [0x00000]
at (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPortStream:.ctor (string,int,int,System.IO.Ports.Parity,System.IO.Ports.StopBits,bool,bool,System.IO.Ports.Handshake,int,int,int,int)
at System.IO.Ports.SerialPort.Open () [0x00000]
at (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:Open ()
at HSMScanner.Program.Main (System.String[] args) [0x00000]
Scanning /dev/ttyS3 Port FailedSystem.IO.IOException: I/O Error
at System.IO.Ports.SerialPortStream..ctor (System.String portName, Int32 baudRate, Int32 dataBits, Parity parity, StopBits stopBits, Boolean dtrEnable, Boolean rtsEnable, Handshake handshake, Int32 readTimeout, Int32 writeTimeout, Int32 readBufferSize, Int32 writeBufferSize) [0x00000]
at (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPortStream:.ctor (string,int,int,System.IO.Ports.Parity,System.IO.Ports.StopBits,bool,bool,System.IO.Ports.Handshake,int,int,int,int)
at System.IO.Ports.SerialPort.Open () [0x00000]
at (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:Open ()
at HSMScanner.Program.Main (System.String[] args) [0x00000]
3) Macs...
Oh dear oh dear. Macs (when a usb serial port is plugged in and drivers and everything are ok) don't return anything on the GetPortNames(). Nada. Looking in the /dev/tty, the extra devices appear only when the device is plugged in and has names like /dev/tty.usbserial-A7006Ro7 unfortunatly, using this name as an argument to the program followed by serial.open dosnt seem to have any effect.
Looking more into it.
In .Net, you need System.IO.Ports. Names like "COM1" and "/dev/ttyS0" are random OS names. Why do you need to hardcode them? The user should just pick one from the list of available ports.
Obviously, you determine this list at runtime. In fact, with USB-to-serial adaptors you might want to consider the possibility that the user discovers only after starting your program that he forgot to connect his USB-to-serial adaptor. That means you should reread this list every time you show the config dialog. The USB adaptor is quite likely not COM1, BTW.
(I can't vouch for the quality of the Mono implementation of SerialPort. A quick google search left me a bit worried, but test yourself)
I had a look at this and it seems the mono code for enumerating serial ports is not going to work on anything but Linux.
(from mcs/class/System/System.IO.Ports/SerialPort.cs)
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 || p == 6) {
string[] ttys = Directory.GetFiles("/dev/", "tty*");
foreach (string dev in ttys) {
if (dev.StartsWith("/dev/ttyS") || dev.StartsWith("/dev/ttyUSB"))
serial_ports.Add(dev);
}
} else {
On OSX I would think you could probably match /dev/tty.* (FreeBSD would need /dev/ttyU[0-9]+)
I can't say I'm enamoured of the way it picks which OS it's running under either..