Robert888
asked on
Com Ports
Hi,
I've been using the following code to open and read data from a com port
h = CreateFile("Com1",
GENERIC_READ|GENERIC_WRITE ,
0,NULL,
OPEN_EXISTING,FILE_FLAG_OV ERLAPPED,N ULL);
if(h == INVALID_HANDLE_VALUE) {
PrintError("Failed to open port",ComNumber);
return;
}
***** To read from the port
COMMTIMEOUTS cto = { 2, 1, 1, 0, 0 };
if(!SetCommTimeouts(h,&cto ))
PrintError("SetCommTimeout s failed",ComNumber);
// create event for overlapped I/O
osRead.hEvent = CreateEvent(NULL,FALSE,FAL SE,NULL);
if(osRead.hEvent == INVALID_HANDLE_VALUE)
PrintError("CreateEvent failed", ComNumber);
// wait for received characters
if(!SetCommMask(h,EV_RXCHA R | EV_RLSD))
PrintError("SetCommMask failed", ComNumber);
Finished=FALSE;
while( !Finished ) {
// get the event mask
if( !WaitCommEvent(h,&mask,&os Read) ) {
DWORD e = GetLastError();
if( e == ERROR_IO_PENDING ) {
DWORD r;
if( !GetOverlappedResult(h,&os Read,&r,TR UE) ) {
PrintError("GetOverlappedR esult failed",ComNumber);
break;
}
} else {
PrintError("WaitCommEvent failed", ComNumber);
break;
}
}
// if no event, then UI thread terminated with SetCommMask(h,0)
if( mask == 0 ) {
return ExitThread;
}
if( mask & EV_RLSD)
{
return CDLost;
}
else{
char buf[10];
DWORD read;
do {
memset(buf,'\0',sizeof(buf ));
read = 0;
if( !ReadFile(h,buf,sizeof(buf ),&read,&o sRead) ) {
if( GetLastError() == ERROR_IO_PENDING ) {
if( !GetOverlappedResult(h,&os Read,&read ,TRUE) ) {
PrintError("GetOverlappedR esult failed", ComNumber);
break;
}
} else {
PrintError("ReadFile failed", ComNumber);
break;
}
}
if(read)
{
*** We have some data
All works ok. The routine waits until data is received then reads it in. If their is a change in CD signal this is also recognised.
The problem I have is I wish the GetOverlappedresult function to return after a set time interval. Whether there is any data or not.
I have tried the following
COMMTIMEOUTS cto = { 200, 1, 3000, 0, 0 };
if(!SetCommTimeouts(h,&cto ))
PrintError("SetCommTimeout s failed",ComNumber);
if( !ReadFile(h,buf,MaxCharNum ber,&read, &osRead) ) {
PrintError("ReadFile failed", ComNumber);
return FALSE;
break;
}
if (read)
{
*** We have data
Which works as required. I.E if after 3 seconds no data is received ReadFile returns with read=0, if data is received ReadFile returns straight away with the data.
But. If i call ReadFile again it returns straight away even when there is no data, How do I get it to perform as it does the first time i called ReadFile.
Or can I change the first example code so that GetOverLappedResult returns after a set interval.
I've been using the following code to open and read data from a com port
h = CreateFile("Com1",
GENERIC_READ|GENERIC_WRITE
0,NULL,
OPEN_EXISTING,FILE_FLAG_OV
if(h == INVALID_HANDLE_VALUE) {
PrintError("Failed to open port",ComNumber);
return;
}
***** To read from the port
COMMTIMEOUTS cto = { 2, 1, 1, 0, 0 };
if(!SetCommTimeouts(h,&cto
PrintError("SetCommTimeout
// create event for overlapped I/O
osRead.hEvent = CreateEvent(NULL,FALSE,FAL
if(osRead.hEvent == INVALID_HANDLE_VALUE)
PrintError("CreateEvent failed", ComNumber);
// wait for received characters
if(!SetCommMask(h,EV_RXCHA
PrintError("SetCommMask failed", ComNumber);
Finished=FALSE;
while( !Finished ) {
// get the event mask
if( !WaitCommEvent(h,&mask,&os
DWORD e = GetLastError();
if( e == ERROR_IO_PENDING ) {
DWORD r;
if( !GetOverlappedResult(h,&os
PrintError("GetOverlappedR
break;
}
} else {
PrintError("WaitCommEvent failed", ComNumber);
break;
}
}
// if no event, then UI thread terminated with SetCommMask(h,0)
if( mask == 0 ) {
return ExitThread;
}
if( mask & EV_RLSD)
{
return CDLost;
}
else{
char buf[10];
DWORD read;
do {
memset(buf,'\0',sizeof(buf
read = 0;
if( !ReadFile(h,buf,sizeof(buf
if( GetLastError() == ERROR_IO_PENDING ) {
if( !GetOverlappedResult(h,&os
PrintError("GetOverlappedR
break;
}
} else {
PrintError("ReadFile failed", ComNumber);
break;
}
}
if(read)
{
*** We have some data
All works ok. The routine waits until data is received then reads it in. If their is a change in CD signal this is also recognised.
The problem I have is I wish the GetOverlappedresult function to return after a set time interval. Whether there is any data or not.
I have tried the following
COMMTIMEOUTS cto = { 200, 1, 3000, 0, 0 };
if(!SetCommTimeouts(h,&cto
PrintError("SetCommTimeout
if( !ReadFile(h,buf,MaxCharNum
PrintError("ReadFile failed", ComNumber);
return FALSE;
break;
}
if (read)
{
*** We have data
Which works as required. I.E if after 3 seconds no data is received ReadFile returns with read=0, if data is received ReadFile returns straight away with the data.
But. If i call ReadFile again it returns straight away even when there is no data, How do I get it to perform as it does the first time i called ReadFile.
Or can I change the first example code so that GetOverLappedResult returns after a set interval.
Can you post source code with ReadFile and comments for what ReadFile(), GetOverlappedResult(), GetLastError() returns in each case?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.