Get Mac Address of Xbee Arduino

I want to get the xbee MAC address, but I fail.

I have the following code.

uint8_t myaddress[10];

uint8_t shCmd[] = {'S','H'};
uint8_t slCmd[] = {'S','L'};
AtCommandRequest atRequestSH = AtCommandRequest(shCmd);
AtCommandRequest atRequestSL = AtCommandRequest(slCmd);
AtCommandResponse atResponse = AtCommandResponse();

void getMyAddress(){
  xbee.send(atRequestSH);

  if(xbee.readPacket(5000)){
    if (xbee.getResponse().getApiId() == AT_COMMAND_RESPONSE) {
      xbee.getResponse().getAtCommandResponse(atResponse);
      if (atResponse.isOk()){
        for(int i = 0; i < atResponse.getValueLength(); i++){
          myaddress[i] = atResponse.getValue()[i];
        }
      }
    }
  }
  delay(1000);
  xbee.send(atRequestSL);

  if(xbee.readPacket(5000)){
    if (xbee.getResponse().getApiId() == AT_COMMAND_RESPONSE) {
      xbee.getResponse().getAtCommandResponse(atResponse);
      if (atResponse.isOk()){
        for(int i = 0; i < atResponse.getValueLength(); i++){
          myaddress[i+6] = atResponse.getValue()[i];
        }
      }
    }
  }
}

I was hoping the array myaddresswould be 10 values ​​because the Xbee MAC address contains 64 bytes.

But the array contains only 8 values, for example: Source Xbee address 0013a200408a31bb The function of the result getMyAddressis013a20408a31bb

My function loses two zeros.

I am printing the MAC address with the following code:

for(int i=0; i < 10; i++)
  Serial.print(myaddress[i], HEX);

Any ideas?

+3
source share
2 answers

The MAC address is 64 bits, which is 8 bytes (64 bits / (8 bits / byte)). ATSHand ATSLrespond with a 4-byte value. Therefore you should define my addressas 8 bytes and copy ATSLto myaddress[i+4].

, memcpy() , :

memcpy( &myaddress[i+4], atResponse.getValue(), 4);

Arudino Serial.print(), , MAC :

for (int i = 0; i < 8; i++) {
    if (myaddress[i] < 0x10) Serial.print( "0");
    Serial.print( myaddress[i], HEX);
}
+1

, , 10.

: 00 13 a2 00 40 8a 31 bb

: 0 13 a2 0 40 8a 31 bb

, , , , :

for(int i=0; i < 10; i++) {
    Serial.print(myaddress[i], HEX);
    Serial.print(" ");
}
+2

All Articles