title: Unable to read write on serial port in C++
body: <p>I am writing a serial communication code in c++ on Visual Studio 2010. For this I <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa363201%28v=vs.85%29.aspx" rel="nofollow noreferrer">try this</a> example, which is working fine. Now, I try to send a string and read it back by shorting Rx and Tx pins of my hardware. Here is my code for sending string:-</p>
  
  <pre><code>char *buffer ="hello";
     DWORD bytesSend;
     DWORD errors;
     COMSTAT status;
     DWORD numWritten;
     WriteFile(hCom, buffer, strlen(buffer), &amp;numWritten, NULL); 
     printf("send succesfully\n");
  </code></pre>
  
  <p>and here is my receiving code:-</p>
  
  <pre><code>DWORD numRead;
  
    BOOL ret = ReadFile(hCom, buffer, 5, &amp;numRead, NULL);
  
      if(!ret)
      {
          printf("Read Fail\n");
      }
      else
      {
          printf("%s\n",buffer);
      }
  </code></pre>
  
  <p>On running my code, receive is getting failed. So, please tell me where is the problem. here is my complete code.</p>
  
  <pre><code>#include "stdafx.h"
  
  #include &lt;windows.h&gt;
  #include &lt;tchar.h&gt;
  #include &lt;stdio.h&gt;
  
  void PrintCommState(DCB dcb)
  {
      //  Print some of the DCB structure values
      _tprintf( TEXT("\nBaudRate = %d, ByteSize = %d, Parity = %d, StopBits = %d\n"), 
                dcb.BaudRate, 
                dcb.ByteSize, 
                dcb.Parity,
                dcb.StopBits );
  }
  
  
  int _tmain( )
  {
     DCB dcb;
     HANDLE hCom;
     BOOL fSuccess;
     TCHAR *pcCommPort = TEXT("COM1"); //  Most systems have a COM1 port
  
     //  Open a handle to the specified com port.
     hCom = CreateFile( pcCommPort,
                        GENERIC_READ | GENERIC_WRITE,
                        0,      //  must be opened with exclusive-access
                        NULL,   //  default security attributes
                        OPEN_EXISTING, //  must use OPEN_EXISTING
                        0,      //  not overlapped I/O
                        NULL ); //  hTemplate must be NULL for comm devices
  
     if (hCom == INVALID_HANDLE_VALUE) 
     {
         //  Handle the error.
         printf ("CreateFile failed with error %d.\n", GetLastError());
         return (1);
     }
  
     //  Initialize the DCB structure.
     SecureZeroMemory(&amp;dcb, sizeof(DCB));
     dcb.DCBlength = sizeof(DCB);
  
     //  Build on the current configuration by first retrieving all current
     //  settings.
     fSuccess = GetCommState(hCom, &amp;dcb);
  
     if (!fSuccess) 
     {
        //  Handle the error.
        printf ("GetCommState failed with error %d.\n", GetLastError());
        return (2);
     }
  
     PrintCommState(dcb);       //  Output to console
  
     //  Fill in some DCB values and set the com state: 
     //  57,600 bps, 8 data bits, no parity, and 1 stop bit.
     dcb.BaudRate = CBR_57600;     //  baud rate
     dcb.ByteSize = 8;             //  data size, xmit and rcv
     dcb.Parity   = NOPARITY;      //  parity bit
     dcb.StopBits = ONESTOPBIT;    //  stop bit
  
     fSuccess = SetCommState(hCom, &amp;dcb);
  
     if (!fSuccess) 
     {
        //  Handle the error.
        printf ("SetCommState failed with error %d.\n", GetLastError());
        return (3);
     }
  
     //  Get the comm config again.
     fSuccess = GetCommState(hCom, &amp;dcb);
  
     if (!fSuccess) 
     {
        //  Handle the error.
        printf ("GetCommState failed with error %d.\n", GetLastError());
        return (2);
     }
  
     PrintCommState(dcb);       //  Output to console
  
     _tprintf (TEXT("Serial port %s successfully reconfigured.\n"), pcCommPort);
  
     char *buffer ="hello";
     DWORD bytesSend;
     DWORD errors;
     COMSTAT status;
     DWORD numWritten;
      WriteFile(hCom, buffer, strlen(buffer), &amp;numWritten, NULL); 
      printf("send succesfully\n");
  
    DWORD numRead;
    BOOL ret = ReadFile(hCom, buffer, 5, &amp;numRead, NULL);
  
      if(!ret)
      {
          printf("Read Fail\n");
      }
      else
      {
          printf("%s",buffer);
      }
  
  
  
     return (0);
  }
  </code></pre>
  
  <p>Here is my output:-
  <img src="https://i.stack.imgur.com/QNHjK.png" alt="enter image description here"></p>