Code working with connecting Arduino + USB adapter doesn't work on 2 Arduino's

Hello
I'm using AT commands to get information about RSSI between XBee connected to USB adapter and XBee connected to Arduino (coordinator). In this case everything seems to work fine, but when instead of a USB adapter i want to use XBee connected to another Arduino the same code doesn't work, as if it couldn't go back again into the AT command mode :frowning:
I'm using Digi XBee series 2 (XB24-Z7WIT-004) and Arduino Leonardo.
Plus I tested if they are communicating with terminal and they do, so everything is good with the configuration part.
Thank you in advance for any clues. :drooling_face:
My code:

String adres1 = "ATDL40B1848D\r"; // connected to arduino
//String adres2 = "ATDL40B18453\r";
String adres3 = "ATDL40A235ED\r"; // connected to usb port

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Start");
  Serial1.begin(9600);
  SendStartCommand();
  SendAddressCommand(adres3);
}

void loop() {
  SendExitCommand();
  Serial1.write("msg"); //sending msg for having different RSSI value in each iteration
  delay(1000);
  SendStartCommand();
  SendRSSICommand();
  //delay(1000);
}
void SendStartCommand() {
  String bufor0 = "+++";
  Serial1.print(bufor0);
  while (Serial1.available() == 0) {
    ; // wait for serial port to connect. Needed for native USB port only
    Serial.print("");
    delay(300);
  }
  //response for the +++ command
  int bufor = 0;
  Serial.print("+++ ");
  while (Serial1.available() > 0) {
    bufor = Serial1.read();
    Serial.write(bufor);
  }
  Serial.println();
}

void SendAddressCommand(String Adres) {
  Serial1.print(Adres);
  while (Serial1.available() == 0) {
    ; // wait for serial port to connect. Needed for native USB port only
    Serial.print("");
    delay(300);
  }
  //response for the atdl address command
  int bufor = 0;
  Serial.print("Zmiana adresu ");
  while (Serial1.available() > 0) {
    bufor = Serial1.read();
    Serial.write(bufor);
  }
  Serial.println();
}

void SendExitCommand() {
  String bufor0 = "ATCN\r";
  Serial1.print(bufor0);
  while (Serial1.available() == 0) {
    ; // wait for serial port to connect. Needed for native USB port only
    Serial.print("");
    delay(300);
  }
  int bufor = 0;
  Serial.print("exit ");
  while (Serial1.available() > 0) {
    bufor = Serial1.read();
    Serial.write(bufor);
  }
  Serial.println();
}

void SendRSSICommand() {
  String bufor0 = "ATDB\r";
  Serial1.print(bufor0);
  while (Serial1.available() == 0) {
    ; // wait for serial port to connect. Needed for native USB port only
    Serial.print("");
    delay(300);
  }
  int bufor = 0;
  Serial.print("RSSI: ");
  while (Serial1.available() > 0) {
    bufor = Serial1.read();
    Serial.write(bufor);
  }
  Serial.println();
}

Problem solved.. I was loading the code to the Arduino while both of them were connected to the USB ports :sweat_smile: I should have start from the basics ... :roll_eyes: