Solved

Problem with Readfile() call during COM communication.

Posted on 2006-07-11
5
477 Views
Last Modified: 2008-03-17
Hey all,

   I'm working on a module for a larger project that is designed to detect the presence of a GPS device attached to a tablet computer.  The GPS may be on any of the COM ports 1-4 (changes depending on exact hardware specs of the tablet but will never be on a com port larger than 4).  It operates at 4800 baud, with 8 data bits, 1 stop bit, no parity, and no handshaking.  The problem I'm having is that, no matter how I set my timeouts, DCB structure, or overlapping, the readfile() call never returns.  If the code is not overlapped (which would be my preference), then the program just hangs, waiting for the readfile() call to return.  If it is overlapped, the call is made, but no events from the call ever occur and no data is ever returned.

The following is the applicable code, in its current incarnation:

            //GPS DETECTION AND TESTING
                  cout << "\n    Detecting GPS . . . ";
                  fputs("\n    Detecting GPS . . . ",auditLog);
                  bool GPSFound = false;
                  HANDLE COMHandle;
                  DCB COMConfig;
                  char COMString[9];
                  char COMBuffer[512];
                  DWORD bytesRead = 0;
                  DWORD COMErrors = 0;
                  COMMTIMEOUTS timeouts;
                  COMSTAT COMStatus;
                  timeouts.ReadIntervalTimeout = 100;
                  timeouts.ReadTotalTimeoutConstant = 0;
                  timeouts.ReadTotalTimeoutMultiplier = 0;
                  timeouts.WriteTotalTimeoutConstant = 100;
                  timeouts.WriteTotalTimeoutMultiplier = 0;
                  for(int i = 1; i <= 4; i++)
                  {
                        sprintf(COMString,"\\\\.\\COM%i\0",i);
                        COMHandle = CreateFile(COMString,GENERIC_READ,0,NULL,OPEN_EXISTING,NULL,NULL);
                        if(COMHandle != INVALID_HANDLE_VALUE)
                        {
                              if(GetCommState(COMHandle,&COMConfig) && SetCommTimeouts(COMHandle,&timeouts))
                              {
                                    COMConfig.BaudRate = CBR_4800;
                                    COMConfig.fParity = false;
                                    COMConfig.Parity = NOPARITY;
                                    COMConfig.StopBits = ONESTOPBIT;
                                    COMConfig.fBinary = true;
                                    COMConfig.fOutxCtsFlow = false;
                                    COMConfig.fOutxDsrFlow = false;
                                    COMConfig.fDsrSensitivity = false;
                                    COMConfig.fOutX = false;
                                    COMConfig.fInX = false;
                                    COMConfig.fRtsControl = RTS_CONTROL_DISABLE;
                                    COMConfig.fDtrControl = DTR_CONTROL_DISABLE;
                                    COMConfig.fAbortOnError = false;
                                    //if(SetCommTimeouts(COMHandle,&timeouts) && SetCommState(COMHandle,&COMConfig))
                                    if(SetCommState(COMHandle,&COMConfig) != 0)
                                    {
                                          cout << "\n        COM Port " << i << " opened successfully!";
                                          //for(int i = 0; i < 1000 && bytesRead == 0; i++)
                                          while(1);
                                          {
                                                ClearCommError(COMHandle,&COMErrors,&COMStatus);
                                                if(COMStatus.cbInQue > 0)
                                                {
                                                      if(ReadFile(COMHandle,&COMBuffer,sizeof(COMBuffer),&bytesRead,NULL))
                                                      {
                                                            cout << "\n        Read:" << COMBuffer;
                                                      }
                                                      else
                                                      {
                                                            cout << "\n        Error:" << GetLastError();
                                                            Sleep(10);
                                                      }
                                                }
                                                else
                                                {
                                                      cout << "\n        No data to read.";
                                                }
                                          }
                                    }
                              }
                              else
                              {
                                    MessageBox(NULL,"ERROR SETTING TIMEOUTS/CONFIG INFO","ERROR",NULL);
                              }
                        }
                        else
                        {
                              cout << "\n        Error opening COM" << i;
                        }
                        CloseHandle(COMHandle);
                  }
            //END GPS DETECTION AND TESTING


I've tried everything I can think of: I've spent hours fiddling with the configuration and timeout settings, I've tried making overlapped calls, I've tried using "FILE_FLAG_NO_BUFFER" and "FILE_FLAG_WRITE_THROUGH", I've tried checking that there's data to read before making the call (the checking of COMStatus.cbInQue).  All to no avail.  Anyone see what I'm doing wrong, or have any suggestions?  Any help at all would be much appreciated.
0
Comment
Question by:The_Tarquin
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 17087254
Try tro set your port settings using

DCB COMConfig;
char* cfg = ""baud=4800 parity=N data=8 stop=1";

BuildCommDCB(cfg,&COMConfig);

SetCommState(COMHandle,&COMConfig);
0
 
LVL 22

Accepted Solution

by:
grg99 earned 115 total points
ID: 17089298
You might have better luck if you first poll all the COM ports for the presence of DTR.  Most serial devices pull this up.

0
 

Author Comment

by:The_Tarquin
ID: 17095175
Thank you both for your responses.  I tried your method, jkr and the problem remained unchanged.  

grg99 - Do you happen to know how to poll the DTR flag in C++?  I did a cursory google search for it and I didn't turn up anything.

Thanks again.
0
 
LVL 4

Expert Comment

by:havman56
ID: 17097314


i have hosted serial communciation code in my website u can see it and use it for free.

it works fine

http://micro_mangal.tripod.com 

4 files are there copy and paste into respective filenames and use it
0
 

Author Comment

by:The_Tarquin
ID: 17104032
grg99 - Working from your suggestion I was able to poke around and work out that I needed to use WaitCommEvent() to poll the DTR flag.  It's working great now.  Thanks!
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

810 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