Pages: [1]   Go Down
Author Topic: [solved]Problem with Serial Communication on Leonardo  (Read 1379 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley ... 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:
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
« Last Edit: January 21, 2013, 05:56:49 am by Highttower » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49384
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1016
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-sad.


The right code is:

Code:
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
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1016
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49384
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Germany, Bielefeld
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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 );
  }
}
Logged

Louisville, KY
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 12
Posts: 1601
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is this also true on the Arduino Micro?

Yes offcourse bcz it uses the same uC.
Logged

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

Pages: [1]   Go Up
Jump to: