Article View: borland.public.cppbuilder.nativeapi
Article #6458Re: Response not in que?
From: Neil P.
Date: Mon, 09 May 2005 21:28
Date: Mon, 09 May 2005 21:28
144 lines
5472 bytes
5472 bytes
Here is a snippet of my code and exactly what I am doing:
1. Is where I am writing and reading.
2. Is my actual read.
// Set the unit on programming mode
clComm.Write("C",1);
do
{
clComm.Read(1);
cResponse = NULL;
cResponse = clComm.cTransBuff;
if (iRead_Tries >= 3)
goto No_Response;
iRead_Tries++;
}
// Wait for 0x0D
while (cResponse[0] != 0x0D);
//--------------------------------------------------------------------------
----
// Read from COM port
//--------------------------------------------------------------------------
----
void __fastcall Comm::Read(unsigned int iExpected)
{
// Initialize
iCollectBytes = 0;
dwActualBytesRead = 0;
bStop_Read = false;
COMSTAT comstat; // Structure of the communication device
// Do we have the handle ?
if (hComm)
{ // Begin if statement (Handle)
// Clear the buffers
for (int i = 0; i <= (BUFSIZE - 1); i++)
bCommData[i] = NULL;
for (int i = 0; i <= (BUFSIZE - 1); i++)
cTransBuff[i] = NULL;
// Set a safety timeout
dwRead_Time_Out = GetTickCount() + (iExpected*2);
// Stop read flag
while (bStop_Read == false)
{ // Begin while statement (Stop Read Flag)
// Do we have the handle ?
//if (hComm)
// Clear any communication error & return # of bytes in the input
buffer
ClearCommError(hComm, // handle to communications device
&dwError, // pointer to variable to receive error
codes
&comstat); // pointer to buffer for communications
status
// Is there anything waiting to be read (BUFSIZE bytes match
cCommData
//capability)
if ((comstat.cbInQue >= iExpected) && (comstat.cbInQue <= BUFSIZE))
{ // Begin if statement (InQue)
// Do we have the handle ?
//if (hComm)
// Read the data from the serial port
if (!ReadFile (hComm,
bCommData,
comstat.cbInQue,
&dwBytesTransferred,
&o))
{ // Begin if statement (read error)
// Check if error is Overlapped I/O pending
if (!(GetLastError() == ERROR_IO_PENDING))
// Report the error
ReportError();
} // End if statement (read error)
else
{ // Begin else statement (Bytes are waiting)
// Return the number of bytes that were read
GetOverlappedResult(hComm,
&o,
&dwActualBytesRead,
FALSE);
// Are there bytes waiting and check if buffer is not overrun
if ((dwActualBytesRead != 0) && (dwActualBytesRead <= BUFSIZE))
{ // Begin if statement (actual bytes read)
// Start the collection of bytes
for (unsigned int i = 0; i <= (dwActualBytesRead - 1); i++)
{ // Begin for statement (byte collect)
// Copy the data to the return variable
cTransBuff[iCollectBytes++] = bCommData[i];
} // End for statement (byte collect)
} // End if statement (actual bytes read)
} // End else statement (Byte are waiting)
} // End if statement (InQue)
// Safety timeout check
if (dwRead_Time_Out <= GetTickCount())
// End the read while loop
bStop_Read = true;
} // End while statement (Stop Read Flag)
} // End if statement (Handle)
}
It probably looks pretty weird, but I am not an expert. It works if
iRead_Tries is a number like 10, but that means according to my code that if
I am running my communication at 9600 baud, and I am expecting 1 byte and I
am multiplying by two, enough so that it will read it the first time and not
even have to get to three.
So I believe, that Windows does not report right away to the struct
__COMSTA, there is actually a delay of some sort? I can't put my finger on
it, but maybe you guys can see something I am doing wrong with this snippet
of code.
By the way, I am not always expecting one byte coming back, that is why, my
readfile has comstat.cbInQue, as the number of bytes to read.
Thank you,
Neil P.
Technical Details
Message-ID:
<427fd7cf$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<427fa27e@newsgroups.borland.com> <vugv711ona1kdbekucqqkg6csqkcd4agrd@4ax.com> <427fca8b$1@newsgroups.borland.com> <ukjv71lvb2s28pvi34r21cf1loa8nsqfnv@4ax.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,369fa724017da622 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Neil P." <nporve01@hotmail.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <427fa27e@newsgroups.borland.com> <vugv711ona1kdbekucqqkg6csqkcd4agrd@4ax.com> <427fca8b$1@newsgroups.borland.com> <ukjv71lvb2s28pvi34r21cf1loa8nsqfnv@4ax.com> Subject: Re: Response not in que? Date: Mon, 9 May 2005 17:28:07 -0400 Lines: 146 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1437 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441 NNTP-Posting-Host: 68.209.111.59 Message-ID: <427fd7cf$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1115674575 68.209.111.59 (9 May 2005 14:36:15 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail