Windows XP Community - XPHeads



Problem using ReadFile/WriteFile in Overlapped I/O

microsoft.public.windowsxp.embedded


Reply
  #1 (permalink)  
Old 06-18-2008, 03:51 AM
nk28
 
Posts: n/a
Problem using ReadFile/WriteFile in Overlapped I/O
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);
}

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-19-2008, 12:49 PM
Mike Warren
 
Posts: n/a
Re: Problem using ReadFile/WriteFile in Overlapped I/O
nk28 wrote:

> 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.


--
- Mike
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 11:19 PM.








Design by Vjacheslav Trushkin for phpBBStyles.com.
Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74