[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Problem with Readfile() call during COM communication.

Posted on 2006-07-11
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 345 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.

649 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