🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #4829

GetOverlappedResult on virtual serial COM port

#4829
From: Thom A.
Date: Sun, 27 Nov 2005 19:07
85 lines
3525 bytes
I have a problem scanning COM ports for a certain device:

Actually, the task seems easy: Open each COM port, send an
identification request, and close the respective connection when either
an invalid response or no response at allis being received.

This works well with serial COM ports and virtual serial COM ports
created by USB<->RS232 adapters. Unfortunately, this is extremely slow
for virtual serial COM ports created by Bluetooth dongles!

The problem resides in the GetOverlappedResult(...) function which waits
almost three seconds, although I have set the Write Timeout to only 60 ms!

Do you know a way to optimize the following write function so that it
doesn't hang up for such a long time?`


int __fastcall ThreadComm::Write(char Buf[], DWORD Count)
{
    BOOL        fWriteStat ;
    DWORD       dwBytesWritten ;
    DWORD       dwErrorFlags;
    DWORD       dwError;
    DWORD       dwBytesSent=0;
    COMSTAT     ComStat;

    const MaxRetries = 9;
    int Tries = 0;

    if (!Enabled()) return -1;
    Lock();
    try
    {
      fWriteStat = WriteFile(FHandle, Buf, Count,
                             &dwBytesWritten, &WRITE_OS ) ;
      if (!fWriteStat)
      {
        if(GetLastError() == ERROR_IO_PENDING)
        {
          Tries = 0;
          while(!GetOverlappedResult(FHandle,&WRITE_OS, &dwBytesWritten,
TRUE ))
          {
             dwError = GetLastError();
             if(dwError == ERROR_IO_INCOMPLETE)
             {
                if (dwBytesWritten == 0) {
                  Tries++;
                  if (Tries == MaxRetries)
                    LogLine(ALog, "Low-level Write to port failed.
Giving up ("+IntToStr(MaxRetries)+")");
                } else {
                  if (Tries > 0)
                    LogLine(ALog, "Low-level Write to port failed
"+IntToStr(Tries)+" times");
                  Tries = 0;
                }
                dwBytesSent += dwBytesWritten;

                if (Tries == MaxRetries) {  // Error - Device in use,
e.g. by ActiveSync ??
                  Unlock();
                  return 0;
                }
                continue;
             }
             else
             {
                ClearCommError(FHandle, &dwErrorFlags, &ComStat ) ;
                break;
             }
          }
          dwBytesSent += dwBytesWritten;
        }
        else
        {
          ClearCommError(FHandle, &dwErrorFlags, &ComStat ) ;
        }
     }
     else dwBytesSent += dwBytesWritten;
   }
   catch(...) { Unlock(); }
   Unlock();
   return dwBytesSent ;
}
Technical Details
Message-ID: <438a0398$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,14a3bc55bdb22e73,start
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Sun, 27 Nov 2005 20:07:04 +0100
From: "Thom A." <thoma@gmx.at>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.5) Gecko/20041217
X-Accept-Language: en-us, en
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: GetOverlappedResult on virtual serial COM port
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 84.166.243.130
Message-ID: <438a0398$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1133118360 84.166.243.130 (27 Nov 2005 11:06:00 -0700)
Lines: 87
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail