Go Down

Topic: [solved]Problem with Serial Communication on Leonardo (Read 1 time) previous topic - next topic

Highttower

Jan 16, 2013, 11:11 am Last Edit: Jan 21, 2013, 11:56 am by Highttower Reason: 1
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 :) ... 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:

Code: [Select]

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 ( https://sites.google.com/site/terminalbpp/ ), 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

PaulS

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.

Krodal

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.

Highttower

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:

Code: [Select]

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

Krodal

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:
http://arduino.cc/en/Reference/Serial
http://arduino.cc/en/Serial/Available
http://arduino.cc/en/Serial/Read

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.

Highttower


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.

Edman84

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

Code: [Select]

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 );
  }
}

DRBArduino

PaulS-
Your post:
Quote
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!

NI$HANT

Quote
Is this also true on the Arduino Micro?


Yes offcourse bcz it uses the same uC.
"Real Men can Accomplish  Anything"

- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੁਂ!

Go Up