// // coms -- Find serial (COM) ports and notify when they come and go. // // - This tool is especially useful with USB virtual serial ports. // - Run once and terminate with ctrl^c or let it run indefinitely. // // 06-Oct-2010 tvb www.LeapSecond.com/tools // #include #include #include #include #define WIN32_LEAN_AND_MEAN #include char buf[32*1024]; #define MAX_COM 256 void main() { int n, size, port; char now[MAX_COM], then[MAX_COM], *p; memset(then, 0, MAX_COM); while (1) { // Get list of all device names. n = QueryDosDeviceA(NULL, buf, sizeof buf); if (n == 0) { fprintf(stderr, "QueryDosDevice failed\n"); exit(1); } // Find defined COM ports in the list. memset(now, 0, MAX_COM); for (p = buf; n > 0; p += size, n -= size) { if (strncmp(p, "COM", 3) == 0) { port = atoi(&p[3]); if (port > 0 && port < MAX_COM) { now[port] = 1; } } size = strlen(p) + 1; } // Look for changes in COM port numbers. if (memcmp(then, now, MAX_COM) != 0) { for (port = 0; port < MAX_COM; port += 1) { if (then[port] - now[port]) { printf("%s", now[port] ? "+" : "-"); } if (then[port] + now[port]) { printf("COM%d ", port); } } printf("\n"); memcpy(then, now, MAX_COM); // cp now then } // Wait a second and check again for changes. _sleep(1000); } }