1

I am doing a quick test for come com ports. I run the following code on my pc but it only adds COM1 to the combo box? My pc has 6 usb ports, so why is it only finding 1?

string[] ports = SerialPort.GetPortNames();

foreach (string port in ports)
{

    comboBox1.Items.Add(port);

 }
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Glen
  • 655
  • 3
  • 16
  • 31
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Feb 21 '13 at 14:26
  • looks like a duplicate of http://stackoverflow.com/questions/3331043/get-list-of-connected-usb-devices – Benny Feb 21 '13 at 14:27

1 Answers1

5

Even if you had a hundred of them, the SerialPort class is used to control serial port resources, and USB is not a serial port.

There are multiple libraries that let you program usb ports, but what exactly are you trying to achieve?

Check out LibUsbDotNet C# USB Library

http://sourceforge.net/projects/libusbdotnet/

If you simply want to enumerate available USB ports, you can do that by quering the WMI, see this answer for more details on how to do that: Get List of connected USB Devices

Community
  • 1
  • 1
animaonline
  • 3,715
  • 5
  • 30
  • 57
  • basically i have a machine that uses an old RS232 port converted to USB to connect to the pc. The machine sends data in serial. So the program im writing needs to check which usb port it is connected to. – Glen Feb 21 '13 at 14:34
  • the idea would be to loop over each usb port, and detect if the machine is connected to that port or not – Glen Feb 21 '13 at 14:37
  • if you want to communicate over usb , use the usb lib I wrote about. – animaonline Feb 21 '13 at 14:39
  • i downloaded and installed the exe...how on earth do i use this library? i followed the code example you left but Visual Studio doesnt even recognise the GetUSBDevices() function – Glen Feb 21 '13 at 14:43
  • I'm sorry, but then apparently you're lacking basic understanding of C#, why don't you improve your skills , then try again, don't expect anyone here to do your homework for you :) – animaonline Feb 21 '13 at 17:06