Problem with Readfile() call during COM communication.

Posted on 2006-07-11
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:

                  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++)
                        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++)
                                                if(COMStatus.cbInQue > 0)
                                                            cout << "\n        Read:" << COMBuffer;
                                                            cout << "\n        Error:" << GetLastError();
                                                      cout << "\n        No data to read.";
                                    MessageBox(NULL,"ERROR SETTING TIMEOUTS/CONFIG INFO","ERROR",NULL);
                              cout << "\n        Error opening COM" << i;

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.
Question by:The_Tarquin
LVL 86

Expert Comment

ID: 17087254
Try tro set your port settings using

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


LVL 22

Accepted Solution

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.


Author Comment

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.

Expert Comment

ID: 17097314

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

it works fine

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

Author Comment

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!

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now