Results 1 to 4 of 4

Thread: [RESOLVED] Serial port exists?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2007
    Posts
    751

    Resolved [RESOLVED] Serial port exists?

    I'd like to check to see if a serial port even exists. I know that if it doesn't, there is an error thrown when you do a port.open

    So, I would imagine that doing a simple IF port.isopen won't work?

    I looked at all the methods on the class and don't see anything obvious on how to do it.

    I just want to see if the hardware exists on the PC and set a boolean variable if it does when the form loads.

    Thanks for the help.

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Serial port exists?

    Try something like this:
    Code:
    Dim availableSerialPorts As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Computer.Ports.SerialPortNames
            If availableSerialPorts.Contains("COM1") Then
                MessageBox.Show("COM1 is available")
            Else
                MessageBox.Show("No COM1 on this computer")
            End If
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3
    Fanatic Member
    Join Date
    Sep 2009
    Location
    Lakewood, Colorado
    Posts
    621

    Re: Serial port exists?

    The normal solution is the use the GetPortNames() method of the SerialPort object. This method returns a String array of installed serial ports. You then can use any of the entries in this array as the argument for the PortName method. Naturally, you still will get an error if you attempt to open a port that has been opened elsewhere -- and is still open. So, you should use Try/Catch when ever you call the Open() method.

    Dick
    Richard Grier, Consultant, Hard & Software
    Microsoft MVP (Visual Basic)

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2007
    Posts
    751

    Re: Serial port exists?

    Thanks guys.

    I wouldn't ask these questions without spending time searching first and couldn't find a thing.

Posting Permissions

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



Click Here to Expand Forum to Full Width