Modified MS/TP capture utility to emit the list of available COM ports on Windows when no command line argument is given.
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -68,6 +68,9 @@ extern "C" {
|
||||
void RS485_Print_Error(
|
||||
void);
|
||||
|
||||
bool RS485_Interface_Valid(unsigned port_number);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
||||
Reference in New Issue
Block a user