Arduino to Arduino via Serial

I’m trying to communicate from one Arduino to another via Serial. I have my boards talking to each other, but the the data received is often mixed up or incomplete. Any ideas would be of great help. :slight_smile:

if(Serial3.available() >= 10)
  {
    String msg = 0;
    String x;
    for(int i = 0; i < 10; i++)
    {
      msg += char(Serial3.read());
    }
    
    if(msg.equals(x))
    {
    }
  
     Serial.print("msg :");
     Serial.println(msg);

Why wait for a certain number of characters? Why not just send them all at once.

Don't use String.

Learn the difference between Serial.write() and Serial.print().

Is it possible the sender is transmitting more characters than the receiver is reading so that the buffer overflows and stuff gets lost?

...R

How did you hook things up ?

Did you remember to connect the grounds of the two arduinos?

Slee_The_Sloth: the the data received is often mixed up or incomplete

Even if the serial reception is perfect, the code you posted will only output ten character chunks and will insert line feeds between them so what you get out won't exactly match what you type in - but should relate fairly obviously (if the serial link is reliable). If you can post a few examples of what was sent and what was received, it might be possible to see whether there is any pattern in the corruption.

By the way, is there some reason you aren't just echoing each character received directly?

while(Serial1.available() > 0)
{
    Serial.write(Serial1.read());
}

I'm relay new to serial communications. I know a little a bought the world of Arduino. So I'm learning a lot. However, I'm certainly open to new ways of doing things. My end goal is to send a code or string from one Arduino mega in the body of my robot to the Arduino mega in the head and then have the Arduino mega turn on a relay or something of that nature. So if there is a better way better code I'm open to it. However I will say that for some reason the I2C will not work. One of the boards is buried deep in my project.

example of data sent: Sean_Moore ( I used my name for lack of something better as a test. )

Data received:

0 Sen_ Sean_moo reSean_Moor eSean_moore Sean_Moore

and so on. I really just need to send something a numbered code or text from one Arduino to the second then chek the recived data in an if statement like:

if (msg = "2490")
    {
     digitalwrite(39, HIGH);
    }

or

if (msg = "relay6_on")
    {
     digitalwrite(39, HIGH);
    }

Thanks for the help. :)

if (msg = "relay6_on")

= is an assignment == is a comparison

And just to be clear if you are using the "[u]S[/u]tring" object, then == will work. However if you are use the standard c-style "[u]s[/u]trings", then you need to be using strcmp().

http://arduino.cc/en/Tutorial/StringComparisonOperators

Lastly, if the communication is between two devices, there is NO reason to send entire "strings." Just send single bytes for commands.

ok now I'm getting the same data I sent as received. But I can not compare it.

 while(Serial3.available() > 0)
{

   const char msg = Serial3.read();
  //  Serial.write(Serial3.read());
    Serial.write(msg);
      /// test buton 5 switch A ON /////

    if (msg == 'Seanxxxxxx')
       {
        digitalWrite(46, LOW); // turn test relay on.
        delay(100);
       }
    if (msg == 'Elsiexxxxx' )
       {
       digitalWrite(46, HIGH); // turn test relay on.
       delay(100);
       }

One simple way to capture characters sent via serial into a String, then evaluate the String contents.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

//A very simple example of sending a string of characters 
//from the serial monitor, capturing the individual 
//characters into a String, then evaluating the contents 
//of the String to possibly perform an action (on/off board LED).

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if(readString.indexOf("on") >=0)
    {
      digitalWrite(ledPin, HIGH);
    }

    if(readString.indexOf("off") >=0)
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  } 
}

Thank you all for your help. We got it to work now. Thanks!