Serial message posting garbage between data

So rather strange thing i havent come across before in my noobness…im writing a serial wrapper for sending data with handshakes and integrity awareness and so on between a c# application and a Mega 2560.

The data is supposed to look like this: (Without quotes)
“1.54,-98.25,342.02” -

But as soon as it gets to the arduino it looks like this with the garbage decreasing the higher the baud rate: (Without quotes)

“??1.5?4,-?98.?25,?34?2.0?2” - (25000 Baud)

“???1??.??5??4??,???-??9??8??.??2??5??,???3??4??2??.??0??2??” - (115200 Baud)

9600 was too long to post. but they all still have all their data in the middle of all that.

This seems to be a Serial issue but just in case, this is the arduino code:

String DataTransfer::RequestData(char command)
{
 Serial.write(command);
 while (true)
 {
 String response = "";
 if (Serial.read() == command)
 {
 while (true)
 {
 char c = Serial.read();
 if (c == '!')
 {
 break;
 }
 response += c;
 }
 Serial.print('<');
 Serial.print(response);
 Serial.print('>');
 while (true)
 {
 if (Serial.read() == '+')
 {
 return response;
 }
 else if (Serial.read() == '-')
 {
 return "";
 }
 else {
 return"EXCEPTION";
 }
 }
 }
 }
}

and this is the C# code:

private static void SerialRequestListener()
        {
            while (true)
            {
                if (SerialPort.IsOpen == false)
                {
                    try
                    {
                        //SerialPort.DtrEnable = true;
                        SerialPort.Open();
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
                if (SerialPort.BytesToRead > 0)
                {
                    if (SerialPort.ReadByte() == '

Oh and data is Strings on both sides…chars appeneded to the end of it in the loops above.

Please for the love of all things good help put my weary mind at ease :):):slight_smile:

)
                   {
                       String message = _orientationString;
                       SerialPort.Write("$");
                       if (message == null)
                           message = “null”;
                       SerialPort.Write(message);
                       SerialPort.Write("!");
                       String echo = “”;

int echoCounter = 0;
                       while (true)
                       {
                           echoCounter++;
                           if (echoCounter > message.Length + 2)
                               break;                                      //Watchdog
                           if (SerialPort.ReadByte() == ‘<’)
                           {
                               while (true)
                               {
                                   char c = (char)SerialPort.ReadChar();
                                   if (c == ‘>’)
                                   {
                                       break;
                                   }
                                   echo += c;
                               }
                           }
                       }
                       if (echo == message)
                       {
                           SerialPort.Write("+");
                       }
                       else if (echo != message)
                       {
                           SerialPort.Write("-");
                       }
                   }
               }
           }
       }


Oh and data is Strings on both sides...chars appeneded to the end of it in the loops above.

Please for the love of all things good help put my weary mind at ease :):):)

I don't see you doing a serial.available() anywhere.

ahh I thought it wasn’t necessary if I was just pulling starting and ending characters out of the buffer instead…I will change it but that wouldn’t have anything to do with the problem would it?

Thank you Graynomad! i woke up and read your message again and realised that i was probably pulling null from the serial buffer and thats what the garbage was! much thanks!!

Maybe the 3rd example in Serial Input Basics would be helpful - simple, reliable and non-blocking.

It is not a good idea to use the String (capital S) class in the small memory of an Arduino - it can cause memory corruption. Just use cstrings - char arrays terminated with 0.

Your PC program should only open the serial port once, wait for the Arduino to reset, and keep it open until it is completely finished with the Arduino.

...R

i woke up and read your message again and realised that i was probably pulling null from the serial buffer and thats what the garbage was!

A null is a stop sign. Nulls are never sent. So, that is not the problem. Reading data that has not arrived IS a problem, if you ASSume that you got good data.

Since you are sending start of packet and end of packet markers, you really should read Robin2's link.