[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem with Readfile() call during COM communication.

Posted on 2006-07-11
5
Medium Priority
?
494 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 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.

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
Suggested Courses

872 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