GIDForums  

Go Back   GIDForums > Computer Programming Forums > MS Visual C++ / MFC Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Jul-2005, 03:52
chirayu chirayu is offline
New Member
 
Join Date: Jun 2005
Location: Pune
Posts: 2
chirayu is on a distinguished road

Serial port problem


Hi ,
I am reading serial port in a loop. using this code.

CPP / C++ / C Code:
BOOL CSerialPort::Read(void* lpBuf, 1, OVERLAPPED& overlapped)
{
  DWORD dwBytesRead = 0;
  BOOL fRes=TRUE;
  
  BOOL bSuccess = ReadFile(m_hComm, lpBuf, 1, &dwBytesRead, &overlapped);
  if (!bSuccess)
  {
    if (GetLastError() != ERROR_IO_PENDING)
    {
      TRACE(_T("Failed in call to ReadFile\n"));
      AfxThrowSerialException();
    }
	else
    {
        DWORD dwRes = WaitForSingleObject(overlapped.hEvent, 15);
        // Write is pending.
        switch(dwRes)
        {
            // OVERLAPPED structure's event has been signaled. 
            case WAIT_OBJECT_0:
                if (!::GetOverlappedResult(m_hComm, &overlapped, &dwBytesRead, TRUE))
                {
                    fRes = FALSE;
                }
                else
                {
                    // Read operation completed successfully.
                    return TRUE;
                }
            break;
            case WAIT_TIMEOUT:
                {
                    CloseHandle(overlapped.hEvent);
                    return FALSE;
                }
                    
            default:
                // An error has occurred in WaitForSingleObject.
                // This usually indicates a problem with the
                // OVERLAPPED structure's event handle.
                fRes = FALSE;
                break;
        }
      }
  }
  return bSuccess;
}

This is a standard serial communication code found everywhere.I have to read the port till I receive 0xA char on the port from a hardware connected to it . So this read call i have put in a Do-While loop.
I send some command to my hardware which responds immediately in 8-10ms.
My intention is to wait for maximum 15ms.But the problem is i still get timeout from waitforsingleobject(..) even though hardware has sent the data on port. No byte is read in that 15ms.However if i increase the timeout to say 35ms in waitforsingleobject it works ok.
Can anyone please tell me why is this happening or what should i look for?

Thanks in advance
Chirayu
Last edited by LuciWiz : 07-Jul-2005 at 04:59. Reason: Please insert your C++ code between [c++] & [/c++] tags
 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 12:31
Please help! Socket problem robsmith C Programming Language 1 09-May-2005 01:03
Problem to read on a serial port collinm C Programming Language 2 30-Mar-2005 01:42
Problem with string and serial port collinm C Programming Language 13 25-Mar-2005 07:39
An ActiveX Control does not work in VB, what problem would that be? mingcosp MS Visual C++ / MFC Forum 0 19-Jan-2005 10:04

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:17.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.