From: Martin Ling Date: Mon, 4 Nov 2013 13:08:09 +0000 (+0000) Subject: On Windows, prefix port names with '\\.\' to work with ports above COM9. X-Git-Tag: libserialport-0.1.0~138 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=99945a1fb53bf4b4d11e7bb54e097c57ea2a230e;p=libserialport.git On Windows, prefix port names with '\\.\' to work with ports above COM9. --- diff --git a/README b/README index 6e3b480..fefbef2 100644 --- a/README +++ b/README @@ -131,8 +131,8 @@ int sp_open(struct sp_port *port, int flags); flags: Flags to use when opening the serial port. Possible flags are: SP_MODE_RDWR, SP_MODE_RDONLY, and SP_MODE_NONBLOCK. - Returns: SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG - if an invalid port is passed. + Returns: SP_OK on success, SP_ERR_FAIL on failure, SP_ERR_MEM on allocation + failure, or SP_ERR_ARG if an invalid port is passed. int sp_close(struct sp_port *port); diff --git a/serialport.c b/serialport.c index ddddb51..c891006 100644 --- a/serialport.c +++ b/serialport.c @@ -343,6 +343,13 @@ int sp_open(struct sp_port *port, int flags) #ifdef _WIN32 DWORD desired_access = 0, flags_and_attributes = 0; + char *escaped_port_name; + + /* Prefix port name with '\\.\' to work with ports above COM9. */ + if (!(escaped_port_name = malloc(strlen(port->name + 5)))) + return SP_ERR_MEM; + sprintf(escaped_port_name, "\\\\.\\%s", port->name); + /* Map 'flags' to the OS-specific settings. */ desired_access |= GENERIC_READ; flags_and_attributes = FILE_ATTRIBUTE_NORMAL; @@ -351,8 +358,11 @@ int sp_open(struct sp_port *port, int flags) if (flags & SP_MODE_NONBLOCK) flags_and_attributes |= FILE_FLAG_OVERLAPPED; - port->hdl = CreateFile(port->name, desired_access, 0, 0, + port->hdl = CreateFile(escaped_port_name, desired_access, 0, 0, OPEN_EXISTING, flags_and_attributes, 0); + + free(escaped_port_name); + if (port->hdl == INVALID_HANDLE_VALUE) return SP_ERR_FAIL; #else