0

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

Luke Quinane
  • 16,447
  • 13
  • 69
  • 88

2 Answers2

2

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
  • 34,724
  • 14
  • 83
  • 123
1

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
  • 1,869
  • 15
  • 24
  • 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 Sep 02 '09 at 06:47
  • 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 Sep 02 '09 at 10:12