Connecting Tech Pros Worldwide Forums | Help | Site Map

Help with ReadFile()

salsipius
Guest
 
Posts: n/a
#1: Oct 11 '05
Hi all thanks in advance for reading. I am writing a serial port app
with the help of many examples and I am not quite clear on how to get
the data back from the method call? I have pasted the read method
below, it takes a void * as the buffer. I know I need to send something
by reference but am unclear on how to do it.

What I want it the buffer to be a string that I can use or manipulte.
Can anyone help Please?

int MBSerial::ReadData( void *buffer, int limit )
{

if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );

BOOL bReadStatus;
DWORD dwBytesRead, dwErrorFlags;
COMSTAT ComStat;

ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );
if( !ComStat.cbInQue ) return( 0 );

dwBytesRead = (DWORD) ComStat.cbInQue;
if( limit < (int) dwBytesRead ) dwBytesRead = (DWORD) limit;

bReadStatus = ReadFile( m_hIDComDev, &buffer, dwBytesRead,
&dwBytesRead, &m_OverlappedRead );
if( !bReadStatus ){
if( GetLastError() == ERROR_IO_PENDING ){
WaitForSingleObject( m_OverlappedRead.hEvent, 2000 );
return( (int) dwBytesRead );
}
return( 0 );
}
return( (int) dwBytesRead );
}


Mike Wahler
Guest
 
Posts: n/a
#2: Oct 11 '05

re: Help with ReadFile()



"salsipius" <salsipius@salsipius.com> wrote in message
news:1129050397.033095.182650@g47g2000cwa.googlegr oups.com...[color=blue]
> Hi all thanks in advance for reading. I am writing a serial port app
> with the help of many examples and I am not quite clear on how to get
> the data back from the method call?[/color]

From looking at the code below, I see two possible pieces
of information you can receive from the 'ReadData()' function:
the type 'int' value it returns (representing the actual
number of bytes read), and the data stored at the address
represented by the parameter 'buffer'.
[color=blue]
> I have pasted the read method
> below, it takes a void * as the buffer. I know I need to send something
> by reference[/color]

No you don't (you could, but there's no reason to).
[color=blue]
> but am unclear on how to do it.[/color]

Allocate sufficient storage to represent whatever data the
function stores there, and pass its address as the 'buffer'
parameter.
[color=blue]
>
> What I want it the buffer to be a string that I can use or manipulte.[/color]

Well, the 'ReadFile()' function appears to store data
at address 'buffer'. I don't know if it places a string
terminator at the end of the data or not. There's no
'ReadFile()' function in standard C++, so you'll need
to look up its documentation to see how to use it and
what it does. (If you want the data at 'buffer' to be
a string, and ReadFile doesn't terminate it with a zero,
just allocate an additional byte in your storage, and
put it there yourself. Hint: the 'bytes read' return value
will tell you where to put it.)
[color=blue]
> Can anyone help Please?[/color]

Not with the nonstandard function, not here.
The 'Hungarian notation' makes this stuff look like
Windows, if so try a Windows newsgroup, website
(e.g. www.msdn.microsoft.com), or mailing list.

-Mike
[color=blue]
>
> int MBSerial::ReadData( void *buffer, int limit )
> {
>
> if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );
>
> BOOL bReadStatus;
> DWORD dwBytesRead, dwErrorFlags;
> COMSTAT ComStat;
>
> ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );
> if( !ComStat.cbInQue ) return( 0 );
>
> dwBytesRead = (DWORD) ComStat.cbInQue;
> if( limit < (int) dwBytesRead ) dwBytesRead = (DWORD) limit;
>
> bReadStatus = ReadFile( m_hIDComDev, &buffer, dwBytesRead,
> &dwBytesRead, &m_OverlappedRead );
> if( !bReadStatus ){
> if( GetLastError() == ERROR_IO_PENDING ){
> WaitForSingleObject( m_OverlappedRead.hEvent, 2000 );
> return( (int) dwBytesRead );
> }
> return( 0 );
> }
> return( (int) dwBytesRead );
> }
>[/color]


John Harrison
Guest
 
Posts: n/a
#3: Oct 12 '05

re: Help with ReadFile()


salsipius wrote:[color=blue]
> Hi all thanks in advance for reading. I am writing a serial port app
> with the help of many examples and I am not quite clear on how to get
> the data back from the method call? I have pasted the read method
> below, it takes a void * as the buffer. I know I need to send something
> by reference but am unclear on how to do it.[/color]

No you don't. void* is a pointer not a reference.
[color=blue]
>
> What I want it the buffer to be a string that I can use or manipulte.
> Can anyone help Please?
>[/color]

[snip]
[color=blue]
>
> bReadStatus = ReadFile( m_hIDComDev, &buffer, dwBytesRead,
> &dwBytesRead, &m_OverlappedRead );[/color]

Should be

bReadStatus = ReadFile( m_hIDComDev, buffer, dwBytesRead,
&dwBytesRead, &m_OverlappedRead );

no & before buffer.

john
Closed Thread