🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: rs232 under XP

#10620
From: Benjamin Pratt
Date: Thu, 01 Apr 2004 02:06
41 lines
2633 bytes
I have a clearer picture of the problem now. Access to the HANDLE is
protected by a semiphore, so if one function (like WaitCommEvent) blocks
while using it, other functions that are passed the HANDLE will also block,
just as you have experienced. You have several options, however.

1. Open two handles to the same port. Open one handle with GENERIC_WRITE and
FILE_SHARE_READ. Open the second with GENERIC_READ. Use the GENERIC_READ
handle for reading and the GENERIC_WRITE handle for writing. If one blocks,
the other is still accessable.

2. Forget using WaitCommEvent. Instead, use ReadFile with the timeout
interval set low or zero. Example:

std::string GetStringFromPort()
{
    COMMTIMEOUTS cOld, cNew;
    GetCommTimeouts(hPort, &cOld);
    cNew = cOld;
    cNew.ReadIntervalTimeout = MAXDWORD;
    cNew.ReadTotalTimeoutConstant = 0;
    cNew.ReadTotalTimeoutMultiplier = 0;
    SetCommTimeouts(hPort, &cNew);
    std::string chars;
    DWORD read;
    do {
        char ch;
        ReadFile(hPort, &ch, 1, &read, NULL);
        if(ch == '\0')
            read = 0;
        else
            chars.push_back(ch);
    } while(read > 0);
    SetCommTimeouts(hPort, &cOld);
    return chars;
}

This function reads characters from the buffer and stores them in a
std::string. It is done when it encounters a Nul or there are no more chars
in the buffer. Because the timeouts are set to zero, ReadFile will never
block. However, you should probably use a short timeout interval so that
characters that have not reached the input buffer yet will be read.
Technical Details
Message-ID: <406b782e$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!news.glorb.com!news.moat.net!newsfeed.mesh.ad.jp!newsfeed.media.kyoto-u.ac.jp!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <40693a3f@newsgroups.borland.com> <4069a26a@newsgroups.borland.com> <4069c1f9$1@newsgroups.borland.com> <406a964f$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,8913094e963c267d
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2004-03-31 18:06:24 PST
From: "Benjamin Pratt" <benjamin.pratt.getridofthis@verizon.net>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <40693a3f@newsgroups.borland.com> <4069a26a@newsgroups.borland.com> <4069c1f9$1@newsgroups.borland.com> <406a964f$1@newsgroups.borland.com>
Subject: Re: rs232 under XP
Date: Wed, 31 Mar 2004 18:06:33 -0800
Lines: 43
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
NNTP-Posting-Host: 24.130.93.201
Message-ID: <406b782e$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1080784942 24.130.93.201 (31 Mar 2004 18:02:22 -0800)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!news.glorb.com!news.moat.net!newsfeed.mesh.ad.jp!newsfeed.media.kyoto-u.ac.jp!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail