Arduino mega 2560 XBEE Communication problem

Hi everyone.

I am trying to get a simple connection between two XBee modules. I have one connected to the PC using serial COM and a USB cable, the other one is connected to the Serial2 of the Arduino mega 2560.

I can send data from the PC using the X-CTU software but it is not the right data. The Xbee module on the Arduino board is a coordinator with API on. Baudrate on both side is 9600, PAN ID is set 1234 on both.

Does anyone have any idea what the problem might be?

I can send data from the PC using the X-CTU software but it is not the right data.

What are you sending? What are you receiving? Post your code.

larry12193:

I can send data from the PC using the X-CTU software but it is not the right data.

What are you sending? What are you receiving? Post your code.

I actually got that sorted. It was a problem with the setup on the Xbee end module, it was not API mode. So I changed the Firmware to API and sent the right API command and then going through the received data as:

if(Serial2.available() > 20)
    {
      if(Serial2.read() == 0x7E)
      {
        for(i=0;i<15;i++)
          Serial2.read();
        for(i = 0;i<4;i++)
          Serial.write(Serial2.read());
      }
    }

Xbee is connected to Serial2 on the Arduino. So I can read the data on the coordinator now.

The other problem is sending anything from the Arduino to the End module. I know there is comms between the two Xbee moduels; after every transmission request from the XCTU, there is a response from the coordinator saying “yep, got the message”. The problem is when I try to send anything from the Arduino to the Xbee coordinator to be sent to the other module. So here is what I use

if(Serial.available())
    {
     Serial2.print("7E 00 12 10 01 00 13 A2 00 40 8B 46 24 FF FE 00 00 77 6F 72 6B 44"); 
     Serial.print(Serial2.read());
    }

What I’m sending to Serial2 is an API command with RF Data being “work” (77 6F 72 6B). Now when I read Serial2 for any response, all I get is -1. I’ve been trying to find a reference to this -1 but I’ve failed so far!

UPDATE:

Actually, I modified the code a little bit to get every API parameter out of what I receive and there is a problem! So my API transmit request is 22 bytes, if I read 21 bytes from the serial, all the info all wrong except the actual data. So the data is right, but the rest of the parameters don’t match at all!

Here is what XCTU is giving me as the sent command:

  • Start delimiter: 7E
  • Length: 00 12 (18)
  • Frame type: 10 (Transmit Request)
  • Frame ID: 01 (1)
  • 64-bit dest. address: 00 00 00 00 00 00 00 00
  • 16-bit dest. address: FF FE
  • Broadcast radius: 00 (0)
  • Options: 00
  • RF data: 77 6F 72 6B
  • Checksum: 2E

And what I am getting at the Arduino with 21 bytes is:
Start: 7E
Length: 0 10
Frame type: 90
Frame ID: 0
64 bit Address: 7D 33 A2 0 40 8B 45 AD
16 bit Address: 3D 64
Broadcast radius: 41
RF Data: work
checksum: 58

So the start byte is right, data is right but the rest of them are all wrong!! And if I read all the 22 bytes:

Start: 7E
Length: 0 10
Frame type: 90
Frame ID: 0
64 bit Address: 7D 33 A2 0 40 8B 45 AD
16 bit Address: 3D 64
Broadcast radius: 41
Option: 77
RF Data: orkX
checksum: 7E

This is the code I use now:

 if(Serial2.available() > 21)
    {
      //temp = Serial2.read();
      if( Serial2.read()== 0x7E)
      {
        //starter 1 byte
        Serial.print("Start: 7E");

        Serial.print("\n");
        
        // length 2 bytes
        Serial.write("Length: ");
        for(i=0;i<2;i++) 
        {
          Serial.print(Serial2.read(), HEX);
          Serial.print(" ");
        }
        Serial.print("\n");
        
        //frame type 1 byte
        Serial.print("Frame type: ");
        Serial.print(Serial2.read(), HEX);
        Serial.print("\n");
        
        //Frame ID 1 byte
        Serial.print("Frame ID: ");
        Serial.print(Serial2.read(), HEX);
        Serial.print("\n");
        
        //64 address 8 bytes
        Serial.print("64 bit Address: ");
        for(i = 0; i<8;i++)
        {
         Serial.print(Serial2.read(),HEX);
         Serial.print(" ");
        }
        Serial.print("\n");
        
        //16 adress 2 bytes
        Serial.print("16 bit Address: ");
        for(i = 0; i<2;i++)
        {
         Serial.print(Serial2.read(),HEX);
         Serial.print(" ");
        }
        Serial.print("\n");
        
        //Broadcast radius 1 byte
        Serial.print("Broadcast radius: ");
        Serial.print(Serial2.read(), HEX);
        Serial.print("\n");
        
        //option 1 byte
        Serial.print("Option: ");
        Serial.print(Serial2.read(), HEX);
        Serial.print("\n");
        
        
        //RF Data 4 bytes
        Serial.print("RF Data: ");
        for(i = 0;i<4;i++)
          Serial.write(Serial2.read());
        
        //check sum 1 byte  
        Serial.print("\nchecksum: ");
        Serial.print(Serial2.read(),HEX);
        Serial.print("\n");
      }

I am really confused and this problem is not making any sense to me at all