Go Down

Topic: Arduino Leonardo troubles reading from Serial1 (Read 3939 times) previous topic - next topic

ectar

I having troubles with Arduino Leonardo when reading from Serial1

Here is my super simple code:

Code: [Select]

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:

Code: [Select]

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.

PaulS

Code: [Select]
    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?

Quote
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?

Erdin

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

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

pylon

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

ectar

Alright, here is the setup - super simple:

https://www.dropbox.com/s/bogzbc2v3j91rvl/IMG_00000002.jpg

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.

pylon

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)?

ectar

#6
Feb 26, 2013, 12:19 am Last Edit: Feb 26, 2013, 12:24 am by ectar Reason: 1
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....

PaulS

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

ectar

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

PaulS

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?

Nick Gammon

Show us your sketch where you initialize both serial ports.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

ectar

Sure, here is my current sketch:

Code: [Select]
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.

Nick Gammon

Code: [Select]

  Serial.begin(57600);


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

Code: [Select]

  Serial.begin(57600);
  while (!Serial) { }
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

ectar

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

Code: [Select]


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


Nick Gammon

Quote

Code: [Select]

  while (!Serial1) {  }



Serial, not Serial1.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up