Search code examples
c++winapiserial-portreadfile

Unable to read from serial port. WaitCommEvent() never returns


I have to read bytes from serial port using win32 api but have not connected any device to the port to test so i write a byte in the port and trying to read but WaitCommEvent() never returns and program remains in waiting state.When i check to see if writing is done , i see is done but the problem is with WaitCommEvent().

 HANDLE hPort;
 TCHAR *pcCommPort = TEXT("COM1");
 hPort = CreateFile( pcCommPort,GENERIC_READ | GENERIC_WRITE, 0,NULL,OPEN_EXISTING,0,NULL);        

 if (hPort == INVALID_HANDLE_VALUE)
    MessageBox( hwnd , L"Error in opening serial port" , L"error" , MB_OK );
 else
    MessageBox( hwnd , L"Port opened" , L"successful" , MB_OK ); //This is displayed


 //Configuration

 DCB conf={0};
 conf.DCBlength = sizeof(conf);

 if(GetCommState(hPort, &conf))
{

  conf.ByteSize = 8;
  conf.Parity = NOPARITY;
  conf.StopBits = ONESTOPBIT;
  conf.fBinary = TRUE;
  conf.fParity = TRUE;

}

else
 MessageBox( hwnd , L"Cannot get comm state" , L"Oops" , MB_OK );

 if(!SetCommState(hPort, &conf))

 {
 MessageBox( hwnd , L"cannot set comm state" , L"Oops" , MB_OK );

 }


 //Timeout
 COMMTIMEOUTS commTimeout;

 if(GetCommTimeouts(hPort, &commTimeout))
 {

   COMMTIMEOUTS timeouts = { 0 };
   timeouts.ReadIntervalTimeout         = 50; 
   timeouts.ReadTotalTimeoutConstant    = 50; 
   timeouts.ReadTotalTimeoutMultiplier  = 10; 

  }

 else
   MessageBox( hwnd , L"cannot get timeout" , L"Oops" , MB_OK );

 if(!SetCommTimeouts(hPort, &commTimeout))
    MessageBox( hwnd , L"cannot set timeout" , L"Oops" , MB_OK );


 //Writing
 char lpBuffer[] = "a";
 DWORD dNoOFBytestoWrite;       
 DWORD dNoOfBytesWritten = 0;     
 dNoOFBytestoWrite = sizeof(lpBuffer);

 WriteFile(hPort,lpBuffer,dNoOFBytestoWrite,&dNoOfBytesWritten,NULL);
 if(dNoOfBytesWritten == 1){
    MessageBox(NULL , L"Writing happened" , L"Attention" , MB_OK); //This is displayed
 }


//Reading
DWORD dwEventMask; 
SetCommMask(hPort, EV_RXCHAR);
WaitCommEvent(hPort, &dwEventMask, NULL); 

char TempChar; 
DWORD NoBytesRead;


ReadFile( hPort,&TempChar,sizeof(TempChar),&NoBytesRead, NULL);
CloseHandle(hPort);//Closing the Serial Port

What's wrong? Why can not i read ?

Thanks


Solution

  • I [...] have not connected any device to the port

    A serial port "reads" bytes coming from the connected device. You have no connected device. Therefore no characters will ever come.

    You can only read back the bytes that you yourself wrote if there is a loopback connection (making your computer its own connected device). Some serial ports will support software-configured loopback, but C# doesn't provide any way to control this1. Otherwise you will need a hardware loopback connection (which if you disable hardware handshaking, can be as simple as a single wire)

    A final option would be a virtual serial port driver that connects to another application without involving any hardware at all.


    1 The Win32 API can enable and disable internal loopback, but it's unfortunately still neither standard nor universally supported. See IOCTL_SERIAL_SET_MODEM_CONTROL.