🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Again Comm Port - Windows Version

#8829
From: McKool
Date: Tue, 21 Sep 2004 14:21
148 lines
5016 bytes
Know somebody how can a Port work in a same way indifferent of the Operative 
System? how can i set the right TimeOut values or a way to set it 
automatically (COMMTIMEOUTS struct)?

Thanks
"McKool" <kenethw@peak-system.com> schrieb im Newsbeitrag 
news:414fe69c$1@newsgroups.borland.com...
> Hello.... i made a Synchronous Comm Port and it works under XP OS where i 
> programmed it), but in W2K and 98 don't work.  I will write here the Read, 
> Write and create functions....
>
> CREATE FUNCTION
> //-----------------------------------------------------------------------
> bool __stdcall clsCommPort::CreatePort(
>    LPTSTR strComm
> )
>
> {
>    // Specific Port is Opened
>    //
> m_hPort = CreateFile (strComm, GENERIC_READ | GENERIC_WRITE,0,
>                          NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
>
>    // Catch error
>    //
> if (m_hPort == INVALID_HANDLE_VALUE){
>  wLastError = GetLastError();
>  return false;
> }
>
> COMMTIMEOUTS CommTimeouts;
>
>    // Time Outs of the are Configurated
>    //
> GetCommTimeouts(m_hPort, &CommTimeouts);
> CommTimeouts.ReadIntervalTimeout = MAXWORD;
> CommTimeouts.ReadTotalTimeoutMultiplier = 1;
> CommTimeouts.ReadTotalTimeoutConstant = 1;
> CommTimeouts.WriteTotalTimeoutMultiplier = 1;
> CommTimeouts.WriteTotalTimeoutConstant = 5;
>
>    if (!SetCommTimeouts(m_hPort, &CommTimeouts)){
>        // Catch error
>        //
>  wLastError = GetLastError();
>  return false;
> }
>
>    // Port is Configurated with the Espicified BaudRate
>    // and the Common and/or standart values
>    //
> PortDCB.DCBlength = sizeof (DCB);
>
> GetCommState(m_hPort, &PortDCB);
>
> PortDCB.BaudRate = wBaudRate;
> PortDCB.fBinary = true;
> PortDCB.fParity = false;
>
> PortDCB.fOutxCtsFlow = false;
> PortDCB.fOutxDsrFlow = false;
> PortDCB.fDtrControl = DTR_CONTROL_DISABLE;
>
> PortDCB.fDsrSensitivity = false;
> PortDCB.fTXContinueOnXoff = true;
> PortDCB.fOutX = false;
> PortDCB.fInX = false;
> PortDCB.fErrorChar = false;
> PortDCB.fNull = false;
> PortDCB.fRtsControl = RTS_CONTROL_DISABLE; //RTS_CONTROL_ENABLE
>    PortDCB.XoffLim = 100;
>    PortDCB.XonLim = 100;
>
> PortDCB.ByteSize = 8;
> PortDCB.fAbortOnError = true;
>    PortDCB.wReserved = 0;
>
> PortDCB.Parity = NOPARITY;
> PortDCB.StopBits = ONESTOPBIT;
>
>    // Set Dcb Configuration to the Port
>    //
> if (!SetCommState(m_hPort, &PortDCB)){
>        // Catch Error
>        //
>  wLastError = GetLastError();
>  return false;
> }
>
>    // Everything Ok
>    //
>    return true;
>
> -- 
> //-----------------------------------------------------------------------
>
> READ FUNCTION:
> //-----------------------------------------------------------------------
> DWORD __stdcall PortReadThread(
>    LPVOID lpvoid
> )
> {
> clsCommPort* pPort = (clsCommPort*) lpvoid;
> DWORD dwBytesTransferred;
> BYTE buffer[2048];
>
> while(pPort->GetHandlerPort() != INVALID_HANDLE_VALUE){
>        ReadFile(pPort->GetHandlerPort(), buffer, 1, 
> &dwBytesTransferred,NULL);
>        if(dwBytesTransferred == 0)
>            Sleep(2);
>        else{
>
>            for (i=0; i < (int)dwBytesTransferred; i++){
>                pPort->AddCharToQueue(buffer[i]);
>            }
>            dwBytesTransferred = 0;
>            pPort->Read();
>        }
> }
>
>    // Finish Thread
>    //
> return 0;
> //-----------------------------------------------------------------------
>
> WRITE FUNCTION
> //-----------------------------------------------------------------------
> bool __stdcall clsCommPort::Write(
>    BYTE Byte
> )
>   DWORD dwBWrote,iErr;
>    bool bTest;
>
>    bTest = WriteFile(m_hPort,&Byte,1,&dwBWrote,NULL);
>    if(!bTest){
>        if(dwBWrote == 0)
>            return false;
>        iErr = GetLastError ();
>        if(iErr != ERROR_IO_PENDING)
>            return false;
>    }
>    return true;
> //------------------------------------------------------------------------
> This work perfecly in Windows XP -SP 2.
>
> Keneth Wagner.
> 
Technical Details
Message-ID: <415037f6$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <414fe69c$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,6ff2f2a7e9dff124
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "McKool" <kenethw@peak-system.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <414fe69c$1@newsgroups.borland.com>
Subject: Re: Again Comm Port - Windows Version
Date: Tue, 21 Sep 2004 16:21:34 +0200
Lines: 150
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Response
NNTP-Posting-Host: 213.157.13.214
Message-ID: <415037f6$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1095776246 213.157.13.214 (21 Sep 2004 07:17:26 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail