detect serial ports

nelson.enlis

Member
Joined
Feb 16, 2009
Messages
8
Programming Experience
Beginner
Hi,

I have a little code for get the serial port available in the system.

VB.NET:
        For Each pn As String In IO.Ports.SerialPort.GetPortNames
            Debug.WriteLine("NAME : " + pn.ToString)
        Next
So, there have the result:

NAME : COM4
NAME : COM1
NAME : COM5

So, The PORT1 and PORT5 its ok (because this are real serial port, with a real RS232 interface), but PORT4, I don't know.

How I can do for get only true serial ports ?

Any support its well received

Thanks
 
Hi,

First, thank for try help.

The port 1 , it's a physical RS232 port embeded on my laptop. the COM5 it's a PCMCIA converter to RS232.

How you explain, in Device Manager appear only COM1 and COM5. I think (like you) that COM4 is a USB serial port (like a mouse).

So, my question is, how get only and exclusively the real SERIAL PORTS ?

Thanks
 
I can't really help you on that since I don't have such a 'shadow' port.

But some information on the background:
I followed My.Computer.Ports.SerialPortNames() (using the Reflector) down to System.IO.Ports.SerialPort.getPortNames() and it turned out that it reads the avalaible information from the registry, in this case all keys from HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM . Turns out I can add my own SerialPorts in there which get recognized by getPortNames() and also accepted by the contructor of SerialPort().

However, trying to opening this port returns an IOException which tells me that the port does not exist (which is indeed true). It's coming from somewhere within the SerialStream Constructor, which has a lot of calls down to UnsafeNativeMethods (calls into kernel32.dll) and catches this exceptions.
Seems to me like you have two ways of checking if the port really does exist, try to open it (and listen to Exceptions which occures) or have a look at the Windows API.

Bobby
 
Back
Top