title: Weird characters in COM Serial Port echo of HELLO WORLD
body: <p>I have code that sends <code>HELLO WORLD:$</code> from my PC to COM6 to a TIVAC board. I have confirmed through IAR that the board receives the right message. Note that <code>$</code> is the terminating character.</p>
  
  <p>I have it set up on the TIVAC board to echo the same message through UART and have confirmed manually through Putty that the echo is correct. However when using this following program which sends the same message and listens for the echo I get weird characters in the echo as shown in this image:</p>
  
  <p><img src="https://i.stack.imgur.com/WNm8T.png" alt=""></p>
  
  <p>It might be an error in the encoding but how do I fix that?</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;windows.h&gt;
  #include &lt;conio.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;io.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;iostream&gt;
  #include &lt;winbase.h&gt;
  #include &lt;tchar.h&gt;
  
  HANDLE GetSerialPort(char *);
  void delay();
  int main(void)
  {
      //
      COMMTIMEOUTS timeouts;
  
  
      HANDLE h1;
      char h1_buffer[] = {"HELLO WORLD:$"};
      char h2_buffer[24];
      DWORD byteswritten, bytesread;
      char c1[] = {"COM6"};
      char c2[] = {"COM6"};
      h1 = GetSerialPort(c1);
  
      timeouts.ReadIntervalTimeout = 1;
      timeouts.ReadTotalTimeoutMultiplier = 1;
      timeouts.ReadTotalTimeoutConstant = 1;
      timeouts.WriteTotalTimeoutMultiplier = 1;
      timeouts.WriteTotalTimeoutConstant = 1;
      WriteFile(h1, h1_buffer, strlen(h1_buffer), &amp;byteswritten, NULL);
      do
      {
          bool exit = FALSE;
  
          ReadFile(h1, h2_buffer, strlen(h2_buffer) + 1, &amp;bytesread, NULL);
  
          if(bytesread)
          {
              h2_buffer[strlen(h2_buffer)] = '\0';
              std::string mystring(h2_buffer);
              std::cout &lt;&lt; "String is  : " &lt;&lt; mystring &lt;&lt; "\n" ;
              printf("GOT IT %d\n", strlen(h2_buffer));
              ReadFile(h1, h2_buffer, strlen(h2_buffer) + 1, &amp;bytesread, NULL);
              printf("%s\n", h2_buffer);
              printf("GOT IT %d\n", strlen(h2_buffer));
          }
          else
          {    
              char stop;
              printf("Nothing read\n");
              printf("Do you want to exit? ");
              scanf(" %c", stop);
              if(stop == 'N' || stop == 'n')
              {
                  exit = TRUE;
              }
  
          }
      }while(1);
      printf("EXIT ");
      CloseHandle(h1);
  }
  HANDLE GetSerialPort(char *p)
  {
      HANDLE hSerial;
      hSerial = CreateFile(p,GENERIC_READ | GENERIC_WRITE, 0,0,OPEN_EXISTING,0, 0);
  
      DCB dcbSerialParams = {0};
      dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
      dcbSerialParams.BaudRate=CBR_115200;
      dcbSerialParams.StopBits=ONESTOPBIT;
      dcbSerialParams.Parity=NOPARITY;
      dcbSerialParams.fParity = 0;
      dcbSerialParams.ByteSize=DATABITS_8;
      dcbSerialParams.fDtrControl = 0;
      dcbSerialParams.fRtsControl = 0;
  
      return hSerial;
  }
  void delay ()
  {
     int i = 1000000000;
     printf("In delay\n");
     while(i&gt;0)
     {
         i--;
     }
  }
  </code></pre>