Arduino Leonardo troubles reading from Serial1

I having troubles with Arduino Leonardo when reading from Serial1

Here is my super simple code:

int incomingByte = 0;

void setup(){ 
  Serial1.begin(57600);
}

void loop()
{
    // send data only when you receive data:
    while (Serial1.available()) {
      Serial.print("Available");
      if (Serial1.available() > 0) {
              // read the incoming byte:
              incomingByte = Serial1.read();
  
              // say what you got:
              Serial.print("I received: ");
              Serial.println(incomingByte, DEC);
      }
    }
}

When I write from PC I dont see any activity on FTDI LEDs (green, yellow)

Interesting thing is: when I skip Serial1.begin(57600); I see blinking light (yellow) on FTDI board conected to PIN 0/1, when I send data from PC

But when I run:

int incomingByte = 0;

void setup(){ 
  Serial1.begin(57600);
}

void loop()
{
  Serial1.println(100,DEC);
    // send data only when you receive data:
    while (Serial1.available()) {
      Serial.print("Available");
      if (Serial1.available() > 0) {
              // read the incoming byte:
              incomingByte = Serial1.read();
  
              // say what you got:
              Serial.print("I received: ");
              Serial.println(incomingByte, DEC);
      }
    }
}

I see green light on FTDI working all the time, and when I write on other side (PC) yellow LED on FTDI blinks, but I receive some messy data, even if I write same thing all the time....

Please advise.

I just want to read and write completely independently.

    while (Serial1.available()) {
      Serial.print("Available");
      if (Serial1.available() > 0) {

Unless there is serial data, the print() method won't be called. If it is, where could that serial data have gone? Is there ANY reason for the if statement?

When I write from PC I dont see any activity on FTDI LEDs (green, yellow)

There are no FTDI LEDs on a board with no FTDI chip.

What is connected to pins 0 and 1 that you are trying to read from/write to?

Ectar, since you use two serial port, you must initialize them both.

I see Serial1.begin(), but you should also do Serial.begin().

You may have first to describe how you have wired things up. Without any wiring you cannot read or write anything to/from Serial1 because nothing is connected there. So say us how your hardware looks like (you can make a photograph if that's easier for you).

Alright, here is the setup - super simple:

FTDI breakout on picture connected to PC... it has 2 light green, yellow. and acts as serial port. PC is just for debugging, it will be connected to some other devices via SERIALUSB.

On your PC you have 2 serial devices then, one is the Leonardo, the other is the FTDI board. What programs on your PC are listening to these serial devices? What baud rates have you configured on these programs? What number of data bits, stop bits and parity?

Do you have the schematics of your FTDI board (just to check that the labels are on the right pins)?

I use Putty, settings are 57600, 8, 1, N
Local echo turned on.
FTDI has 5 pins:
3.3V GND RX TX 5V

FTDI RX goes to PIN1 (TX)
FTDI TX goes to PIN0 (RX)

Can someone post me an example where I can read data from Serial1 and print it in Serial, Read data from Serial and print it in Serial1 ? This essentially what I need, just need to run some logic in between....

FTDI has 5 pins:

Of which you have connected 3. What happens if you connect either 3.3V or 5V to the appropriate pin on the Arduino? Or, have you determined that these are actually output pins? Just curious, as I don't have one of those boards, although I do have a Leonardo that reads perfectly well from an XBee on TX/RX.

Just connected 5V, doesn't help any way... which is expected.

There are some inconsistencies in the way RX and TX are sometimes labeled. What happens if you swap the wires, so RX is connected to RX, and TX is connected to TX?

Show us your sketch where you initialize both serial ports.

Sure, here is my current sketch:

int incomingByte = 0;

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

void loop()
{
  //Serial1.println(100,DEC);
    // send data only when you receive data:
    while (Serial1.available()) {
      Serial.print("Available");
      if (Serial1.available() > 0) {
              // read the incoming byte:
              incomingByte = Serial1.read();
              // say what you got:
              Serial.print("I received: ");
              Serial.println(incomingByte, DEC);
      }
    }
}

Swapping RX-TX on Leonardo (0<->1) doesn't help
But when I detach RX-TX and send characters over to COM9 (FTDI) I see it blinks, if I attach them to Leonardo, and reboot, nothing blinks when I send characters.

  Serial.begin(57600);

On the Leonardo you have to wait for Serial to become active:

  Serial.begin(57600);
  while (!Serial) { }

Tried, still doesn't work here is my sketch:

int incomingByte = 0;

void setup(){ 
  Serial1.begin(57600);
  Serial.begin(57600);
  while (!Serial1) {  }
}

void loop()
{
  //Serial1.println(100,DEC);
    // send data only when you receive data:
    while (Serial1.available()) {
      Serial.print("Available");
      if (Serial1.available() > 0) {
              // read the incoming byte:
              incomingByte = Serial1.read();
              // say what you got:
              Serial.print("I received: ");
              Serial.println(incomingByte, DEC);
      }
    }
}
  while (!Serial1) {  }

Serial, not Serial1.

I think I have some breakthrough ! Apparently FTDI module I have doesn't work properly, even It use to work with other setup before ! :frowning:
I've connected it to Prolic USB/COM adapter and data is coming through.... I was so positive module is ok

Nope... still messy, now I get something over serial, but what I send not what I receive over Serial1 (COM3), instead of 1 I receive g and so on....
When I switch Putty to work with Serial (COM8) it works perfect....(same settings just different COM port) WTH !? :frowning:

Let's see the current sketch.

Finally!!! I got it, apparently Prolific modules doesn't work properly with Leonardo, the very first one on the picture is Prolific as well, not FTDI.
So luckily I had one actual FTDI attached to my MultiWii controller, when I've used that one - now all works!

They actually partially working , writing data from Arduino to PC was fine! But not reading! Crazy... I have no idea why it was so messy with Prolific chip based Ser/USB breakout boards, any ideas?

Please enable DTR and RTS in the usb terminal, then it'll be okay

this code work for me

#define LED 13

void setup() {
// initialize both serial ports:
pinMode(LED,OUTPUT);// active high
digitalWrite(LED,LOW);
Serial.begin(57600);
while (!Serial) { }
Serial1.begin(57600);
while (!Serial1) { }
delay(2000);
Serial.println("TEST Serial");
digitalWrite(LED,HIGH);
}

void loop() {
// read from port 1, send to port 0:
if (Serial1.available()) {
digitalWrite(LED,HIGH);
while(Serial1.available()){
byte inByte = Serial1.read();
Serial.write(inByte);
}
digitalWrite(LED,LOW);
}

// read from port 0, send to port 1:
if (Serial.available()) {
digitalWrite(LED,HIGH);
while(Serial.available()){
byte inByte = Serial.read();
Serial1.write(inByte);
}
digitalWrite(LED,LOW);
}
}