Article View: borland.public.cppbuilder.nativeapi
Article #518Re: Detecting virtual comports
From: Giuliano
Date: Fri, 25 Jan 2008 20:22
Date: Fri, 25 Jan 2008 20:22
50 lines
2493 bytes
2493 bytes
Il Fri, 25 Jan 2008 14:26:01 +0100, "Anton" <acvoosten@zeelandnet.nl> ha scritto:
[snip]
>With a few of those existing virtual ports which are not really active, it
>takes more then 10 seconds to open them and that's just the thing I want to
>avoid.
>Maybe there's a timeout you can set or something else?
[snip]
Hi Anton
ten seconds? Uhm... I never seen a such behaviour when opening a com port.
For example, AFAIK, if the USB of a virtual COM port is unplugged, the port simply
disappears. However, I use the following code to detect if a comport exists and if it's
available. The fn returns true if the com exists and set the 2nd parameter to true
if it's busy.
bool IsComPortAvailable( int PortNumber, bool& InUse )
{
AnsiString const ComName = AnsiString( "\\\\.\\COM" ) + PortNumber;
HANDLE const hComm =
CreateFile(
ComName.c_str(),
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
0
);
if ( hComm == INVALID_HANDLE_VALUE ) {
DWORD const dwError = GetLastError();
if ( dwError == 5 ) // Port exists, but cannot be opened
InUse = true;
else if ( dwError == 2 ) // Port does not exist
return false;
}
else {
CloseHandle( hComm );
InUse = false;
}
return true;
}
Regards
Giuliano
Technical Details
Message-ID:
<vegkp3hhb89k52eol1c49492gc46k3iedv@4ax.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!newsgate.cistron.nl!xs4all!news2.euro.net!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4799cc68$1@newsgroups.borland.com> <njkjp3d5feaailo8p568elmqhntg8br1ku@4ax.com> <4799e374$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,1645444b8ff69f0e X-Google-Attributes: gid10cc67,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit From: Giuliano <nonfracassatelepalline@non_fracassate_le_pall.ine> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: Detecting virtual comports Date: Fri, 25 Jan 2008 21:22:08 +0100 Reply-To: dont_brk_the_O_O@withspam.org Message-ID: <vegkp3hhb89k52eol1c49492gc46k3iedv@4ax.com> References: <4799cc68$1@newsgroups.borland.com> <njkjp3d5feaailo8p568elmqhntg8br1ku@4ax.com> <4799e374$1@newsgroups.borland.com> X-Newsreader: Forte Agent 2.0/32.652 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 81.72.236.131 X-Trace: newsgroups.borland.com 1201292527 81.72.236.131 (25 Jan 2008 12:22:07 -0700) Lines: 51 Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!newsgate.cistron.nl!xs4all!news2.euro.net!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail