Problem using ReadFile/WriteFile in Overlapped I/O

N

nk28

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. Should I place the read & write in different treads?.I am using the
following code:

int main()
{ 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
);

fSuccess = GetCommState(hCom, &dcb);

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

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

fSuccess = SetCommMask(hCom, EV_RXCHAR | EV_TXEMPTY);
}

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 (!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.
fRes = TRUE;
}
}
}
else
{
// WriteFile completed immediately.
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);

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

Mike Warren

nk28 said:
I am making a C program that accesses the COM port for reading &
writing AT commands to my mobile phone.


You will probably have more luck asking this in a C/C++ group.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top