title: Reading bytes from serial port C++ Windows
body: <p>Hey I am trying to interface with the xbees I have connected to my windows machine. I am able to write to the end device through the coordinator in AT mode, and can see the data streamed to my XCTU console. However, I am having trouble understanding how to read that incoming data.</p>
  
  <p>The code I am currently using is below. Essentially the only part that is crucial is the last 5 lines or so (Specifically the read and write file lines), but I am going to post all of it just to be thorough. How do I read the data I sent to the xbee over the com port? The data I sent was simply 0x00-0x0F.</p>
  
  <p>I think I am misunderstanding how the read file functions. I am assuming that the bits I send to the xbee is stored in a buffer which can than be read one at a time. Is that correct? Or do I need to write the entire byte than read the data available? Im sorry if my train of though is confusing, I am fairly new to serial communication. Any help is appreciated.</p>
  
  <pre><code>#include &lt;cstdlib&gt;
  #include &lt;windows.h&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  /*
   * 
   */
  int main(int argc, char** argv) {
      int n = 8; // Amount of Bytes to Read
      HANDLE hSerial;
      HANDLE hSerial2;
      hSerial = CreateFile("COM3",GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);// dont need to GENERIC _ WRITE
      hSerial2 = CreateFile("COM4",GENERIC_READ,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);// dont need to GENERIC _ WRITE
      if(hSerial==INVALID_HANDLE_VALUE || hSerial2==INVALID_HANDLE_VALUE){
          if(GetLastError()==ERROR_FILE_NOT_FOUND){
  //serial port does not exist. Inform user.
      cout &lt;&lt; "Serial port error, does not exist" &lt;&lt; endl;
      }
  //some other error occurred. Inform user.
      cout &lt;&lt; "Serial port probably in use" &lt;&lt; endl;
      }
  
      DCB dcbSerialParams = {0};
      dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
      if (!GetCommState(hSerial, &amp;dcbSerialParams)) {
          cout &lt;&lt; "error getting state" &lt;&lt; endl;
      }
      dcbSerialParams.BaudRate=CBR_9600;
      dcbSerialParams.ByteSize=8;
      dcbSerialParams.StopBits=ONESTOPBIT;
      dcbSerialParams.Parity=NOPARITY;
      if(!SetCommState(hSerial, &amp;dcbSerialParams)){
          cout &lt;&lt; "error setting serial port state" &lt;&lt; endl;
  
      }
  
      COMMTIMEOUTS timeouts = {0};
  
      timeouts.ReadIntervalTimeout = 50;
      timeouts.ReadTotalTimeoutConstant = 50;
      timeouts.ReadTotalTimeoutMultiplier =10;
      timeouts.WriteTotalTimeoutConstant = 50;
      timeouts.WriteTotalTimeoutMultiplier = 10;
  
      if (!SetCommTimeouts(hSerial, &amp;timeouts)){
          cout &lt;&lt; "Error occurred" &lt;&lt; endl;
      }
  
      DWORD dwBytesWritten = 0;
      DWORD dwBytesRead = 0;
      unsigned char oneChar;
      for (int i=0; i&lt;16; i++)
          {
            oneChar=0x00+i;
            WriteFile(hSerial, (LPCVOID)&amp;oneChar, 1, &amp;dwBytesWritten, NULL);
            ReadFile (hSerial2, &amp;oneChar, 1, &amp;dwBytesRead, NULL); // what I tried to do, just outputs white space
          }
  
      CloseHandle(hSerial);
  
  
  
      return 0;
  }
  </code></pre>