Serial communication via C++

I'm making a program currently to control my arduino through C++ via xbox controller/command box in a visual program. It works 100% perfect except for one thing, the data it recieves from the arduino's Serial library is always extremely malformed. It's always at the beginning/end of the sentence.

Usually it appends "80" to the beginning, and for example when I do this:

Serial.write("This is a long string to test.");

it gives me this:

80This is a long string to test.^A

(The character at the end was actually one character, not ^A, but the forums do not support it). In any case, I'm not sure what to do. print, println and write all do this for me but I don't know why. I'd really appreciate any help with this

Gonna need to see the XBox source code.

Xbox is not involved in the recieving of serial at all :). I’m using a class called ‘CSerial’ I found on codeguru here http://www.codeguru.com/cpp/i-n/network/serialcommunications/article.php/c2503/

int CSerial::ReadData( void *buffer, int limit )
{

      if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );

      bool bReadStatus;
      DWORD dwBytesRead, dwErrorFlags;
      COMSTAT ComStat;

      ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );
      if( !ComStat.cbInQue ) return( 0 );

      dwBytesRead = (DWORD) ComStat.cbInQue;
      if( limit < (int) dwBytesRead ) dwBytesRead = (DWORD) limit;

      bReadStatus = ReadFile( m_hIDComDev, buffer, dwBytesRead, &dwBytesRead, &m_OverlappedRead );
      if( !bReadStatus )
      {
            if( GetLastError() == ERROR_IO_PENDING )
            {
                  WaitForSingleObject( m_OverlappedRead.hEvent, 2000 );
                  return( (int) dwBytesRead );
            }
            
            return( 0 );
      }

      return( (int) dwBytesRead );

}

is the recieve function, and I’m getting it like this

      if ( Arduino->ReadDataWaiting() > 0 )
      {
            char buffer[40];

            Arduino->ReadData( (void*)buffer, 40 );

            std::string CleanString = buffer;

            //was trying to see if I could just edit out the clear errors here
            
            ConsoleText->insert( "\t" );
            ConsoleText->insert( CleanString.c_str() );
            ConsoleText->insert( "\n" );
      }

It’s odd, because the error is always the same for each string of text. pong will always be 80pong^A etc.

Try this...

      if ( Arduino->ReadDataWaiting() > 0 )
      {
            char buffer[40];

            [glow]int nRead = [/glow]Arduino->ReadData( (void*)buffer, [glow]39[/glow] );

            [glow]buffer[nRead] = 0;[/glow]

            std::string CleanString = buffer;

            //was trying to see if I could just edit out the clear errors here

            ConsoleText->insert( "\t" );
            ConsoleText->insert( CleanString.c_str() );
            ConsoleText->insert( "\n" );
      }

And please report back what you find.

Thanks, that worked perfectly with Serial.print, so all I needed to add was

CleanString.erase(0, 2);

where I was doing the string cleanup, and it's good to go. Thanks for the help :). I'll make sure to post the program when it's done!

Is "80" still at the front of the received string?