Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
Windos
win32.3rdparty
win32.directx.audio
win32.directx.ddk
win32.directx.graphics
win32.directx.input
win32.directx.managed
win32.directx.misc
win32.directx.networking
win32.directx.sdk
win32.directx.video
win32.dirx.grap.shaders
win32.gdi
win32.international
win32.kernel
win32.messaging
win32.mmedia
win32.networks
win32.ole
win32.rtc
win32.tapi
win32.tapi.beta
win32.tools
win32.ui
win32.wince
win32.wmi
windows.mediacenter
winfx.aero
winfx.announcements
winfx.avalon
winfx.collaboration
winfx.fundamentals
winfx.general
winfx.indigo
winfx.sdk
winfx.winfs
  
 
date: Mon, 16 Jun 2008 22:12:00 -0700,    group: microsoft.public.win32.programmer.directx.managed        back       


problem using ReadFile/WriteFile in overlapped I/O   
I am sorry if this is in the wrong group. I am making a C program that 
accesses the COM port for reading & writing AT commands to my mobile phone. 
my problem is that I want the reading & writing to take place independently 
and I want it to be completely event driven. I am currently using ReadFile 
with an infinite timeout so that I don't do anything until I receive 
something in the buffer. But I am unable to understand how to issue a 
WriteFile command while I am still waiting for the read to take place. I am 
using the following code:

                DCB dcb;
	HANDLE hCom;
	BOOL fSuccess;
	OVERLAPPED o;
	COMMTIMEOUTS timeouts;
	DWORD dwEvtMask;
	char *pcCommPort = "COM4";  //specify the port to be opened

	hCom = CreateFile( pcCommPort,
                     GENERIC_READ | GENERIC_WRITE, //read & write
                     0,    // comm devices must be opened w/exclusive-access
                     NULL, // no security attributes
                     OPEN_EXISTING, // comm devices must use OPEN_EXISTING
					 FILE_FLAG_OVERLAPPED, //Overlapped I/O enabled
                     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);
	}

    fSuccess = GetCommState(hCom, &dcb);

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

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

 	dcb.BaudRate = CBR_9600;     // 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 is successfully reconfigured.\n", pcCommPort);

    //Set port timeout values
    timeouts.ReadIntervalTimeout         = MAXDWORD; //These values ensure 
that the read operation
    timeouts.ReadTotalTimeoutMultiplier  = 0;        //returns immediately 
with the characters that have
    timeouts.ReadTotalTimeoutConstant    = 0;        //already been 
received, even if none are received
    timeouts.WriteTotalTimeoutMultiplier = 0;        //Time-outs not used
    timeouts.WriteTotalTimeoutConstant   = 0;        //for write operation


    if (!SetCommTimeouts(hCom, &timeouts))
    {
        //assert(0);
        printf("Error setting time-outs. %d\n",GetLastError());
        return (1);
    }

    // Set the event mask.

    fSuccess = SetCommMask(hCom, EV_RXCHAR | EV_TXEMPTY);

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

BOOL WriteBuffer(HANDLE hComm,char * lpBuf)
{
   OVERLAPPED osWrite = {0};
   DWORD dwWritten;
   DWORD dwToWrite=strlen(lpBuf);
   BOOL fRes;

   // Create OVERLAPPED structure hEvent.
   osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
   if (osWrite.hEvent == NULL)
      // Error creating overlapped event handle.
      return FALSE;

   // Issue write.
   if (!WriteFile(hComm, lpBuf, dwToWrite, &dwWritten, &osWrite))
   {
      if (GetLastError() != ERROR_IO_PENDING)
      {
         // WriteFile failed, but it isn't delayed. Report error and abort.
         printf("\nWriteFile failed with error %d.\n",GetLastError());
         fRes = FALSE;
      }
      else
      {
         // Write is pending.
         if (!GetOverlappedResult(hComm, &osWrite, &dwWritten, TRUE))
            fRes = FALSE;
         else
         {   // Write operation completed successfully.
            printf("\nWrite operation completed successfully.\n");
            //printf("Bytes written=%d\n",dwWritten);
            fRes = TRUE;
         }
      }
   }
   else
   {
      // WriteFile completed immediately.
      printf("\nWriteFile completed immediately.\n");
      fRes = TRUE;
   }

   CloseHandle(osWrite.hEvent);
   return fRes;
}

int ReadBuffer(HANDLE HCom)
{
    OVERLAPPED osRead = {0};
    DWORD dwRead;
    BOOL fRead;

    // Create OVERLAPPED structure hEvent.
    osRead.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (osRead.hEvent == NULL)
      // Error creating overlapped event handle.
      return (1);

    DWORD dwCommEvent;

    for ( ; ; )
    {
        WaitCommEvent(HCom, &dwCommEvent, &osRead);
        if ( WaitForSingleObject(osRead.hEvent,INFINITE) == WAIT_OBJECT_0)
        {
            char szBuf[100];
            do
            {
                memset(szBuf,0,sizeof(szBuf));
                ReadFile( HCom,szBuf,sizeof(szBuf),&dwRead,&osRead);    
                if(dwRead!=0)
                {
                    printf("%s\n",szBuf);
                }
            }while (dwRead > 0 );
        }
        else
        {
            printf("Waiting for single object failed with error no 
%d\n",GetLastError());
            CloseHandle(osRead.hEvent);
            return (2);
        }
    }
    CloseHandle(osRead.hEvent);
}
date: Mon, 16 Jun 2008 22:12:00 -0700   author:   nk28

Re: problem using ReadFile/WriteFile in overlapped I/O   
"nk28"  schrieb
>I am sorry if this is in the wrong group.

Yep, it is. :) It's about 
http://msdn.microsoft.com/en-us/library/bb318658%28VS.85%29.aspx


Armin
date: Tue, 17 Jun 2008 14:13:56 +0200   author:   Armin Zingler

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us