Link to home
Start Free TrialLog in
Avatar of The_Tarquin
The_Tarquin

asked on

Problem with Readfile() call during COM communication.

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.
Avatar of jkr
jkr
Flag of Germany image

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);
ASKER CERTIFIED SOLUTION
Avatar of grg99
grg99

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of The_Tarquin
The_Tarquin

ASKER

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.


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
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!