Com Ports

Posted on 1997-04-21
Medium Priority
Last Modified: 2013-12-04

I've been using the following code to open and read data from a com port

      h = CreateFile("Com1",

      if(h == INVALID_HANDLE_VALUE) {
         PrintError("Failed to open port",ComNumber);

***** To read from the port

      COMMTIMEOUTS cto = { 2, 1, 1, 0, 0 };
            PrintError("SetCommTimeouts failed",ComNumber);

      // create event for overlapped I/O
   osRead.hEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
   if(osRead.hEvent == INVALID_HANDLE_VALUE)
      PrintError("CreateEvent failed", ComNumber);

   // wait for received characters
   if(!SetCommMask(h,EV_RXCHAR | EV_RLSD))
      PrintError("SetCommMask failed", ComNumber);

   while( !Finished ) {
      // get the event mask
      if( !WaitCommEvent(h,&mask,&osRead) ) {
         DWORD e = GetLastError();
         if( e == ERROR_IO_PENDING ) {
            DWORD r;
            if( !GetOverlappedResult(h,&osRead,&r,TRUE) ) {
               PrintError("GetOverlappedResult failed",ComNumber);
         } else {
            PrintError("WaitCommEvent failed", ComNumber);

      // if no event, then UI thread terminated with SetCommMask(h,0)
      if( mask == 0 ) {
         return ExitThread;

      if( mask & EV_RLSD)
              return CDLost;
         char buf[10];
         DWORD read;


             do {
             read = 0;
            if( !ReadFile(h,buf,sizeof(buf),&read,&osRead) ) {
               if( GetLastError() == ERROR_IO_PENDING ) {
                  if( !GetOverlappedResult(h,&osRead,&read,TRUE) ) {
                     PrintError("GetOverlappedResult failed", ComNumber);
               } else {
                  PrintError("ReadFile failed", ComNumber);
*** We have some data

All works ok.  The routine waits until data is received then reads it in.  If their is a change in CD signal this is also recognised.

The problem I have is I wish the GetOverlappedresult function to return after a set time interval.   Whether there is any data or not.

I have tried the following

      COMMTIMEOUTS cto = { 200, 1, 3000, 0, 0 };

        PrintError("SetCommTimeouts failed",ComNumber);            
        if( !ReadFile(h,buf,MaxCharNumber,&read,&osRead) ) {
                        PrintError("ReadFile failed", ComNumber);
                        return FALSE;

            if (read)
            *** We have data      

Which works as required. I.E if after 3 seconds no data is received ReadFile returns with read=0, if data is received ReadFile returns straight away with the data.

But.  If i call  ReadFile again it returns straight away even when there is no data,  How do I get it to perform as it does the first time i called ReadFile.

Or can I change the first example code so that  GetOverLappedResult returns after a set interval.
Question by:Robert888
LVL 15

Expert Comment

ID: 1334703
Can you post source code with ReadFile and comments for what ReadFile(), GetOverlappedResult(), GetLastError() returns in each case?

Accepted Solution

Melange earned 300 total points
ID: 1334704
   Instead of immediately calling GetOverlappedResult after WaitCommEvent, wait on your event object with WaitForSingleObject. You can set the timeout period for waiting on the event in milliseconds in the call to WaitForSingleObject.
    Also I would recommend using a manual reset event when doing timeouts. This way if you do timeout, you can still check the status of the event later on without having to worry about missing it.
    Once you get a signalled event (not a timeout), call ResetEvent and then call GetOverlappedResult. Since the operation should now be completed, GetOverlappedResult will now return immediately with the requested info (either success or failure). If you need more info, let me know.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Choosing the right mix of apps is very much necessary for CPAs for making the most of the latest technology through which they can boost their growth.
This tutorial summarizes the causes behind"an unknown error prevented access to the PST File”.  It also suggests the various solutions to fix the problem.
Video by: Tony
This video teaches viewers how to export a project from Adobe Premiere Pro and the various file types involved.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question