1

I have an usb device that is plugged in as a serial port on com3. I use this to open port:

string[] ports = SerialPort.GetPortNames();
foreach (string portName in ports)
{
     try
     {
          var port = new SerialPort(portName, 256000);
          port.Open();
     }
     catch(Exception ex)
     {
          Console.WriteLine(ex.Message);
     }
}

I get an io exception here "port com3 does not exist". I use VS2012 + Win7x64Pro. I tried to reboot windows and everything worked fine, but just 1 time. Some days ago I run this project on VS2012 + Win8 and everything was great. It worked great, no exceptions were found. I found great amount of similar questions but there are not any solution for this problem. For more information: as a usb device I use BlueGiga BLED112 with driver version "ble-1.1.1-71". Explain me please where and what am I doing wrong.

Vasilii Ruzov
  • 554
  • 1
  • 10
  • 27

1 Answers1

1

Bluetooth often creates phantom virtual serial ports that can't actually be opened. In an even worse scenario, it is paired, and the driver spends a minute looking for a not-present Bluetooth accessory before failing to open.

Other serial devices may not respond well to being opened for no reason (opening does change voltages on several serial pins).

Solution: Never open all serial ports returned by GetPortNames in a loop. Instead open just the one your device is attached to. (You can know this by checking device descriptor strings, or in the worst case, asking the user which of the listed ports is the right one)

Perhaps you're just trying to use the Bluetooth radio? In that case you're using the wrong approach. The serial port associated with Bluetooth is for use when you pair with a device that identifies itself as the SPP (Serial Port Profile). Not for manipulating the radio. For that you use the Sockets API. See Bluetooth Programming with Windows Sockets. From C#, you'll want to use one of the third-party libraries that wraps this stuff.

Community
  • 1
  • 1
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • but I get an error actually when I'm trying to connect to the port that I need to connect, com3. and GetPortNames returns me just 1 string "com3". I'm trying to use bluetooth low energy devices via this dongle(bled112). and it works fine 1 time after reboot. but then happens smth mysterious. i have a driver from bluegiga that connects this dongle as a serial port – Vasilii Ruzov May 08 '13 at 20:25
  • The serial port device will not be your USB Bluetooth radio. Do you have an SPP Bluetooth device? – Ben Voigt May 08 '13 at 20:25
  • what do you actually mean? on one hand I have BLED112 and on the other hand I have a heart rate monitor that supports Bluetooth smart – Vasilii Ruzov May 08 '13 at 20:31
  • It's possible that your heart rate monitor is SPP. Have you paired it? – Ben Voigt May 08 '13 at 23:24
  • No, I didn't. but I didn't do it when I used win8 and there everything was ok. and how to pair dongle with heart rate monitor? – Vasilii Ruzov May 09 '13 at 08:45