tags:

views:

67

answers:

2

hi evry one, i write a code(vs 2003,ver 1.1) for listing all the com ports available in a system.Now i want to know which of these com ports are connected to a device,and which one is available.

i wrote a dll in vc++ and done it. thnx

+1  A: 

Try opening each port. If you can't open it (or an exception is thrown) the port is not available. If you can open it, it is available.

X-Cubed
While this is true, the fact that a port is available or not bears no correlation to whether a device (listening or not) is actually connected to that port.
Coxy
True, but assuming the OP knows which device they are looking for, it's easy enough to send an init string to each port that is available and wait for a valid response.
X-Cubed
+1  A: 

This is going to be a tough one.

Serial communications are just send/receive wires, they aren't negotiated connections like TCP.

You can open a connection (using System.IO.Ports) to a port that has no connected device.

You can also open a connection and write to it all day using different baud rates, etc., and you may never get a response back, even if there is a connected device.

All that said, you can get a list of serial ports (not including USB ports) on the machine using the code below, but it will tell you nothing about whether or not there is a device connected to them:

foreach (string s in SerialPort.GetPortNames()) {
    Console.WriteLine("{0}", s);
    }
richardtallent
The question refers to VS2k3, which does not have SerialPort.
AMissico
The class SerialPort was introduced in .NET 2.0 and therefore does not exist in .NET 1.1
Scoregraphic
karthik already stated they have code for listing ports.
AMissico