Connecting Tech Pros Worldwide Help | Site Map

Help with ReadFile()

 
LinkBack Thread Tools Search this Thread
  #1  
Old October 11th, 2005, 05:15 PM
salsipius
Guest
 
Posts: n/a
Default Help with ReadFile()

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 );
}


  #2  
Old October 11th, 2005, 06:25 PM
Mike Wahler
Guest
 
Posts: n/a
Default 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]


  #3  
Old October 12th, 2005, 07:25 AM
John Harrison
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.