I’m trying to put together a cross-platform application that interfaces with Insteon automation hardware. In the .Net based examples I’ve found, the System.IO.Ports.SerialPort class is used to read data from both an RS-232 Serial Port based device and a USB Serial Modem. On a Windows platform, the Serial Modems generally get mapped to a virtual COM Port and it’s not to difficult to run down a list, passing in new COM addresses and attempting to figure out which one the USB or Serial device is attached to.
Unfortunately on the Mac, I’m totally out of my element. MonoMac allows me to program in a more comfortable language, but something as low level as talking to hardware is a bit beyond my reach. My initialization code is as follows:
private void OpenSerialConnection(string comPort)
{
_serialPort = new System.IO.Ports.SerialPort("/dev/ttys1", 19200, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
_serialPort.Encoding = System.Text.Encoding.Default;
_serialPort.WriteTimeout = 1000;
_serialPort.DataReceived += OnDataReceived;
_serialPort.Open();
}
The “/dev/ttsy1” value which is in place of where the “comPort” variable would go is part of some examples I found that suggest the address might be in the form of /dev/ttys*. After some searching, I discovered that a usb device would likely register as “/dev/tty.usbserial*”, but I’m wondering if there’s a more elegant way if detecting what this value is that I can leverage in Mono rather than instructing a user to go into Terminal and type “ls /dev/tty.*” and type it themselves?
I don’t have any personal experience with Mono on the Mac, but I think it’s possible to include and use Objective-C classes in your Mono projects. Assuming that’s the case, take a look at ORSSerialPort. ORSSerialPortManager’s
-availablePortsmethod will give you an array of all the serial ports available on the system. You can also sign up to receive notifications when ports are added/removed, and the library also makes it easy to open a port, configure it, and send and receive data.Without something like this, you have to use IOKit to discover ports on the system, and the POSIX APIs for opening, configuring, reading from, and writing to serial ports. IOKit and the POSIX serial port APIs are low(ish) level C APIs.