[solved]Problem with Serial Communication on Leonardo

Hi everyone,

I've started work with Arduino Leonardo last week. I realy happy with getting fast results form the Board with out knowing anything about Microcontrollers before. In my first projects i read some measured from a photo sensor and a LED. Its works realy fine :slight_smile: ... if i put the data via Keyboard-Simulator (Keyboard.begin) on my Computer. As I want to do it more professional via Serial it fails.

So I start to test some very very easy:

void setup() {
  Serial1.begin(9600);
  pinMode(2, OUTPUT);
}

void loop()
{
   digitalWrite(2, LOW);
   while (Serial.available()) {
      Serial1.println("Ping") ;
      digitalWrite(2, HIGH);
      delay(500);
   }
}

This result is very strange: If i do the Serial Monitor (IDE->Tools->Serial Monitor). Its works realy fine:

Sending data -> The LED on port 2 is glowing, and i get every half second a new "Ping".

But if i am using my common serial monitor ( Terminal ), it will be this behaviour:

Sending data -> The LED on port 2 is glowing, but there is no "Ping" i'll wait as long as i want nothing come up.

The ping appears if I reconnect to the port.

My config is:

The same is if i write a quick serial interface in C# -> same wrong behaviour.

Am I doing something on the arduion worng? Should i set some flag like "Yes-I-want-realy-to-send-now"?

Thanks for reading and help.

Best regards,

Tobias

There are two serial ports on the Leonardo, Serial and Serial1. The Serial instance is via the onboard USB-to-serial conversion process. Serial1 is via the RX and TX pins.

If you don't have anything connected to the RX and TX pins, using Serial1 is not going to accomplish anything.

Using Serial.available(), Serial.print(), or Serial.read() without using Serial.begin() is silly.

You should have started with the common Arduino Uno.
The Arduino Leonardo is very specific for USB emulation, and this makes it sometimes not so easy to use.

You are mixing Serial and Serial1. Are you sure you want to do that ? How did you connect everything ?

The default Serial on the Leonardo board is a software simulation for the serial monitor via the USB connector.
The extra Serial1 is the hardware serial port inside the microcontroller (RX,TX) and can be used for anything.

In the sketch you wait for something to be available, but you don't read it.
And if the Leonardo is always sending, I'm having troubles connecting it for the sketch upload.
Your settings are okay, and it should be no problem to use any serial monitor program.
I used the most simple one : http://qst.sourceforge.net/

While I was writing this, PaulS already wrote the same.

Thank you lots for answering.

While I'm posting the source, i noticed that form a last Test there was the "1" after the serial and put it out .... once :(.

The right code is:

void setup() {
  Serial.begin(9600);
  pinMode(2, OUTPUT);
}

void loop()
{
   digitalWrite(2, LOW);
   while (Serial.available()) {
      Serial.println("Ping") ;
      digitalWrite(2, HIGH);
      delay(500);
   }
}

The Boad is connected via USB.

As i understund the Leonardo is the next Generation form the UNO, is that a big mistake?

Sorry for that,

Tobias

In my opinion, the Leonardo is specifically for USB emulation. The Uno is still the most common board to start with.

You test if something is available (in the buffer), but you don't read it. So if some data is in the buffer, it is always available.
You must read the data from the buffer, so the avaible function will return zero if the buffer is empty.

Read about the functions and look at the examples:

The Leonardo requires that DtrEnable be set when opening the serial connection (on the PC end). The Serial Monitor does that. Most PC applications do not. There was a discussion on why GoBetwino failed to work with a Leonardo, and this missing attribute was determined to be the culprit.

I'm guessing that your C# application doesn't use DtrEnable, and that the other terminal app doesn't either.

PaulS:
The Leonardo requires that DtrEnable be set when opening the serial connection (on the PC end). The Serial Monitor does that. Most PC applications do not. There was a discussion on why GoBetwino failed to work with a Leonardo, and this missing attribute was determined to be the culprit.

I'm guessing that your C# application doesn't use DtrEnable, and that the other terminal app doesn't either.

Thanks a lot thats helps.

You can solve the DTR Problem by editing "Arduino\hardware\arduino\cores\arduino\CDC.cpp.
Just comment if (_usbLineInfo.lineState > 0) { at line 208 and } at line 216.
The following testcode should work out of the box

void setup()
{
  Serial.begin(115200); //This pipes to the serial monitor
  while(!Serial);
  Serial1.begin(115200); //This is the UART, pipes to sensors attached to board
}

void loop()
{
  if( Serial.available() )
  {
    int inByte = Serial.read();
    Serial1.write( inByte );
  }
  if( Serial1.available() )
  {
    int inByte = Serial1.read();
    Serial.write( inByte );
  }
}

PaulS-
Your post:

There are two serial ports on the Leonardo, Serial and Serial1. The Serial instance is via the onboard USB-to-serial conversion process. Serial1 is via the RX and TX pins.

Is this also true on the Arduino Micro? I don't know why I never noticed this on any description, but it could solve all my problems!

Is this also true on the Arduino Micro?

Yes offcourse bcz it uses the same uC.

Edman84:
You can solve the DTR Problem by editing "Arduino\hardware\arduino\cores\arduino\CDC.cpp.
Just comment if (_usbLineInfo.lineState > 0) { at line 208 and } at line 216.
The following testcode should work out of the box

void setup()

{
  Serial.begin(115200); //This pipes to the serial monitor
  while(!Serial);
  Serial1.begin(115200); //This is the UART, pipes to sensors attached to board
}

void loop()
{
  if( Serial.available() )
  {
    int inByte = Serial.read();
    Serial1.write( inByte );
  }
  if( Serial1.available() )
  {
    int inByte = Serial1.read();
    Serial.write( inByte );
  }
}

OH MY GOD! TKX SO MUCH! I was searching for solution today and i was getting sad cause i can't connect my arduino micro in my external serial monitor. Your tip solved my problem! tks again!!!

PaulS:
The Leonardo requires that DtrEnable be set when opening the serial connection (on the PC end).

Thanks for the advice guys, I've been struggling on that serial communication problem for 3 days!
You are awesome.

Edman84:
You can solve the DTR Problem by editing "Arduino\hardware\arduino\cores\arduino\CDC.cpp.
Just comment if (_usbLineInfo.lineState > 0) { at line 208 and } at line 216.
The following testcode should work out of the box

void setup()

{
  Serial.begin(115200); //This pipes to the serial monitor
  while(!Serial);
  Serial1.begin(115200); //This is the UART, pipes to sensors attached to board
}

void loop()
{
  if( Serial.available() )
  {
    int inByte = Serial.read();
    Serial1.write( inByte );
  }
  if( Serial1.available() )
  {
    int inByte = Serial1.read();
    Serial.write( inByte );
  }
}

How do I download the CDC.cpp library to get _usbLineInfo? I'm having the same problem with my Leonardo.

Edman84:
You can solve the DTR Problem by editing "Arduino\hardware\arduino\cores\arduino\CDC.cpp.
Just comment

if (_usbLineInfo.lineState > 0) {

at line 208 and

}

at line 216.
The following testcode should work out of the box

void setup()

{
 Serial.begin(115200); //This pipes to the serial monitor
 while(!Serial);
 Serial1.begin(115200); //This is the UART, pipes to sensors attached to board
}

void loop()
{
 if( Serial.available() )
 {
   int inByte = Serial.read();
   Serial1.write( inByte );
 }
 if( Serial1.available() )
 {
   int inByte = Serial1.read();
   Serial.write( inByte );
 }
}

Thank u very much for the solution. Almost 5 years gone but still effective. Ive been googling around for a few days and couldnt have found any way to solve that problem until i encounter your post. :))