From: seattleboatguy on
I have a MFC application that creates a thread, opens a serial port in
the thread, and attempts to write to the port (some sample code appears
below). The WriteFile() fails, and GetLastError() tells me that the
error code is 997, which is an overlap error. I'm new to serial
programming, but as I understand it, you get an overlap error when a
write conflicts with a read. I am not intentionally reading anything
from the port, so I don't know why I'm getting the error. Do you have
any suggestions? Thanks in advance.

int SerialThread::writeToPort(char * outBuf, int charCnt)
{
BOOL stat;
unsigned long outBytes=0, errWd=0;
COMSTAT comStat;
ResetEvent(m_olOutEvent.hEvent);
comStat.cbInQue = comStat.cbOutQue = 0;
stat = ClearCommError(m_hComHandle, &errWd, &comStat);
if ((comStat.cbOutQue == 0) && (charCnt > 0))
{
stat = WriteFile(m_hComHandle,
outBuf,
charCnt,
&outBytes,
&m_olOutEvent);

From: William DePalo [MVP VC++] on
"seattleboatguy" <douglas(a)eskimo.com> wrote in message
news:1139579942.805554.158200(a)z14g2000cwz.googlegroups.com...
>I have a MFC application that creates a thread, opens a serial port in
> the thread, and attempts to write to the port (some sample code appears
> below). The WriteFile() fails, and GetLastError() tells me that the
> error code is 997, which is an overlap error. I'm new to serial
> programming, but as I understand it, you get an overlap error when a
> write conflicts with a read. I am not intentionally reading anything
> from the port, so I don't know why I'm getting the error. Do you have
> any suggestions? Thanks in advance.

The error code tells you only that the I/O operation is ongoing. You can't
get complete status until the transfer is done. After WriteFile() returns
successfully or when it returns ERROR_IO_PENDING you can add a call to
GetOverlappedResult(). If the last paramter value is TRUE the function will
wait for the operation to complete.

Regards,
Will


From: Scott McPhillips [MVP] on
seattleboatguy wrote:
> I have a MFC application that creates a thread, opens a serial port in
> the thread, and attempts to write to the port (some sample code appears
> below). The WriteFile() fails, and GetLastError() tells me that the
> error code is 997, which is an overlap error. I'm new to serial
> programming, but as I understand it, you get an overlap error when a
> write conflicts with a read. I am not intentionally reading anything
> from the port, so I don't know why I'm getting the error. Do you have
> any suggestions? Thanks in advance.
>
> int SerialThread::writeToPort(char * outBuf, int charCnt)
> {
> BOOL stat;
> unsigned long outBytes=0, errWd=0;
> COMSTAT comStat;
> ResetEvent(m_olOutEvent.hEvent);
> comStat.cbInQue = comStat.cbOutQue = 0;
> stat = ClearCommError(m_hComHandle, &errWd, &comStat);
> if ((comStat.cbOutQue == 0) && (charCnt > 0))
> {
> stat = WriteFile(m_hComHandle,
> outBuf,
> charCnt,
> &outBytes,
> &m_olOutEvent);
>

This is normal behavior when using overlapped I/O. It means the I/O has
not completed yet but your code is free to perform other work while the
I/O operation proceeds on its own. That's the purpose of using
overlapped I/O.

You need to find the MTTTY sample code in MSDN, and the related article
about serial programming for more description of how to handle
overlapped I/O with the serial port. It is somewhat complex, but the
MTTTY sample code gives you everything you need.

--
Scott McPhillips [VC++ MVP]