I would like to exchange data via Xbee. I've set up a communication based on strings between the Arduino and a Matlab software. Unfortunately this kind of communication isn't enough robust and sometimes messages aren't delivered correctly. So I decided to move to the binary communication.

The problem:

I'm not able to send binary data from the Arduino to the computer.

Let's say we want to send the decimal value 17 which is 0x11 in hex if we receive 16 from Matlab.

SoftwareSerial xbee(pinRx, pinTx); 
void setup()
  xbee.begin( BaudRateXbee );
void loop()
    getData = xbee.read(); 
    Serial.print("Received: ");
    if(getData == 16)
      // First attempt   
      //uint8_t s = 17; 
      //xbee.write(s);  -> Fail
      // Second attempt
      //xbee.write(0x11);  -> Fail
      // Third attempt
      byte message[] = {0x11, 0x11};
      xbee.write(message, sizeof(message)); 

In the third attempt I tried sending an array of bytes and noticed that the received values were different.

byte message[] = {0x11, 0x11};

xbee.write(message, sizeof(message));

The Matlab software sends data correctly. I used the fwrite function as follows fwrite(xbee,hex2dec('10')); and the fread function to receive the ack which:

reads binary data from the device connected to the serial port object, obj, and returns the data

Matlab relevant code:

disp('Ack Requested...')
ack = fread(xbee);
if (ack == 17) 
     % communication established

I've also tried using XC-TU to monitor traffic on Serial port (blu values are sent from the PC, red ones are received from the Arduino) and as you can see red values are different and none of them is equivalent to 17! What am I missing?

enter image description here

  • what is the range of your number? – Martynas Nov 10 '14 at 7:25
  • I think the problem is in Matlab. You send data from string so String a = "1"; is byte 31. Make a debug, add Serial.println(getData,HEX); and see what are you getting – Martynas Nov 10 '14 at 7:55
  • The range is +/- 5000. I am able to receive data from Matlab. I've done the test with XC-TU to see the hex values sent from Arduino and as you can see, the boards sends every time different values. (Red ones) XC-TU and matlab receive the same data. – UserK Nov 10 '14 at 14:25
  • xbee.write(message, sizeof(message)); this shows in MatLab as 75,70??? Not sure but try to check baud rates or so. I just can't see what is wrong.. – Martynas Nov 10 '14 at 14:28
  • Yes exactly. Every time I click a button in MATLAB, it sends 16. Arduino receives it and responds with 17 twice. The values sent are completely different from each other. It doesn't make sense... Is the software serial library the problem? – UserK Nov 10 '14 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.