Arduino to Arduino Serial Communication

Hi All

I am trying to communicate between an Uno and Mega. Mega is the TX and Uno is the RX. I am simply trying to transmit values of eight int variables separated by commas.

Here is the transmitter code:

int start_time;
int end_time;
int injury_time;
int injury_time_display;
int home_score;
int away_score;
int match_stage;
int menu_stage;

void setup(){
Serial.begin(9600);
}

void loop(){
    Serial.print(start_time);
    Serial.print(",");
    Serial.print(end_time);
    Serial.print(",");
    Serial.print(injury_time);
    Serial.print(",");
    Serial.print(injury_time_display);
    Serial.print(",");
    Serial.print(home_score);
    Serial.print(",");
    Serial.print(away_score);
    Serial.print(",");
    Serial.print(match_stage);
    Serial.print(",");
    Serial.println(menu_stage);

    delay(1000);

I am using the Serial.parseInt() function to split the received values and assign them to respective variables.

The problems I am having are:

  • When I reset the Mega(TX) the order of the variables sent changes and I have to reset the Uno(RX) a few times with the Serial Monitor running to correct the order.
  • A similar problem is that when the code runs for a few minutes the order of the variables sent randomly changes again and I have to physically disconnect and reconnect the serial cable to fix the order.

Could someone please help me with these issue?

Here is the receiver code:

int start_time;
int end_time;
int injury_time;
int injury_time_display;
int home_score;
int away_score;
int match_stage;
int menu_stage;

void setup(){
Serial.begin(9600);
}

void loop()
{
while (Serial.available() > 0) {
   start_time = Serial.parseInt();
   end_time = Serial.parseInt();
   injury_time = Serial.parseInt();
   injury_time_display = Serial.parseInt();
   home_score = Serial.parseInt();
   away_score = Serial.parseInt();
   match_stage = Serial.parseInt();
   menu_stage = Serial.parseInt();
  
   if (Serial.read() == '\n') {
     break;      
   }
 }
}

You need to add some unique start-of-packet and end-of-packet codes to the transmitter, and a way of recognizing them at the receiver.
I use "<" and ">" for this sort of thing.

Thanks for the reply.

Could you please guide me how to parse the special character?

I did try to split the value of each variable into individual lines and added a letter followed by a comma and the value of the variable. For example say if start_time variable has a value of 1 then the code to transmit this value was

Serial.print("a,");
Serial.println(start_time);

I could not get this code to work. If it had worked I was hoping to use Case to assign the value of the variable properly.

I could not get this code to work

What code?

Sorry what I meant to say was that I cannot get the code work where I am assigning a letter followed by a comma and variable value. The code I have posted in my first message works but the order is changed as mentioned earlier.

I have a library available for doing exactly what you want - but not using textual representations of the numbers.

Use that to transfer a struct of integers - that way there's no need to convert between ASCII and numbers.

CSV seems like a sensible encoding scheme to me, and using a newline to terminate each record seems like the most obvious approach when you are using a textual encoding scheme.

Simple delimited serial code that might be of use.

//zoomkat 3-5-12 simple delimited ',' string parce 
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out
        //do stuff with the captured readString 
        readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}