Skip to content
This repository was archived by the owner on Aug 9, 2022. It is now read-only.

Commit 271d04f

Browse files
committed
1 parent a8cd90f commit 271d04f

File tree

1 file changed

+61
-15
lines changed

1 file changed

+61
-15
lines changed

src/termios.c

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,34 +80,80 @@ struct termios_list *first_tl = NULL;
8080
/*----------------------------------------------------------
8181
serial_test
8282
83-
accept: filename to test
84-
perform:
83+
accept: filename to test
84+
perform:
8585
return: 1 on success 0 on failure
86-
exceptions:
86+
exceptions:
8787
win32api: CreateFile CloseHandle
8888
comments: if the file opens it should be ok.
8989
----------------------------------------------------------*/
9090
int serial_test( char * filename )
9191
{
92-
unsigned long *hcomm;
93-
int ret;
94-
hcomm = CreateFile( filename, GENERIC_READ |GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0 );
95-
if ( hcomm == INVALID_HANDLE_VALUE )
92+
int ret = 0;
93+
94+
// Getting the Windows Version
95+
OSVERSIONINFO osvi;
96+
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
97+
BOOL bGetVer = GetVersionEx(&osvi);
98+
99+
// Using the QueryDosDevice API (on NT)
100+
if (bGetVer && (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT))
96101
{
97-
if (GetLastError() == ERROR_ACCESS_DENIED)
98-
{
99-
ret = 1;
100-
}
101-
else
102+
// This solution is based on http://www.codeproject.com/KB/system/enumports.aspx
103+
TCHAR szDevices[65535];
104+
DWORD dwChars = QueryDosDevice(NULL, szDevices, 65535);
105+
106+
if (dwChars)
102107
{
103-
ret = 0;
108+
int i=0;
109+
110+
for (;;)
111+
{
112+
//Get the current device name
113+
char* pszCurrentDevice = &szDevices[i];
114+
115+
if (strlen(pszCurrentDevice) > 3 && strcmp(pszCurrentDevice,filename)==0)
116+
{
117+
ret = 1;
118+
break;
119+
}
120+
121+
// Go to next NULL character
122+
while(szDevices[i] != '\0')
123+
i++;
124+
125+
// Bump pointer to the next string
126+
i++;
127+
128+
// The list is double-NULL terminated, so if the character is
129+
// now NULL, we're at the end
130+
if (szDevices[i] == '\0')
131+
break;
132+
}
104133
}
105134
}
106135
else
107136
{
108-
ret = 1;
137+
// Buggy way to test if we can open the comport (on Win9x)
138+
unsigned long *hcomm;
139+
hcomm = CreateFile( filename, GENERIC_READ |GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0 );
140+
if ( hcomm == INVALID_HANDLE_VALUE )
141+
{
142+
if (GetLastError() == ERROR_ACCESS_DENIED)
143+
{
144+
ret = 1;
145+
}
146+
else
147+
{
148+
ret = 0;
149+
}
150+
}
151+
else
152+
{
153+
ret = 1;
154+
}
155+
CloseHandle( hcomm );
109156
}
110-
CloseHandle( hcomm );
111157
return(ret);
112158
}
113159

0 commit comments

Comments
 (0)