MFC ReadFile issue
Posted on 2002-04-23
I am trying to write some software that will talk over a serial port, async, and in the background.
When I set up my port, I use overlaped comms cuz of the async...
HANDLE com_port = CreateFile(
GENERIC_READ | GENERIC_WRITE,
0, // comm devices must be opened w/exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
FILE_FLAG_OVERLAPPED, // overlapped I/O
NULL); // hTemplate must be NULL for comm devices
In this mode, I then setup timeouts and settings to the right speed etc.
I am able to connect a remote Hyperterm with a NULL modem cable and I can send data with write File...
BOOL w = WriteFile(comms_channel,
But I cannot receive data with a read file... (the overlap below is a different one to the one used for the write... I also tried using the same one, or none at all, all round)
bResult = ReadFile(
hCommPortHandle, // handle to file to read from
&chInputByte, // pointer to buffer that receives data
1, // number of bytes to read
&iLengthRead, // pointer to number of bytes read
&ov); // pointer to overalp structure for data
Without an overlapped port, the read succeeds, but there is no data there... when I connect a local hyperterm... there is definately data in the receive buffer.
Microsoft documentation is pants with regard to the overlapped data stuff, and I cannot get it to work... there was an example of setting the ov.hEvent to be another handle and this is set when the read is finished... but there was no documentation for that either.
Anyone know how I can recieve data over a COM port under windows 2K/NT using a file handle asynchronously whilst writing on it.