Asynchronous Overlapped I/O usign VC++ 6

C, C++, Visual C++, C++.Net Topics
Post Reply
User avatar
SevenZero
Major
Major
Posts: 263
Joined: Sun Nov 01, 2009 8:37 pm

Asynchronous Overlapped I/O usign VC++ 6

Post by SevenZero » Fri Nov 04, 2011 1:31 am

Here is a nicely written code that describes serial asynchronous communication over COM port 2.

Code: Select all

#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;
}
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: Asynchronous Overlapped I/O usign VC++ 6

Post by SemiconductorCat » Sat Nov 05, 2011 6:04 am

First of all good source code ! I suggest other users who are interest in C++ to review this code.
we can get fun out of code reviewing ! Believe me.

secondly.I have gone through your code ,and this is my review output.
I have some questions to ask/know and experience to share.

1.isn't that #include <windows.h> ? but in code it's #include "windows.h"
#include "windows.h" means search the file in the directory that same source file exists.

2. Before opening the "COM2" don't I need to check for the security attributes?
I think you need to pass the ACCESS_SYSTEM_SECURITY to the last parameter (handle to a
template file) in CreateFile API.So this code may not work in Windows 7, I have a windows 7
x64 computer , I'll check and feedback about this.

so that's only I noticed in my short code review of your beautiful code, I suggest others also to
review this code.Believe me code reviewing is fun ;)
User avatar
Enigma
Lieutenant
Lieutenant
Posts: 74
Joined: Sun Jan 16, 2011 12:40 am
Location: Colombo, Sri Lanka

Re: Asynchronous Overlapped I/O usign VC++ 6

Post by Enigma » Sat Nov 05, 2011 9:15 pm

Hi
Nice code SevenZero :)
2. Before opening the "COM2" don't I need to check for the security attributes?
I think you need to pass the ACCESS_SYSTEM_SECURITY to the last parameter (handle to a
template file) in CreateFile API.So this code may not work in Windows 7, I have a windows 7
x64 computer , I'll check and feedback about this.
@Sandun Bro - He is using OPEN_EXISTING flag so I think the hTemplateFile file must be NULL.

Thanks
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: Asynchronous Overlapped I/O usign VC++ 6

Post by SemiconductorCat » Sun Nov 06, 2011 7:19 am

yes , your correct, hTemplateFile is ignored unless we create a new file.
so when OPEN_EXISTING is there then it's ignored.

However , this code is failing in windows 7 x64 bit unless you give the run as administrator option.
So I think we need to grant or check for the privileges before invoking the CreateFile() API.

I'll try to fix the code for windows 7 x64. You can also try , if you got a x64 windows 7 computer.
Post Reply

Return to “C/C++ Programming”