My Arduino don't read the serial port when I connect the XBee Shield [Solved]

First, I'm sorry for my English, it's really bad, well, my problem is when I send the instruction Serial.print(); or Serial.write(); and have the shield active and connected, my Arduino send a nonsense text, I want and try a lot of things, but I don't see the problem, adjunct my code

#include <SoftwareSerial.h>
SoftwareSerial XBee(3,2);

void setup() 
{
  Serial.begin(9600);
  XBee.begin(9600);
}

void loop() 
{
  if (XBee.available()>0)
  {
    int dato = XBee.read();
    Serial.write(char(dato));
  }
}

adjunct the Serial Monitor read:

See this? You need to give some time for the communication to be established. See what some example programs are doing!

1 Like

thanks for your help, by some chance would you have an example about this?? I seen some programs related about this project but in all use the same structure

If your examples actually there will be a "wait" after calling to establish communications.

Finally, I was able to solve it. To do so, I had to change the char to string for data reading, and I used a counter to discard the data that didn’t work for me. Although the bit size depends on the type of data you want to preserve, I am attaching the code in which I use the XBee to read digital on/off signals, where you can see the logic I used so that the information could be read without any problem.

#include <SoftwareSerial.h>
SoftwareSerial XBee(2,3);
char dato;
int cont;
unsigned long previousMillis=0;
const long interval=500;
void setup() 
{
  Serial.begin(9600);
  XBee.begin(9600);
  cont=0;
}

void loop() 
{
  if (XBee.available()>0)
  {
    //desencriptado de paquetes de datos de XBee API//
    if(cont < 20)
    {
      dato = XBee.read();
    }
    else if (cont == 20)
    {
      dato = XBee.read();
      unsigned long currentMillis = millis();
      if(currentMillis - previousMillis >= interval)
      {
        previousMillis=currentMillis;
        //Serial.print(String(dato,HEX));
        if (String(dato,HEX)=="0")
        {
          Serial.print("off");
        }
        else if (String(dato,HEX)=="1")
        {
          Serial.print("on");
        }
        Serial.print(",");
        Serial.print(" ");
        Serial.println();
      }
      //Serial.println(cont);
    }
    else if (cont > 20)
    {
      dato = XBee.read();
      if (cont > 21)
      {
        cont=0;
      }
    }
    cont++;
    //Serial.println(String(dato,HEX));
    //---------------------------------------------//               
  }
}

The codes that are in the comments were used to verify which bit contained the data of interest and if the data was being received correctly. I clarify this to avoid any confusion.

I used string because the information is sent in hex, and the information can be a letter or a number, and I need to use the function hex to transform this package from hex to string