I am trying to communicate with my phone over a serial port by sending AT commands to it. While I'm able to write the command to it, I'm unable to read the reply. I am using overlapped I/O & using the WaitCommEvent to retrieve the result of the write command, as & when it arrives. However, I keep getting error no 997(overlapped I/O is in progress). here's my code:

DCB dcb;
	HANDLE hCom;
	BOOL fSuccess;
	OVERLAPPED o;
	COMMTIMEOUTS timeouts;
	DWORD dwEvtMask;
	char buffer[2048];
	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);
    }
//write command is successfully issued here as a function call

OVERLAPPED osRead = {0};
   DWORD dwRead;
    char chread[200];
   DWORD dwToRead=strlen(chread);
   BOOL fRead;


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

if (!SetCommMask(hCom, EV_RXCHAR))
    {
        // Error setting communications event mask
        printf("Error setting communications event mask\n");
        return FALSE;
    }

DWORD dwCommEvent;
    for ( ; ; )
    {
        if (WaitCommEvent(hCom, &dwCommEvent, &osRead))
        {
            do
            {
                if (ReadFile(hCom, &chread, strlen(chread), &dwRead, &osRead))
                    // A byte has been read; process it.
                    printf("%s\n",chread);
                else
                {
                    // An error occurred in the ReadFile call.
                    printf("ReadFile failed with error no %d.\n",GetLastError());
                    break;
                }
            } while (dwRead);
        }
        else
        {
            // Error in WaitCommEvent
            printf("WaitCommEvent failed with error no %d.\n",GetLastError());
            break;
        }
    }
   CloseHandle(osRead.hEvent);
}

Brother I'm getting the same error ...If you solved that one.
Help me .....

Here I copy my hard partition....The problem is IO overlapped is not working properly...I think u know the details about this ...that is u have already faced this problem....

I'm getting the messages like IO is in progress like that...
And at last I couldn't unlock the partition,,,I nvalid handle occurs...
Here I copy my 1GB (R: ) source partition to destination partition (N:) with block size of 50 Mb.

Any way see my source code

HANDLE h_MyDevice_src2,h_MyDevice_dest2;
	  BOOL bResult,bResult1,bResult2,bResult_unlock,bResult_unlock1;
	 DWORD dwRetBytes1,dwRetBytes2;


	 STARTING_LCN_INPUT_BUFFER stStartLCN;

	 stStartLCN.StartingLcn.QuadPart = 0;

	    LPVOID bMBR2 = VirtualAlloc(NULL,52428800,MEM_COMMIT,PAGE_READWRITE); 

     if(!bMBR2)
     {
     printf("Buffer Allocation failed");
     // goto label;
      ExitProcess(1);
     } 
	


	


	  for (int k = 0; k < 5; k++) 
   { 
 
   // Create an event object for this instance. 
 
      hEvents[k] = CreateEvent( 
         NULL,    // default security attribute 
         TRUE,    // manual-reset event 
         TRUE,    // initial state = signaled 
         NULL);   // unnamed event object 
	  //CreateEvent(NULL, TRUE, FALSE, NULL)

      if (hEvents[k] == NULL) 
      {
         printf("CreateEvent failed with %d.\n", GetLastError()); 
         return 0;
      }

	   
	  }
        



	  h_MyDevice_src2 = CreateFile(/*(L"\\Device\\HarddiskVolume8" */ _T("\\\\.\\R:"),

		                          GENERIC_READ|GENERIC_WRITE,
								  FILE_SHARE_READ | FILE_SHARE_WRITE,
                                  NULL,
								  OPEN_EXISTING,
								  /*FILE_FLAG_NO_BUFFERING*/
								  FILE_ATTRIBUTE_NORMAL |FILE_FLAG_OVERLAPPED,
								  NULL);

      if (h_MyDevice_src2==INVALID_HANDLE_VALUE)
	  {
	  printf("\nUnable to Open the Device: Error Number is : %d\n",GetLastError());	
	  }


		  
	   
	  h_MyDevice_dest2=CreateFile(/*L"\\Device\\HarddiskVolume11"*/ L"\\\\.\\N:",GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,
                                   NULL,OPEN_EXISTING,/*FILE_FLAG_NO_BUFFERING*/FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, NULL);
      if (h_MyDevice_dest2==INVALID_HANDLE_VALUE)
	  {
	  printf("\nUnable to Open the Device: Error Number is : %d\n",GetLastError());
	  }



     OVERLAPPED ovlp2;	 
	 ovlp2.Offset     = 0; 
     ovlp2.OffsetHigh = 0; 
	 ovlp2.hEvent     = hEvents[2];


     //Lock source 

	   bResult	= DeviceIoControl(h_MyDevice_src2,FSCTL_LOCK_VOLUME,NULL,NULL,NULL,NULL,&BytesReturned,&ovlp2);			
	  if(!bResult)
	  {
	  printf("Lock Volume Failed %d \n",GetLastError());
	  }

       

	/*    

      bResult2= DeviceIoControl(h_MyDevice_src2,
		                        FSCTL_GET_VOLUME_BITMAP,
								&stStartLCN,            // input buffer
								sizeof(stStartLCN),     // size of input buffer
								&vbb,                    //A pointer to the output buffer
								sizeof(vbb),// size of output buffer 

								&BytesReturned,         // number of bytes returned
								 gLPOVERLAPPED3     // LPOVERLAPPED structure

								);			
	  if(bResult2==0)
	  {
	printf("Tthe buffer was not large enough to accommodate a complete bitmap from the requested starting LCN to the last cluster on the volume. + Error no %d \n",GetLastError());
	  }

*/
      

	 OVERLAPPED ovlp3;	 
	 ovlp3.Offset     = 0; 
     ovlp3.OffsetHigh = 0; 
	 ovlp3.hEvent     = hEvents[3];

     //Lock destination 

	  bResult1	= DeviceIoControl(h_MyDevice_dest2,FSCTL_LOCK_VOLUME,NULL,NULL,NULL,NULL,&BytesReturned,&ovlp3);			
	  if(!bResult1)
	  {
	   printf("Lock Volume Failed %d \n",GetLastError());
	  }

	

			
	

	 //Setting file pointer 
    	 
	// LONGLONG lDistance =sHardDiskInfo[7].llStartingOffset;

	   LONGLONG lDistance11=0;
	   LONGLONG lDistance22=0;



     //write file set pointer
	 DWORD dwPtr1=SetFilePointer(h_MyDevice_src2,lDistance11,NULL,FILE_CURRENT);

     if (dwPtr1 == INVALID_SET_FILE_POINTER) // Test for failure
	 { 
	 DWORD dwError1 = GetLastError(); 
	 cout<<"\n Error occured  "<<dwError1;
	 } 
     
   //  InitializeCriticalSection(&cs1);  
    // EnterCriticalSection( &cs1 );  


   
	 
	 
	 OVERLAPPED ovlp0;	 
	 ovlp0.Offset     = 0; 
     ovlp0.OffsetHigh = 0; 
	 ovlp0.hEvent     = hEvents[0];
	 

	 OVERLAPPED ovlp1;
	 ovlp1.Offset     = 0; 
     ovlp1.OffsetHigh = 0; 
	 ovlp1.hEvent=hEvents[1];
	 









	 for( LONGLONG i=1;i<=1073741824/52428800;i+=1)  // 2gb/50 mb
	 {  
       

     if(!ReadFile(h_MyDevice_src2, bMBR2,52428800, &dwRetBytes1,&ovlp0)) 	//50mb
	 {
     printf("\nUnable to Read the Drive with  Error Number is : %d\n",GetLastError());		  
	 }
	
   //   LeaveCriticalSection( &cs1 ); 
     

	
	 cout<<"\n \n"<< i <<" restored ";


	 WriteFile(h_MyDevice_dest2,bMBR2,52428800,&dwRetBytes2,&ovlp1);

	 lDistance11+=(LONGLONG)dwRetBytes1;  // offset increments for src 


     }

     
	// fp1.Close();             //close destination



     OVERLAPPED ovlp4;	 
	 ovlp4.Offset     = 0; 
     ovlp4.OffsetHigh = 0; 
	 ovlp4.hEvent     = hEvents[4];
	 OVERLAPPED ovlp5;	 
	 ovlp5.Offset     = 0; 
     ovlp5.OffsetHigh = 0; 
	 ovlp5.hEvent     = hEvents[5];

	
     	bResult_unlock = DeviceIoControl(h_MyDevice_src2,FSCTL_UNLOCK_VOLUME,NULL,NULL,NULL,NULL,&BytesReturned,&ovlp4);

	   if(!bResult_unlock)
	   {
	   printf("UnLock Volume Failed %d \n",GetLastError());
	   }
			

	   bResult_unlock1 = DeviceIoControl(h_MyDevice_dest2,FSCTL_UNLOCK_VOLUME,NULL,NULL,NULL,NULL,&BytesReturned,&ovlp5);

		if(!bResult_unlock1)
		{
		printf("UnLock Volume Failed %d \n",GetLastError());
		}
			

		  //  CancelIo(hEvents[0]);
		//	CancelIo(hEvents[1]);
		//	CancelIo(hEvents[2]);
		//	CancelIo(hEvents[3]);
		//	CancelIo(hEvents[4]);
		//	CancelIo(hEvents[5]);


     CloseHandle(h_MyDevice_src2); //close source
	 CloseHandle(h_MyDevice_dest2);//close destination

	 cout<<"\n Press any key to continue...........\n\n";
	 char c=getchar();

With regards Krish (krishnampkkm@gmail.com)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.