user16541120
user16541120

Reputation:

Is there a way to query the devices connected to COM ports to determine if a specific device is connect in C++?

I want to determine if a specific device is connected to a COM port. I currently have the following code, which uses QueryDosDevice to determine MS-DOS device names

TCHAR lpTargetPath[5000];

for (int i = 0; i < 255; i++) // checking ports from COM0 to COM255
{
    std::string str = "COM" + std::to_string(i);
    DWORD test = QueryDosDevice(str.c_str(), lpTargetPath, 5000);

    // Test the return value and error if any
    // If successful, test = # of characters stored in lpTargetPath
    // If not
    if (test != 0) //QueryDosDevice returns zero if it didn't find an object
    {
        std::cout << str << ": " << lpTargetPath << std::endl;
    }

    if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER)
    {
        cout << "buffer error" << endl;
    }
}

However, this just returns general device names as follows:

COM3: \Device\Serial0
COM6: \Device\USBSER000

Is there some other method to determine more specific information about the device so I can verify if a particular device is connected? Would the best way just be to poll the COM ports in use for some sort of signature input that would identify the device, or can I actually access serial numbers or some unique identifier (note the display name is just "USB Serial Device(COM6)" so not much help there)

Upvotes: 1

Views: 1967

Answers (1)

The operating system only knows about the COM port, not what's connected to it.

If you know much about COM ports, you know they are quite simple - at least, the part that everyone actually uses is simple. There is transmit, and receive, and that is it. The computer is able to blindly send data through one wire, and the device is able to blindly send data to the computer through a different wire, and we just hope and pray that it all works out.

There isn't even a way for the computer to automatically know how quickly to send data. If it gets it wrong then it sends or receives gibberish.

Other ports like VGA have extra wires which allow the computer to discover which kind of screen is plugged in. COM ports do not have this. USB has a kind of data message the computer can send to ask the device what it is. COM ports don't have this either.

For this reason, the program that's using the COM port has to already know what it's talking to - for example, the user might select it from a menu.


If your computer doesn't have a COM port you might buy an adapter that plugs into your USB port. Then you can plug the adapter's COM port into something that needs a COM port. Because the adapter plugs into a USB port, the computer is able to know about the adapter and the port on the adapter, but still not what the port is plugged into.

Some USB devices have these adapters built in. Sometimes they are real adapters - the device actually has a USB/COM adapter chip that talks to the COM part of the device. Other times they are virtual - the device is just pretending to be a USB/COM adapter chip talking to a COM device. If your device is one of these virtual adapters, you might be able to find some information about the device by querying the adapter/port, and it might show up as e.g. "Device You Are Looking For Virtual COM Port". If the device is using a real adapter chip, though, it will probably just say e.g. "USB COM Port Chip", since the chip doesn't know what device it's a part of, and you're still out of luck.

Upvotes: 2

Related Questions