?
Solved

Serial read not working

Posted on 2006-04-13
3
Medium Priority
?
678 Views
Last Modified: 2008-02-01
I have some code that I wrote for Serial communication through a com port which has always worked for reading from and writing to a com port.  Now I am trying to communicate with 2 different com ports from 2 different threads and I'm having problems.  I making a test program to write to one com port (COM1)  and I want to see how long it takes to receive it from the other com port (COM7).  I am successfully writing to COM1 but I'm not receiving it at COM7.  My serial read and write functions look like:
BOOL CSerialComm::ReadCommBlock(LPSTR lpszBlock,int nMaxLength )
{
      DWORD dwLength      = 0;
      DWORD dwRead;
      OVERLAPPED osReader      = {0};

      if (m_bComOpen == true)
      {
            osReader.hEvent      = CreateEvent(NULL, TRUE, FALSE, NULL);
            if (osReader.hEvent == NULL)
            {
                  CloseHandle(osReader.hEvent);
                  return FALSE;
            }
            if (!ReadFile(hComm, lpszBlock, nMaxLength, &dwRead, &osReader))
            {
                  if (GetLastError()!= ERROR_IO_PENDING)      //read not delayed?
                  {      //Error in communication
                        CloseHandle(osReader.hEvent);
                        return FALSE;
                  }
            }
            else
            {      // read completed immediately
                  if (dwRead < 1)
                  {
                        CloseHandle(osReader.hEvent);
                        return FALSE;
                  }
                  else
                  {
                        CloseHandle(osReader.hEvent);
                        return TRUE;
                  }
            }
            CloseHandle(osReader.hEvent);
      }
      return FALSE;
} // end of ReadCommBlock()
BOOL CSerialComm::WriteCommBlock(LPSTR lpszBlock , DWORD dwBytesToWrite)
{
      OVERLAPPED osWrite      = {0};
      DWORD dwWritten;
      DWORD dwRes;
      bool bFlag;

      if (m_bComOpen == true)
      {
            // Create this write operation's OVERLAPPED structure's hEvent.
            osWrite.hEvent      = CreateEvent(NULL, TRUE, FALSE, NULL);
            if (osWrite.hEvent == NULL)
            {
                  CloseHandle(osWrite.hEvent);
                  return FALSE;
            }
            //  Issue write
            if (!WriteFile(hComm, lpszBlock, dwBytesToWrite, &dwWritten, &osWrite))
            {
                  if (GetLastError() != ERROR_IO_PENDING)
                  {
                        bFlag      = FALSE;
                  }
                  else // write is pending
                        dwRes      = WaitForSingleObject(osWrite.hEvent, INFINITE);
                  switch (dwRes)
                  {
                        // OVERLAPPED structure's event had been signaled
                  case WAIT_OBJECT_0:
                        if (!GetOverlappedResult(hComm, &osWrite, &dwWritten, FALSE))
                              bFlag      = FALSE;
                        else // Write operation completed successfully
                              bFlag      = TRUE;
                        break;
                  default:
                        bFlag      = FALSE;
                        break;
                  }
            }
            else
                  bFlag      = TRUE;
            CloseHandle(osWrite.hEvent);
      }
      return bFlag;
}

I thought one possibility is that I'm not doing to overlapping right because I wasn't and still not to sure of what I'm doing there.  Can someone help me get this working.  I know that it is possible to send a string through com port and read it on another com port on the same computer because I tried ( successfully ) to send a string throught windows' hyperterminal and receive it in another hyperterminal on the same computer.
0
Comment
Question by:docieb
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 16449171
Why are you using 'OVERLAPPED' in the 1st place if you have multple threads anyway? Also, if you do it, be sure you also use it in your read function, e.g.

BOOL CSerialComm::ReadCommBlock(LPSTR lpszBlock,int nMaxLength )
{
     DWORD dwLength     = 0;
     DWORD dwRead;
     OVERLAPPED osReader     = {0};

     if (m_bComOpen == true)
     {
          osReader.hEvent     = CreateEvent(NULL, TRUE, FALSE, NULL);
          if (osReader.hEvent == NULL)
          {
               CloseHandle(osReader.hEvent);
               return FALSE;
          }
          if (!ReadFile(hComm, lpszBlock, nMaxLength, &dwRead, &osReader))
          {
               if (GetLastError()!= ERROR_IO_PENDING)     //read not delayed?
               {     //Error in communication
                    CloseHandle(osReader.hEvent);
                    return FALSE;
               }
          }
          else
          {     // read completed immediately
               if (dwRead < 1)
               {
                    CloseHandle(osReader.hEvent);
                    return FALSE;
               }
               else
               {

                    dwRes     = WaitForSingleObject(osWrite.hEvent, INFINITE);
                       switch (dwRes)
                       {
                            // OVERLAPPED structure's event had been signaled
                       case WAIT_OBJECT_0:
                            if (!GetOverlappedResult(hComm, &osReader, &dwRead, FALSE))
                                 bFlag     = FALSE;
                            else // Rad operation completed successfully
                                 bFlag     = TRUE;
                            break;
                       default:
                            bFlag     = FALSE;
                            break;
                       }
                    CloseHandle(osReader.hEvent);
                    return TRUE;
               }
          }
          CloseHandle(osReader.hEvent);
     }
     return FALSE;
} // end of ReadCommBlock()
0
 

Author Comment

by:docieb
ID: 16449451
I thought I read somewhere that overlapped is needed when using multiple threads or is that just when multiple threads are accessing the same port.  If so, that makes since to me now.  Overlaping is for waiting for a port to be free.  Ok, well anywho.  I added the overlapping part like you mentioned and it seems to read the string now.
0
 
LVL 86

Expert Comment

by:jkr
ID: 16449508
>>I thought I read somewhere that overlapped is needed when using multiple thread

No, not really. The concept of using 'OVERLAPPED' comes in handy when you have a pool of worker threads. or a highly async scenario. You can always use multiple threads without that.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question