Help with ReadFile()

S

salsipius

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

Mike Wahler

salsipius said:
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?

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'.
I have pasted the read method
below, it takes a void * as the buffer. I know I need to send something
by reference

No you don't (you could, but there's no reason to).
but am unclear on how to do it.

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

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.)
Can anyone help Please?

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
 
J

John Harrison

salsipius said:
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.

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

[snip]


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

Should be

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

no & before buffer.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top