DJ Sures Homepage Contact Me Follow us on Twitter Follow me on Facebook
Share/Bookmark
Back

2010-11-29 Calgary Getportnames Is Broken

This is a fustrating problem that I stumbled on today. I have been using System.IO.Ports.SerialPort.GetPortNames() for years, until today. I can't seem to find why it's happening, but I have screenshots below.

The registry shows the correct comport names. The code is very simple. But the port shows a COM70 for some strange reason. Other times, it shows COM7i and sometimes with weird characters.

After viewing the registry, I wanted to extract the port names from it. So I looped through the registry and still ended up with the same results... COM70 *frown*

It appears someone else has had this problem also here.

This MSDN forum acknowledgees the issue and a Microsoft representative replied, but obvoiusly it was not repaired.

Well, so anyway, this ended up being my fix...


public List GetAvailableCommunicationPorts() {

List ports = new List();

foreach (string val in SerialPort.GetPortNames()) {

string portName = string.Empty;

bool wasNumber = false;
foreach (char c in val.ToCharArray())
if (c >= '0' && c <= '9') {

portName += c;
wasNumber = true;
} else if (wasNumber)
break;
else
portName += c;

ports.Add(portName);
}

ports.Sort();

return ports;
}







Copyright © 1995-2012 DJ Sures

hacker emblem