Published on

How to Get a Handle for Serial Ports `COM10` and Higher

Authors

We have software at work that remotely controls one of our products over serial communication, and for some reason it sometimes failed to communicate. This is a note from when I investigated that issue.

The cause was that the code had not been written to handle COM10 and above. To specify serial ports COM10 and higher, you need to write it like this.

char port[11];

memset(port, '\0', sizeof(port));

// Specifying COM10 or above results in INVALID_HANDLE_VALUE
// Up to COM9, the following form works
// sprintf(port, "COM%d", port_number);

// COM10 and above
sprintf(port, "\\\\.\\COM%d", port_number);

HANDLE handle = CreateFile(port, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

Once I specified the COM port this way, the connection worked without problems.

Reference:

[SDK32] How to Specify Serial Ports COM10 and Above