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);
}