thanks xpi0t0s...hey does the following code do the same thing?
Code:
#include "windows.h"
#include "time.h"
#include "string.h"
#include "stdio.h"

BOOL SetCommDefaults(HANDLE hSerial);

int main(int argc, char* argv[])
{
    HANDLE hSerial = CreateFile("COM2",GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED |FILE_FLAG_NO_BUFFERING,NULL);
    if (hSerial == INVALID_HANDLE_VALUE) return GetLastError();
    SetCommDefaults(hSerial);

    HANDLE hReadEvent = CreateEvent(NULL,TRUE,FALSE,"RxEvent");
    OVERLAPPED ovRead;
    OVERLAPPED ovWrite;
    memset(&ovRead,0,sizeof(ovRead));
    memset(&ovWrite,0,sizeof(ovWrite));

    ovRead.hEvent = hReadEvent;

    char szRxChar = 0;
    DWORD dwBytesRead = 0;
    DWORD dwBytesWritten = 0;

    while(szRxChar != 'q')
    {
        // Check if a read is outstanding
        if (HasOverlappedIoCompleted(&ovRead))
        {
            // Issue a serial port read
            if (!ReadFile(hSerial,&szRxChar,1,
                    &dwBytesRead,&ovRead))
            {
                DWORD dwErr = GetLastError();
                if (dwErr!=ERROR_IO_PENDING)
                    return dwErr;
            }
        }

        // Write the time out to the serial port
        time_t t_time = time(0);
        char buf[50];
        sprintf(buf,"Time is %s\n\r",ctime(&t_time));
        if (HasOverlappedIoCompleted(&ovWrite))
        {
            WriteFile(hSerial,buf,strlen(buf),
                    &dwBytesWritten,&ovWrite);
        }


        // ... Do some other processing

        // Wait 5 seconds for serial input
        if (!(HasOverlappedIoCompleted(&ovRead)))
            WaitForSingleObject(hReadEvent,5000);

        // Check if serial input has arrived
        if (GetOverlappedResult(hSerial,&ovRead,
                &dwBytesRead,FALSE))
        {
            // Wait for the write
            GetOverlappedResult(hSerial,&ovWrite,
                &dwBytesWritten,TRUE);
            // Display a response to input
            sprintf(buf,"You pressed the '%c' key\n\r",
                szRxChar);
            WriteFile(hSerial,buf,strlen(buf),
                    &dwBytesWritten,&ovWrite);
        }

    }

    CloseHandle(hSerial);
    CloseHandle(hReadEvent);
    return 0;
}

BOOL SetCommDefaults(HANDLE hSerial)
{
    DCB dcb;
    memset(&dcb,0,sizeof(dcb));
    dcb.DCBlength=sizeof(dcb);
    if (!GetCommState(hSerial,&dcb))
        return FALSE;
    dcb.BaudRate=9600;
    dcb.ByteSize=8;
    dcb.Parity=0;
    dcb.StopBits=ONESTOPBIT;
    if (!SetCommState(hSerial,&dcb))
        return FALSE;
    return TRUE;
}

Last edited by shabbir; 7Oct2008 at 10:13.. Reason: Code block