?
Solved

overlapped I/O

Posted on 1999-10-29
3
Medium Priority
?
495 Views
Last Modified: 2013-11-20
My application needs to write to the comm ports (COMM5 & COMM6).

Can someone explain what's overlapped I/o and nonoverlapped I/O. Please give a detailed answer.

0
Comment
Question by:sunraj
3 Comments
 
LVL 3

Expert Comment

by:akalmani
ID: 2171119
Hope this helps , the contents are from MSDN help

1) Synchronization and Overlapped Input and Output
2) Overlapped I/O    
    The help is in deatil and extremely large he has given each and every concepts about Overlapped I/O.
0
 
LVL 2

Expert Comment

by:wyy_cq
ID: 2171463
when a reading/writing overlapped , the function will return immediately .
BUT the data is not ready, when the data is ready or the operation is fail a callback function you provide will be called .
0
 
LVL 2

Accepted Solution

by:
abdij earned 40 total points
ID: 2279432
There are two modes of IO. One is blocked IO where the program control is blocked (no other opertions can be performed by the threadat the time till io occurs). Second one and the prefered one is the Overlapped or non-blocked IO wher you just peek to see if IO has happened and if so proceed.

Here is a sample (MSDN) that illustrate how to do non-blocked it:

The following example opens a handle to COM5 and fills in a DCB structure with the current configuration. The DCB structure is then modified and used to reconfigure the device.

/* A sample program to illustrate setting up a serial port. */

#include <windows.h>

int
main(int argc, char *argv[])
{
  DCB dcb;
  HANDLE hCom;
  BOOL fSuccess;
  char *pcCommPort = "COM5";

  hCom = CreateFile( pcCommPort,
                     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
                     0,    // not overlapped I/O
                     NULL  // hTemplate must be NULL for comm devices
                     );

  if (hCom == INVALID_HANDLE_VALUE) {
      // Handle the error.
      printf ("CreateFile failed with error %d.\n", GetLastError());
      return (1);
    }

  // We will build on the current configuration, and skip setting the size
  // of the input and output buffers with SetupComm.

  fSuccess = GetCommState(hCom, &dcb);

  if (!fSuccess) {
      // Handle the error.
      printf ("GetCommState failed with error %d.\n", GetLastError());
      return (2);
    }

  // Fill in the DCB: baud=57,600 bps, 8 data bits, no parity, and 1 stop bit.

  dcb.BaudRate = CBR_57600;     // set the baud rate
  dcb.ByteSize = 8;             // data size, xmit, and rcv
  dcb.Parity = NOPARITY;        // no parity bit
  dcb.StopBits = ONESTOPBIT;    // one stop bit

  fSuccess = SetCommState(hCom, &dcb);

  if (!fSuccess) {
      // Handle the error.
      printf ("SetCommState failed with error %d.\n", GetLastError());
      return (3);
    }

  printf ("Serial port %s successfully reconfigured.\n", pcCommPort);
  return (0);
}

// Here is the Listener or the event handler code snippet (MSDN)

The following example code opens the serial port for overlapped I/O, creates an event mask to monitor CTS and DSR signals, and then waits for an event to occur. The WaitCommEvent function should be executed as an overlapped operation so the other threads of the process cannot perform I/O operations during the wait.

HANDLE hCom;
OVERLAPPED o;
BOOL fSuccess;
DWORD dwEvtMask;

hCom = CreateFile( "COM1",
    GENERIC_READ | GENERIC_WRITE,
    0,    // exclusive access
    NULL, // no security attributes
    OPEN_EXISTING,
    FILE_FLAG_OVERLAPPED,
    NULL
    );

if (hCom == INVALID_HANDLE_VALUE)
{
    // Handle the error.
}

// Set the event mask.

fSuccess = SetCommMask(hCom, EV_CTS | EV_DSR);

if (!fSuccess)
{
    // Handle the error.
}

// Create an event object for use in WaitCommEvent.

o.hEvent = CreateEvent(
    NULL,   // no security attributes
    FALSE,  // auto reset event
    FALSE,  // not signaled
    NULL    // no name
    );

assert(o.hEvent);

if (WaitCommEvent(hCom, &dwEvtMask, &o))
{
    if (dwEvtMask & EV_DSR)
    {
         // To do.
    }

    if (dwEvtMask & EV_CTS)
    {
         // To do.
    }
}

Since you have not explained what exactlty you are communicating with i will simply assume it does not matter and procced to give you the code snippet to write to the port.

BOOL bRes = FALSE;
DWORD nNumberOfBytesToWrite = 0;
DWORD NumberOfBytesWritten = 0;
OVERLAPPED OverLapped;
char szBuffer[] = "This is what you wanted to write";

// create the overlapped event
OverLapped.hEvent = CreateEvent(
    NULL,   // no security attributes
    FALSE,  // auto reset event
    FALSE,  // not signaled
    NULL    // no name
    );


// there are 32 characters in my buffer
nNumberOfBytesToWrite = 32;

bRes = WriteFile(hCom,(LPCVOID) szBuffer,nNumberOfBytesToWrite,  &NumberOfBytesWritten,OverLapped);

if(!bRes)
{
 // handle write error
 // may be overlapped error
 DWORD dwError = GetLastError();
 if(dwError == ERROR_IO_PENDING)
 {
  // not an actual error
 }
 else
 {
  // error
 }

// write success
 ;
}

On the success of WriteFile() the event created in the OverLapped object will be signalled. You need to have a WaitCommEvent() in the write thread just like the one shown in the Read thread above.

Feel free to contact me for more info
abdij_b@hotmail.com

Hpope this is useful
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

601 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