diff --git a/bacnet-stack/demo/mstpcap/main.c b/bacnet-stack/demo/mstpcap/main.c index 080f78f5..235be5aa 100644 --- a/bacnet-stack/demo/mstpcap/main.c +++ b/bacnet-stack/demo/mstpcap/main.c @@ -845,6 +845,24 @@ static BOOL WINAPI CtrlCHandler( return TRUE; } + +/************************************************************************* +* Description: print available COM ports +* Returns: none +* Notes: none +**************************************************************************/ +static void print_com_ports(void) +{ + unsigned i = 0; + + printf("List of available COM ports:\r\n"); + /* try to open all 255 COM ports */ + for (i=1; i<256; i++) { + if (RS485_Interface_Valid(i)) { + printf("COM%u\r\n", i); + } + } +} #else static void sig_int( int signo) @@ -945,6 +963,11 @@ int main( } if (argc > 1) { RS485_Set_Interface(argv[1]); + } else { + #if defined(_WIN32) + print_com_ports(); + return 0; + #endif } if (argc > 2) { my_baud = strtol(argv[2], NULL, 0); diff --git a/bacnet-stack/ports/win32/rs485.c b/bacnet-stack/ports/win32/rs485.c index 8216f597..105fe788 100644 --- a/bacnet-stack/ports/win32/rs485.c +++ b/bacnet-stack/ports/win32/rs485.c @@ -125,6 +125,41 @@ void RS485_Set_Interface( } } +/**************************************************************************** +* DESCRIPTION: Check the serial port to see if port exists +* RETURN: true if port exists +* ALGORITHM: none +* NOTES: none +*****************************************************************************/ +bool RS485_Interface_Valid(unsigned port_number) +{ + HANDLE h = 0; + DWORD err = 0; + bool status = false; + char ifname[255] = ""; + + sprintf(ifname, "\\\\.\\COM%u", port_number); + h = CreateFile( + ifname, + GENERIC_READ | GENERIC_WRITE, 0, + NULL, OPEN_EXISTING, 0, + NULL); + if (h == INVALID_HANDLE_VALUE) { + err = GetLastError(); + if ((err == ERROR_ACCESS_DENIED) || + (err == ERROR_GEN_FAILURE) || + (err == ERROR_SHARING_VIOLATION) || + (err == ERROR_SEM_TIMEOUT)) { + status = true; + } + } else { + status = true; + CloseHandle(h); + } + + return status; +} + const char *RS485_Interface( void) { diff --git a/bacnet-stack/ports/win32/rs485.h b/bacnet-stack/ports/win32/rs485.h index c4475f8d..820bba18 100644 --- a/bacnet-stack/ports/win32/rs485.h +++ b/bacnet-stack/ports/win32/rs485.h @@ -68,6 +68,9 @@ extern "C" { void RS485_Print_Error( void); + bool RS485_Interface_Valid(unsigned port_number); + + #ifdef __cplusplus } #endif /* __cplusplus */