|
-
Feb 9th, 2010, 11:24 AM
#1
Thread Starter
Fanatic Member
[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.
-
Feb 9th, 2010, 11:50 AM
#2
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 -
-
Feb 9th, 2010, 12:06 PM
#3
Fanatic Member
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)
-
Feb 9th, 2010, 12:28 PM
#4
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|