Serial2 not printing

gxhary

New member
I have an Xbee connected via serial2 on teensy 3.1
This code works fine no problem
C++:
void setup() {
  Serial.begin(19200);
  Serial2.begin(19200);
}

void loop() {
  Serial2.println("test");
  delay(1000);
    String  message = "";
    char incomingByte;
    while (Serial2.available()>0){
      incomingByte = Serial2.read();
      message += incomingByte;
      }
    if (message != ""){
      Serial.println(message);
    }
}

However when i implement this code into my project i get nothing.
I tried
Code:
while(!Serial2);
and it passed however Serial2.println("text"); does not work I get nothing in the output, and I cannot read anything.

I noticed that adding serial2.begin(19200); before every print line somehow works but reading does not work.
Can anyone help with this?
 
Quick test of the Teensy would be to jumper the Serial2 Pins "Rx to Tx" on the Teensy and confirm that works to see the "test" go out and come back. Seems the above sketch should show that.

If that does not show up then there is a bad pin or wrong pin in use.

If it does work then the issue it seems is connection or function of the Xbee.

<EDIT>: Is there a COMMON GND to the Xbee?
 
Quick test of the Teensy would be to jumper the Serial2 Pins "Rx to Tx" on the Teensy and confirm that works to see the "test" go out and come back. Seems the above sketch should show that.

If that does not show up then there is a bad pin or wrong pin in use.

If it does work then the issue it seems is connection or function of the Xbee.

<EDIT>: Is there a COMMON GND to the Xbee?
this code works fine with serial2 the problem is when i add it to my main project, it doesnt work, yet somehow when i switch to serial3 all work fine, i dont want to switch to serial 3 though
 
this code works fine with serial2 the problem is when i add it to my main project, it doesnt work, yet somehow when i switch to serial3 all work fine, i dont want to switch to serial 3 though
Looking at the pinout sheet, the Serial2 pins are used as OUT1A and IN1 for I2S audio. Even if you don't use Audio directly, maybe something in the libraries initializes the IN/OUT pins to be used for audio instead of serial devices.

One thought would be to move the Serial2.begin call after other library calls in setup.
 
this code works fine with serial2 the problem is when i add it to my main project, it doesnt work

Try making a copy of your main project and delete portions until Serial2 starts working.

Or if you don't find the problem, delete as much of your main project so you can show us a complete non-working code. If anyone can copy it into Arduino IDE and experience Serial2 not working, odds are good we'll be able to help you find the problem. We're usually pretty good on this forum at figuring out why a program doesn't work... when we can actually run that program on our own Teensy board.
 
Looking at the pinout sheet, the Serial2 pins are used as OUT1A and IN1 for I2S audio. Even if you don't use Audio directly, maybe something in the libraries initializes the IN/OUT pins to be used for audio instead of serial devices.

One thought would be to move the Serial2.begin call after other library calls in setup.
i call it later in the setup after the everything is done, one thing i noticed is it only prints if the serial2.begin is the line before it, however is doesnt read at all
 
i call it later in the setup after the everything is done, one thing i noticed is it only prints if the serial2.begin is the line before it, however is doesnt read at all
This is where the RX<>TX jumper in place of the actual XBee board test in p#2 might give helpful feedback.

If that works with .begin early in setup then the hardware is good.
If it works without the XBee then points to the issue.
If it then fails on Rx with just the jumper and the .begin late in setup - then something is perhaps reconfiguring the Serial2 pin.
 
Back
Top