Menu

#10 COM-ports names problem!

open
nobody
Other (1)
5
2009-02-27
2009-02-27
Mehanik
No

As we know the GlobalSat BT-355 Bluetooth datalogger has the same data exchange protocol as DG-100. DGManager.NET can successfully download data from BT-335.
But sometimes serial ports names looks strange: COM20c, COM25i or something else, and it is impossible to use such a COM-port. It is a strange result of SerialPort.GetPortNames() work for virtual serial ports.

To solve this problem suggest to update "SetupPortMenu" method as below (use regular expressions):

private void SetupPortMenu(bool iniPortExists)
{
portToolStripMenuItem.DropDownItems.Clear();

ToolStripMenuItem portMenuItem = new ToolStripMenuItem("Refresh");
portMenuItem.Click += new EventHandler(refreshPortsItem_Click);
portToolStripMenuItem.DropDownItems.Add(portMenuItem);

List<string> portNames = new List<string>(SerialPort.GetPortNames());

string pattern = @"COM\d+";
Regex reg = new Regex(pattern, RegexOptions.IgnoreCase);

foreach (string portName in portNames)
{

Match match = reg.Match(portName);
string name = match.ToString();

portMenuItem = new ToolStripMenuItem(name);

if (portName == Settings.Port || (!iniPortExists && portName == Port.PortName))
{
portMenuItem.Checked = true;
}

portMenuItem.Tag = "Radio";

portMenuItem.Click += new EventHandler(portMenuItem_Click);

portToolStripMenuItem.DropDownItems.Add(portMenuItem);
}
}

Discussion


Log in to post a comment.