0

For a project i have to communicate with a netduino, So i use serial communication to communicate with the netduino.

But here's my problem I cannot find my Usb portname, i use this small piece of code to find the port names.

 private void GetPortNames()
        {
            string[] ports = SerialPort.GetPortNames();
            ComportListbox.DataSource = ports;
        }

It doesnt show the usb port names. What am i doeing wrong, or how can i fix this issue.

EDIT

Question edited:

Can i see the usbportname from my usbport where the NETduino is attached to. So i hope to see COM10 for example. I looked in the system managment and saw that the usb is called Port_#0001.Hub_#0001. How can i open this port.

The_Monster
  • 494
  • 2
  • 7
  • 28
  • What serial communication has to do with usb portname? What do you expect to see in the list of com-ports? – Sinatr May 07 '14 at 09:55
  • Sinatr, Like i said i'm communicating with a netduino witch is connected to a usb port. I want to know the port name of the usb port. SerialPort.GetPortNames(); looked like the right way to go. – The_Monster May 07 '14 at 10:00
  • I might misunderstood you. Does listbox shows `com1`, `com2`, etc. or is it **blank**? Do you want to see `com5` and it is not there or what do you want to see? – Sinatr May 07 '14 at 10:04
  • The listbox is blank, i checked the array but that is empty also – The_Monster May 07 '14 at 10:06
  • Still it is unclear what do you *expect* to see. [`GetPortNames`](http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.getportnames.aspx) will return a list of *serial* ports, while you saying *usb*. If it's a usb-to-serial converter of some sort, then you get something like `com5` from it and this function should return it. If you don't see it, then perhaps your hardware is not working/powered/connected, etc. – Sinatr May 07 '14 at 10:13
  • If your array is empty, nothing else will work :( – Derek May 07 '14 at 10:14
  • I have edited the question so it will be easier to understand(i hope) – The_Monster May 07 '14 at 10:22
  • You should add the .Items.Add code to your example so people won't think that it is the DataSource part that is not working. – Derek May 07 '14 at 11:05
  • Serial ports are very primitive devices, they don't support plug & play. So there is no way to find out what's connected to a specific COM port. You can usually dig out more info about the ports with System.Management and the Win32_SerialPort class. You'll get a vendor name. Download [WMI Code Creator](http://www.microsoft.com/en-us/download/details.aspx?id=8572) to play with queries, it also auto-generates the code you need. – Hans Passant May 07 '14 at 11:53

1 Answers1

1

If ComportListbox has an "add" method, why not just use that with a for loop.

foreach ( string portName in ports )
{
   ComportListbox.Items.Add( portName );
}

If not, let me know and I will delete this answer.

Otherwise you may have to use a BindingList<string>. see: Binding List<T> to DataGridView in WinForm

Or you might even have to create an object that contains a string property for the binding name.

Community
  • 1
  • 1
Derek
  • 7,615
  • 5
  • 33
  • 58
  • Thanks for the comment, Comportlistbox doesnt have a Add function. But maybe i can make a array and at the portnames in there and later insert define the array as a Datasource for the ListBox – The_Monster May 07 '14 at 10:04
  • You might be able to do ComportListbox.Items.Add( portName ); It is hard to know, couldn't find api for Comportlistbox. – Derek May 07 '14 at 10:12
  • I don't know if BindingList would work, or if you would have to make a new class "MyPort" with a property "PortName" and make a BindingList. – Derek May 07 '14 at 10:13
  • The ComportListbox.items.add does work but the problem is that the ports string is empty – The_Monster May 07 '14 at 10:14