Serial Communication not working between two arduinos

I can't believe I can't get this to work on my own but anyway, here goes!
I am trying to get two arduinos to communicate (a Mega and a Uno). I have jumpers between their ground pins and 5v to Vin etc.
The following code works fine to communicate directly to Arduino (Uno) via USB.

/*Serial_LED_02.ino  Arduining 4 May 2015
Controlling the LED in pin 13 with the Serial Monitor.
--- Command list: ---
? -> Print this HELP 
a -> LED On  "activate"
d -> LED Off "deactivate"
s -> LED     "status" 

Example using the switch statement.
*/
 
#define LED 13          // Pin 13 is connected to the LED
char rxChar= 0;         // RXcHAR holds the received command.

//=== function to print the command list:  ===========================
void printHelp(void){
  Serial.println("--- Command list: ---");
  Serial.println("? -> Print this HELP");  
  Serial.println("a -> LED On  \"activate\"");
  Serial.println("d -> LED Off \"deactivate\"");
  Serial.println("s -> LED     \"status\"");  
  }
  
//---------------- setup ---------------------------------------------
void setup(){
  Serial.begin(9600);   // Open serial port (9600 bauds).
  pinMode(LED, OUTPUT); // Sets pin 13 as OUTPUT.
  Serial.flush();       // Clear receive buffer.
  printHelp();          // Print the command list.
}

//--------------- loop ----------------------------------------------- 
void loop(){
  if (Serial.available() >0){          // Check receive buffer.
    rxChar = Serial.read();            // Save character received. 
    Serial.flush();                    // Clear receive buffer.
  
  switch (rxChar) {
    
    case 'a':
    case 'A':                          // If received 'a' or 'A':
  if (digitalRead(LED) == LOW){        // If LED is Off:
          digitalWrite(LED,HIGH);      // Turn On the LED.
          Serial.println("LED turned On");
  }
        else Serial.println("LED already On!");
        break;

    case 'd':
    case 'D':                          // If received 'd' or 'D':
  if (digitalRead(LED) == HIGH){       // If LED is On:
          digitalWrite(LED,LOW);       // Turn Off the LED.
          Serial.println("LED turned Off");
  }
        else Serial.println("LED already Off!");
        break;
        
    case 's':
    case 'S':                          // If received  's' or 'S':
  if (digitalRead(LED) == HIGH)        // Read LED status.
          Serial.println("LED status: On");
        else Serial.println("LED status: Off");
        break;
        
    case '?':                          // If received a ?:
        printHelp();                   // print the command list.
        break;
        
    default:                           
      Serial.print("'");
      Serial.print((char)rxChar);
      Serial.println("' is not a command!");
    }
  }
}
// End of the Sketch.

When I connect USB to the Arduino Mega and upload this code:

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

void loop() {
  if (Serial.available()) {      // If anything comes in Serial (USB),
    Serial1.write(Serial.read());   // read it and send it out Serial1 (pins 0 & 1)
  }
   if (Serial1.available()) {      // If anything comes in Serial (USB),
    Serial.write(Serial1.read());   // read it and send it out Serial1 (pins 0 & 1)
  }

 
}

I can send serial data to the Mega on its own and receive it back if I have jumpers connected (Serial1Tx->SerialRx and Serial1Rx -> SerialTx) on the mega . However if I connect Serial1 tx on the Mega to the Arduino Uno Rx and Serial1rx to the Uno Tx (along with ground and vin) i get nothing back. If I connect the Mega SerialTx and RX to the Uno instead of Serial1 Tx and Rx I get gibberish on the console.

' imand!
' command⸮⸮a com

Console is set to baud 9600.
What am I doing wrong?

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable.

You can send data in a compatible format with code like this

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

I suggest you use SoftwareSerial to create an extra serial port on the Uno as that will leave HardwareSerial available for debugging messages.

...R

Hope that you will be able to figure out from the following setup and codes why your setup/code is not working.

MegaUart.png
Figure-1: Connection diagram between MEGA and UNO

A: Master-MEGA Codes

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

void loop()
{
  if (Serial.available())
  { // If anything comes in Serial (USB),
    Serial1.write(Serial.read());   // read it and send it out Serial1 (pins 0 & 1)
  }
  if (Serial1.available())
  { // If anything comes in Serial (USB),
    Serial.write(Serial1.read());   // read it and send it out Serial1 (pins 0 & 1)
  }
}

B: Slave-UNO Codes

#include<SoftwareSerial.h>
SoftwareSerial mySUART(2, 3); //mySUART(SRX, STX)

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

void loop()
{
  if(mySUART.available())
  {
    Serial.write(mySUART.read());
  }
  if(Serial.available())
  {
    mySUART.write(Serial.read());
  }
}

C: Communication between MEGA-UNO

D: Procedures
(1) Connect MEGA and UNO as per connection diagram of Fig-1.
(2) Upload MAaster-MEGA codes into Arduino MEGA and Slave-UNO codes into Arduino UNO.
(3) Press reset buttons of bothe Ardunos.
(4) Bring SM1 of MEGA and SM2 of UNO.
(5) At the inputbox of SM1, enter Hello! and click on the Send button. Check that the message has appeared on SM2.
(6) At the inputbox of SM2, enter Fine! and click on the Send button. Check that the message has appeared on SM1.

MegaUart.png

Thanks Golam and Robin for the answers. I have figured out why at least some of my setup wasn't working. My Uno appears to be broken somehow, sending a byte once a second only seems to work when connected to Serial monitor but not when conne ted to a battery, I've replaced it with a Nano which does seem to work! Also the TX1 and RX1 pins on my cheap Mega clone are mislabeled (Swapped!)