Leonardo Serial Commuincation works only in Serial monitor

Hi all,

This maybe is an old issue. I tried searching in the forum but in vain.

I am trying to communicate a Leonardo with an UNO through USB.
Leonardo - OTG USB - USB - Uno(Type B connector)

Both the devices seem to work in the serial monitor of Arduino IDE(running in a Windows8).

But when i connect them, i am not able to get the serial data.

Kindly find my sketches attached.
Uno sketch:

#include <LiquidCrystal.h>

const char len_sTx = 8, len_sRx = 8;
byte sTx[len_sTx], sRx[len_sRx];

LiquidCrystal lcd(8, 9, 10, 11, 12, 13);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(262144);
  lcd.begin(16,2);
}

void loop() {
  char i;
  if(Serial.available() > 0)
  {
    Serial.readBytes(sRx, len_sRx);
    sTx[0] = sRx[7];
  }
  lcd.setCursor(0,0);
  lcd.print("Rx:");
  for(i = 0; i < len_sRx; i++)
  {
    lcd.setCursor(i+3,0);
    lcd.print(sRx[i]);
    i++;
  }
  for(i = 1; i<len_sTx; i++)
  {
    sTx[i] = sTx[i-1] + 1;
  }
  Serial.write(sTx, len_sTx);
  lcd.setCursor(16,1);
  lcd.print("Tx:");
  for(i =0; i < len_sTx; i++)
  {
    lcd.setCursor(i+19,1);
    lcd.print(sTx[i]);
    i++;
  }
  delay(500);
}

Leonardo sketch:

const char len_sTx = 8, len_sRx = 8;
byte sTx[len_sTx], sRx[len_sRx];
char flag_sTx = 0;

void setup() {
  // put your setup code here, to run once:
Serial.begin(262144);
while(!Serial){
  ;
}
}

void loop() {
  char i;
  // put your main code here, to run repeatedly:
  if(flag_sTx == 0)
  {
    sTx[0] = 49;
    flag_sTx = 1;
  }
  else
  {
    if(Serial.available() > 0)
    {
      Serial.readBytes(sRx, len_sRx);
      sTx[0] = sRx[7];
    }
  }
  for(i = 1; i<len_sTx; i++)
  {
    sTx[i] = sTx[i-1] + 1;
  }
  Serial.write(sTx, len_sTx);
  delay(500);
}

I am not sure what I am doing wrongly here.

The sketches does the nearly same job. Gets the array from a serial, uses the last element alone as the first element, increments the values of other elements and sends it back.

  Serial.begin(262144);

That's an odd baud rate. Why are you using that?

    Serial.readBytes(sRx, len_sRx);

That function read some bytes, but did not NULL terminate the array. It told you how many bytes it read. You decided to just throw that value away. Why?

What is a "OTG USB"?

I have planned for a future application at the baud rate(250K). I also tried with the standard baud rate of 9600 but still no response.

Regarding the array, i thought it won't be the source of this problem. I also tried by sending a single byte.

Sorry about the OTG confusion. It is a micro-B.

So, you have a USB to micro-B cable connected to the Leonardo, with the USB end free. You have a USB to B cable connected to the UNO, with the USB end free. And, you what? Tied them in a knot?

Like the attachment

If you are having trouble sending data back and forth, you should be using MUCH simpler code, until you resolve that problem.

Both Arduinos are USB slaves. It may not be possible to get them to communicate via a USB cable. But, connecting TX of one to RX of the other, and connecting grounds should work.

That should not work at all. It should not work just because you connected the USB ports together. You can probably tie two USB flash drives with a USB A-A cable but that won't make them copy files into each other.

I thought the Leonardo can work as a USB Host

My mistake

zgravity:
I thought the Leonardo can work as a USB Host

My mistake

Possibly not. The 32U4 is only able to function as a device (slave):
Spec sheet says:
USB 2.0 Full-speed/Low Speed Device Module