Discussion:
Windows 10 CreateFile on COMx port do not returns
(too old to reply)
CortexA57
2018-11-26 13:44:13 UTC
Permalink
Hi! I've a code that list all serial ports available using the
CreateFile function.
The code is working on XP, Win7 systems and on many Win10 .
Only on some Win10 systems the application will hung up over non
existing port:
ie in a systema with COM1,COM2, COM3,COM4,COM10,COM11 ports (all USB
deviced except the COM1, COM2 ports), when the CreateFile will try to
open COM5, the application hung-up and never returns from CreateFile....
any idea?
Thanks


Code looks like this one:
int CUart::CreateComList(comList* cList, int size)
{
HANDLE hPort;
unsigned int i;
wchar_t buf[64];
wchar_t buff1[64];
unsigned int cnt;

for(int i=1; i<256 && cnt<size; i++)
{
swprintf(buf,64,L"COM%d",i);
swprintf(buff1,64,L"\\\\.\\COM%d",i);

//Try to open the port(FILE_SHARE_READ|FILE_SHARE_WRITE)
BOOL bSuccess = FALSE;

hPort = CreateFile(buff1,
GENERIC_READ|GENERIC_WRITE, 0,
NULL,
OPEN_EXISTING, 0, NULL
);

//// NEVER RETURNS IF PORT IS NON-EXISTENT ON WIN10 PRO 64bit ..
//// WHY???

if (hPort == INVALID_HANDLE_VALUE)
{
DWORD dwError = GetLastError();

//Check to see if the error was because some other app had the
//port open or a general failure
if (dwError == ERROR_ACCESS_DENIED ||
dwError == ERROR_GEN_FAILURE ||
dwError == ERROR_SHARING_VIOLATION ||
dwError == ERROR_SEM_TIMEOUT)
bSuccess = TRUE;
}
else
{
//The port was opened successfully
bSuccess = TRUE;
//Don't forget to close the port, since we are going to do
// nothing with it anyway
CloseHandle(hPort);
}
if (bSuccess)
{
/* Add item to combo box */
lstrcpyn(cList[cnt].comName,buff1,sizeof(cList[cnt].comName));
lstrcpyn(cList[cnt].comShowName,buf,sizeof(cList[cnt].comShowName));
cnt++;
}
}
return(cnt); // Number of com ports
}
Uwe Sieber
2018-11-27 08:14:26 UTC
Permalink
Instead of opening the port just query its kernel name
by means of QueryDosDevice. It cannot hang and furthermore
gives an idea what is behind the COM, e.g. \Device\VCP0 for
an FTDI USB to Serial device.

I've never seen CreateFile hanging when opening a COM port,
so please let us know if you find of what type this nasty
COM5 is...

Uwe
Post by CortexA57
Hi! I've a code that list all serial ports available using the
CreateFile function.
The code is working on XP, Win7 systems and on many Win10 .
Only on some Win10 systems the application will hung up over non
ie in a systema with COM1,COM2, COM3,COM4,COM10,COM11 ports (all USB
deviced except the COM1, COM2 ports), when the CreateFile will try to
open COM5, the application hung-up and never returns from CreateFile....
any idea?
Thanks
int CUart::CreateComList(comList* cList, int size)
{
HANDLE hPort;
unsigned int i;
wchar_t buf[64];
wchar_t buff1[64];
unsigned int cnt;
for(int i=1; i<256 && cnt<size; i++)
{
swprintf(buf,64,L"COM%d",i);
swprintf(buff1,64,L"\\\\.\\COM%d",i);
//Try to open the port(FILE_SHARE_READ|FILE_SHARE_WRITE)
BOOL bSuccess = FALSE;
hPort = CreateFile(buff1,
GENERIC_READ|GENERIC_WRITE, 0,
NULL,
OPEN_EXISTING, 0, NULL
);
//// NEVER RETURNS IF PORT IS NON-EXISTENT ON WIN10 PRO 64bit ..
//// WHY???
if (hPort == INVALID_HANDLE_VALUE)
{
DWORD dwError = GetLastError();
//Check to see if the error was because some other app had the
//port open or a general failure
if (dwError == ERROR_ACCESS_DENIED ||
dwError == ERROR_GEN_FAILURE ||
dwError == ERROR_SHARING_VIOLATION ||
dwError == ERROR_SEM_TIMEOUT)
bSuccess = TRUE;
}
else
{
//The port was opened successfully
bSuccess = TRUE;
//Don't forget to close the port, since we are going to do
// nothing with it anyway
CloseHandle(hPort);
}
if (bSuccess)
{
/* Add item to combo box */
lstrcpyn(cList[cnt].comName,buff1,sizeof(cList[cnt].comName));
lstrcpyn(cList[cnt].comShowName,buf,sizeof(cList[cnt].comShowName));
cnt++;
}
}
return(cnt); // Number of com ports
}
CortexA57
2018-12-20 13:22:22 UTC
Permalink
Post by Uwe Sieber
Instead of opening the port just query its kernel name
by means of QueryDosDevice. It cannot hang and furthermore
gives an idea what is behind the COM, e.g. \Device\VCP0 for
an FTDI USB to Serial device.
Same result: program hang-up an never returns.
Post by Uwe Sieber
I've never seen CreateFile hanging when opening a COM port,
so please let us know if you find of what type this nasty
COM5 is...
Solved :-) it was the USB COM driver of that device:
When connected, all Ok, but if I remove it from USB port, then
application hung up.
It seems that Windows call that device's driver also if the device has
been removed : maybe to try to power up the device in case it is in
suspended state(?) I really don't known way.
Anyway, I've removed that driver, buy another UB serial converted and
all works fine now....

thanks to all
A57

Loading...