I seem to be getting some form of problem with using the SerialPorts.GetPortNames() function.
I want to get a list of the available ports and print them out on the console. I am aware that my ports are there because I’m testing them on Arduino so I know what kind of output I should receive.
I current have;
foreach(string str in SerialPort.GetPortNames())
{
Debug.Log(String.Format(str));
}
I have written an alternative for OS X users who wish to use SerialPorts.GetPortNames(), instead of calling this function, OS X users can simply use the method script.
void 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/tty.*"))
serial_ports.Add (dev);
Debug.Log (String.Format (dev));
}
}
}
In the GetPortNames function, it looks for ports that begin with “/dev/ttyS” or “/dev/ttyUSB” . However, OS X ports begin with “/dev/tty.”.
The Debug.Log will print out each of the available ports for you.