From 4fd295b4acdfed77be0660a29b3a00cb8b8a7259 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Sat, 1 Jan 2011 22:27:02 +0100 Subject: [PATCH] Always prefix the port name with "\\.\" before opening the port. The "\\.\" prefix allows to access the Win32 device namespace directly, without going through the file system. This is required to support non-standard port names, and COMx ports with a number greater than 9. --- src/serial_win32.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/serial_win32.c b/src/serial_win32.c index bddd9d8..9ec432d 100644 --- a/src/serial_win32.c +++ b/src/serial_win32.c @@ -91,6 +91,19 @@ serial_open (serial_t **out, const char* name) if (out == NULL) return -1; // ERROR_INVALID_PARAMETER (The parameter is incorrect) + // Build the device name. + const char *devname = NULL; + char buffer[MAX_PATH] = "\\\\.\\"; + if (name && strncmp (name, buffer, 4) != 0) { + size_t length = strlen (name) + 1; + if (length + 4 > sizeof (buffer)) + return -1; + memcpy (buffer + 4, name, length); + devname = buffer; + } else { + devname = name; + } + // Allocate memory. serial_t *device = (serial_t *) malloc (sizeof (serial_t)); if (device == NULL) { @@ -99,7 +112,7 @@ serial_open (serial_t **out, const char* name) } // Open the device. - device->hFile = CreateFileA (name, + device->hFile = CreateFileA (devname, GENERIC_READ | GENERIC_WRITE, 0, NULL, // No security attributes. OPEN_EXISTING,