Skip to content

Commit

Permalink
Support for serial ports greater-than COM9
Browse files Browse the repository at this point in the history
  • Loading branch information
jaka committed Jun 26, 2019
1 parent 3a9b0dc commit 0f7ac91
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
39 changes: 35 additions & 4 deletions include/windows.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
#include "usbtemp.h"
#include <stdio.h>

static int ut_errno;

static int get_new_serial_port(char *buf, const unsigned int buflen, const char *s)
{
const char *fmt_x = "COM%u", *fmt_xx = "\\\\.\\COM%u";
const char *fmt, *p;
unsigned int num = 0;

for (p = s; !isdigit(*p); p++);

while (isdigit(*p)) {
num *= 10;
num += *p - '0';
p++;
}

fmt = num > 9 ? fmt_xx : fmt_x;
if (snprintf(buf, buflen, fmt, num) < 0) {
return -1;
}

return 0;
}

static int owReset(HANDLE fd)
{
int rv;
Expand Down Expand Up @@ -101,11 +124,19 @@ HANDLE owOpen(const char *serial_port)
HANDLE fd;
DCB dcb = { 0 };
COMMTIMEOUTS timeouts = { 0 };
char new_serial_port[16];

fd = CreateFile(serial_port, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
fd = CreateFile(serial_port, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (fd == INVALID_HANDLE_VALUE) {
ut_errno = 3;
return fd;
if (get_new_serial_port(new_serial_port, sizeof(new_serial_port) - 1, serial_port) < 0) {
ut_errno = 3;
return fd;
}
fd = CreateFile(new_serial_port, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (fd == INVALID_HANDLE_VALUE) {
ut_errno = 3;
return fd;
}
}

SecureZeroMemory(&dcb, sizeof(DCB));
Expand Down Expand Up @@ -146,4 +177,4 @@ HANDLE owOpen(const char *serial_port)
void owClose(HANDLE fd)
{
CloseHandle(fd);
}
}
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ int main(int argc, char **argv)
}

if (verbose) {
printf("USB Thermometer CLI v1.041 Copyright 2019 usbtemp.com Licensed under MIT licence.\n");
printf("USB Thermometer CLI v1.05 Copyright 2019 usbtemp.com Licensed under MIT licence.\n");
}

if (action == HELP) {
Expand Down

0 comments on commit 0f7ac91

Please sign in to comment.