Author Topic: clear *data after Rfduino.onReceive(char *data, int len){}  (Read 2685 times)

ruben_vleuten

  • RFduino Newbie
  • *
  • Posts: 11
  • Karma: +0/-0
    • View Profile
clear *data after Rfduino.onReceive(char *data, int len){}
« on: April 07, 2014, 12:11:41 PM »
Hi,

At the moment I'm having issues with printing the incoming BLE data to serial. I'm sending either a 4 byte message or a 10 byte message over BLE to a RFduino which I then want to print in serial. These messages will follow each other up in a random order. So sometimes it will be a 4 byte and sometimes a 10 byte.

I'm doing the following:

void RFduinoBLE_onReceive(char *data, int len) {
 
  if (len > 0) {
    Serial.println(data);
  }
}


It all works fine with the 4 byte messages or the 10 byte separate. But as soon as I'm using them together it goes wrong. When I'm sending a 4-byte message after a 10-byte message it keeps on printing 10-byte values. It seems like it keeps them in memory. This is for instance what happens when I send two times a 4-byte messages (4444) followed by a 10-byte message (xxxxxxxxx) and again by a 4-byte message(4444):

4444
4444
xxxxxxxxxx
4444xxxxxx


It seems that it keeps the last 6 bytes in memory and I don't know how to clear them. I tried several things but I can't solve it. Is there a way to receive messages of different lengths without mixing them up?

Cheers R.

jfp

  • RFduino Newbie
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: clear *data after Rfduino.onReceive(char *data, int len){}
« Reply #1 on: April 07, 2014, 11:49:13 PM »
Hi,

The received data are valid up to len characters. If you really want a string null terminated string you should do:

Code: [Select]
  if (len > 0) {
    char mydata[len + 1];
    memcpy(mydata, data, len);
    mydata[len] = 0;
    Serial.println(mydata);
  }

Or you can include the terminated null character in the string send, so, for a text message, sending strlen(text) + 1 characters not strlen(text) characters.

JF

 

anything