views:

192

answers:

2

To get all avaliable Serialports from the system i use the following command.

SerialPort.GetPortNames

It works fine for the mainboard serial port, but with the bluetooth device i get the wrong portnames.

For Example: Instead of COM7 i get sometimes COM70 or COM7ö. Its always 1 letter to much.

any suggestens?

PS: I am using newest Visual Studio Express in Windows 7 PPS: The dirty hack to cut the last letter didn't work because i don't know which one is the bluetooth serial port (with various bluetoothstick or devices it changes the number of the comport) and after trying various sticks i reached COM10, ergo COM100 or COM10f

EDIT: the code i am using right now. reading the regestry, but still the same problem.

RegistryKey myRegistry = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm");
foreach (string valuename in myRegistry.GetValueNames())
{
    if (myRegistry.GetValue(valuename) is String)
    {
        if (valuename.Contains("BthModem"))
        {
            richTextBox1.AppendText(">" + myRegistry.GetValue(valuename) + "<" + Environment.NewLine);
        }
    }
} 

Normally the second or third request is working with a result like

COM11ᯋ< COM10S< COM11< COM10< COM11< COM10<

how can that be?

A: 

You can utilize WMI to query the system for serial ports, including those that are added by bluetooth devices and USB-To-Serial devices. Maybe that way you won't encounter this issue. See at CodeProject.

M.A. Hanin
didn't work on my devenvironment. System.Managment could not be found. Maybe it's not part of express edition ??
wurlog
A: 

This has been reported as a bug with non-null terminated strings:

Can you manually walk the registry?

HKLM\Hardware\DeviceMap\SerialComm
Chris Haas
I get the values from the registry, but even there is only on Comport connected via bluetooth the registry is showing 2 Comports. Do you have any idea why thats happening?
wurlog
Still same problem :( but normally the 3rd try is the right one. how could this be?
wurlog