0

Hi I have problem with school project. I have 3 Arduino - 1 Transmiter with NRF; 2,3 - NRF receiver and wire slave receiver. I can't send data via "Wire" library (in code char "datar") to receiver arduino. I won't get anything, but when I want to send like Wire.write("blablabla"); it pass. But when in char is NRF data code crashing when go to "Wire.write".

/*
*/
#include <Wire.h>
#include <SPI.h>
#include "RF24.h"

char datar = '';

//Konfiguracja
const uint64_t pipes[2] = {0xF1F2F3F0F7/*odbieranie*/, 0xF1F2F3F0F8/*wysylanie*/};
RF24 radio(9, 10); //Piny nrf (SPI) 9 i 10
//-- Konfiguracja

void setup() {
  Serial.begin(9600);
  Serial.println("LOL");
  Wire.begin();
  radio.begin();
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate(RF24_250KBPS);
  radio.openReadingPipe(1, pipes[0]);
  radio.openWritingPipe(pipes[1]);
  radio.setChannel(78);
  radio.setAutoAck(true); 
  radio.startListening();
}


void loop() {

  int len; 
  if(radio.available())
  {
  len = radio.getDynamicPayloadSize();
  radio.read(&datar, len);
  delay(5);
  Serial.println(datar);
  Wire.beginTransmission(7);
  Wire.write(&datar);
  Wire.endTran345smission();
  radio.printDetails();
  }
  delay(5);


}
  • What are the input of the write function? Are you sure you need to pass a pointer or a variable – MathieuL Oct 2 '15 at 15:58
2

Your problem is here:

char datar = '';

...

 len = radio.getDynamicPayloadSize();
 radio.read(&datar, len);

The variable "datar" is one byte long. You then find the payload size (presumably greater than one byte) and read into datar more than one byte. This overwrites other parts of memory and the program crashes. You need to make datar larger, eg.

char datar [50];

The exact size would depend on your maximum payload size.

  • Absolute maximum size NRF24L01+ supports should be 32 bytes. (And it wouldn't hurt if one also updated their RF24 libraries through the IDE) – Avamander Oct 3 '15 at 7:49
1

The code crash because you pass an memory address to the write function if you look to the arduino doc you pass a variable to the function therefore just writing datar as argument will end your problem

  • 1
    Not really, unless the payload size is only one byte. – Nick Gammon Oct 2 '15 at 16:11
1

I think the answer is a combination of Nick and Mathieu's answers.

Like Nick said you haven't assigned any memory to datar, so this is probably causing a crash because of buffer overrun.

Now to the call to read. Mathieu said you are passing a pointer into the function when you probably didn't mean to. But if you put Nicks change in then you do need to pass in a pointer to the character array. You can do this by radio.read(datar, len); because the character array is a character pointer.
You do not want to pass a pointer to a character pointer &datar because that would mean the function was able to move the memory that you have allocated.

Also, you need to prevent buffer overflows in the "new improved" code by checking that the value of len is less than or equal to the value you allocated when you declared the data buffer (i.e. 50). Because you are using a number in two places you should declare it as a constant at the top of the file.

const int MaxBufferSize = 50;
char datar[MaxBufferSize];
...
len = radio.getDynamicPayloadSize();
if (len > MaxBufferSize)
  len = MaxBufferSize;
radio.read(datar, len);

Hope that helps.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.