title: Data loss via serial port (RS232)
body: <p>I am trying to read a series of data via serial port. Below are my test scenarios:</p>
  
  <p><strong><em>I have a sender (POS terminal) and a receiver (dll) on windows 10. Both connected via RS232 cable (9 pins).</em></strong></p>
  
  <ol>
  <li><p>The sender writes 16 bytes of data at one time. The receiver is able to read the data, 1 byte per read, by looping 16 times.</p></li>
  <li><p>The sender writes 114 bytes of data at one time. The receiver is able to read the data , 1 byte per read, by looping 114 times.</p></li>
  <li><p>The sender writes 115 bytes of data at one time. The receiver is able to read the data, 1 byte per read, by looping 115 times. But the last character is lost. In other words, the last character seems to be corrupted.</p></li>
  </ol>
  
  <p>E.g. Data written: <strong>ABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNO</strong></p>
  
  <p>I am able to get "<strong>ABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMN</strong>" correctly. But the last character <strong>'O'</strong> is corrupted. Instead the data read in place of last character is (ASCII value) 205, 255, 234 etc. The data is not consistent during multiple read attempts.</p>
  
  <p>Can anyone please put some light on this? Can anyone guide me if I am missing something-somewhere?</p>
  
  <p>I referred this article for my project: <a href="https://msdn.microsoft.com/en-us/library/aa450580.aspx" rel="nofollow noreferrer">Serial Port Communication</a>
  <code>
  Below is the code snippet:
  </code></p>
  
  <pre><code>//Port opening.
  HANDLE hPortDailUp = CreateFile(portfinal,
              GENERIC_READ | GENERIC_WRITE,
              0,
              NULL,
              OPEN_ALWAYS,
              0,
              NULL);
  
  //Setting COM timeouts.
  SetCommMask(hPortDailUp, 0);
  SetupComm(hPortDailUp, 2048, 2048);
  GetCommTimeouts (hPortDailUp, &amp;commTimeOuts);
  commTimeOuts.ReadIntervalTimeout = 100;
  commTimeOuts.ReadTotalTimeoutMultiplier = 70;
  commTimeOuts.ReadTotalTimeoutConstant = 1000; //1000
  commTimeOuts.WriteTotalTimeoutMultiplier = 0;
  commTimeOuts.WriteTotalTimeoutConstant = 0;
  SetCommTimeouts(hPortDailUp, &amp;commTimeOuts);
  
  //Setting port configuration.
  DCB PortDCB;
  PortDCB.DCBlength = sizeof(DCB);
  
  PortDCB.BaudRate = BaudRate;
  PortDCB.ByteSize = 8;
  PortDCB.Parity = NOPARITY;
  PortDCB.StopBits = ONESTOPBIT;
  PortDCB.fOutxCtsFlow = RTS_CONTROL_DISABLE;
  PortDCB.fRtsControl = RTS_CONTROL_DISABLE;
  SetCommState(hPortDailUp, &amp;PortDCB)
  
  //Reading data from serial port.
  unsigned char tempRecvBuf[3001] = {'\0'};
  ReadFile(hPortDailUp, tempRecvBuf + nBytesRead, 1, &amp;TempLen, NULL);
  
  //PortDCB Value at runtime.
  PortDCB {DCBlength=28 BaudRate=115200 fBinary=1 ...}    _DCB
      DCBlength           28          unsigned long
      BaudRate            115200      unsigned long
      fBinary             1           unsigned long
      fParity             0           unsigned long
      fOutxCtsFlow        0           unsigned long
      fOutxDsrFlow        0           unsigned long
      fDtrControl         1           unsigned long
      fDsrSensitivity     0           unsigned long
      fTXContinueOnXoff   0           unsigned long
      fOutX               0           unsigned long
      fInX                0           unsigned long
      fErrorChar          0           unsigned long
      fNull               0           unsigned long
      fRtsControl         0           unsigned long
      fAbortOnError       0           unsigned long
      fDummy2             0           unsigned long
      wReserved           0           unsigned short
      XonLim              2048        unsigned short
      XoffLim             512         unsigned short
      ByteSize            8 '\b'      unsigned char
      Parity              0 '\0'      unsigned char
      StopBits            0 '\0'      unsigned char
      XonChar             17 '\x11'   char
      XoffChar            19 '\x13'   char
      ErrorChar           0 '\0'      char
      EofChar             0 '\0'      char
      EvtChar             0 '\0'      char
      wReserved1          0           unsigned short
  </code></pre>