Results 1 to 4 of 4

Thread: List available com ports

  1. #1
    jon.Haigh is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    Oct 2011
    Posts
    2
    Reputation
    0

    List available com ports

    Hello

    I am very new to .net and I am trying to figure out the best way to get a list of all of the currently available com ports. I have tried some code I found on the forum and it isn't working.

    Dim ports As String() = SerialPort.GetPortNames()




    Dim port As String
    Dim x As SerialPort
    For Each port In ports
    x = port
    If x.IsOpen Then
    MessageBox.Show("hello")




    End If
    Next port

    it says that the port string cannot be saved to x

    Any ideas as to how I can correct this or do it better. Thanks!

  2. #2
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,090
    Reputation
    2804
    You know that each 'port' in 'ports' string array is the string name, is is not a SerialPort object.
    So how you can get a SerialPort object by its string name? Yes, you go to documentation and find that you can use one of the constructors: SerialPort Constructor (System.IO.Ports)
    How to format posts with code blocks etc - present the problem/post properly

    Visual Studio Community 2017

  3. #3
    jon.Haigh is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    Oct 2011
    Posts
    2
    Reputation
    0
    Thank you for the help. I corrected the code and it is working by showing me the com's of all my serial ports. Why doesn't it show USB ports though? shouldn't it? If not what else do I need to do? Thanks

  4. #4
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,090
    Reputation
    2804
    USB ports are not serial ports. For USB you probably need a third party library.
    How to format posts with code blocks etc - present the problem/post properly

    Visual Studio Community 2017

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •