- Home /
SerialPorts.GetPortNames error
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));
}
But there is no output, anybody know why?
Edit: I'm using OS X if that helps
Answer by ryanas · Feb 18, 2014 at 06:45 PM
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.
Thank you to Eroteme for showing me Mono SerialPort.cs.
Sweet! This would be a good thing to add to the $$anonymous$$ono source code.
Fyi, had to tweak the above code to get it to work for me (spectacular tool, thank you) because thinks moved under System and IO, etc:
void getPortNames ()
{
int p = (int)System.Environment.OSVersion.Platform;
List<string> serial_ports = new List<string> ();
// Are we on Unix?
if (p == 4 || p == 128 || p == 6) {
string[] ttys = System.IO.Directory.GetFiles ("/dev/", "tty.*");
foreach (string dev in ttys) {
if (dev.StartsWith ("/dev/tty.*"))
serial_ports.Add (dev);
Debug.Log (System.String.Format (dev));
}
}
}
Answer by Eroteme · Feb 18, 2014 at 05:24 PM
The method is implemented in the Mono SerialPort.cs. It works as expected on my computer.
If you're on Windows, perhaps you should double-check if the registry key HKEY_LOCAL_MACHINE\\HARDWARE\\DEVICEMAP\\SERIALCOMM contain elements.
I'm currently using a $$anonymous$$ac and if I write
Debug.Log(SerialPorts.GetPortNames());
The output is simply "System.String[]"
Your answer