Newbie With (Potential Pointer) Isssue?

Greetings, All!
My name is nick and I am new to Audrino and C++ in general. I am capable (for the most part) of deciphering how to adapt code to suite my learning needs but seem to have hit a hard place. If anyone can give me an explanation of what I am doing wrong, I would greatly appreciate it. right now if I just put the supplied examples on each nano and upload, they talk back and forth fine. But I want to modify the data sent to the other end.

I have (2) Nanos each connected to a NRF2401L+ transciever. I am using the Radiohead library located HERE

The library comes with two sets of code a Client and a Server. Here is the client.

// nrf24_client.pde
// -*- mode: C++ -*-
// Example sketch showing how to create a simple messageing client
// with the RH_NRF24 class. RH_NRF24 class does not provide for addressing or
// reliability, so you should only use RH_NRF24 if you do not need the higher
// level messaging abilities.
// It is designed to work with the other example nrf24_server.
// Tested on Uno with Sparkfun NRF25L01 module
// Tested on Anarduino Mini (http://www.anarduino.com/mini/) with RFM73 module
// Tested on Arduino Mega with Sparkfun WRL-00691 NRF25L01 module

#include <SPI.h>
#include <RH_NRF24.h>

// Singleton instance of the radio driver
RH_NRF24 nrf24;
// RH_NRF24 nrf24(8, 7); // use this to be electrically compatible with Mirf
// RH_NRF24 nrf24(8, 10);// For Leonardo, need explicit SS pin
// RH_NRF24 nrf24(8, 7); // For RFM73 on Anarduino Mini

void setup() 
{
  Serial.begin(9600);
  while (!Serial) 
    ; // wait for serial port to connect. Needed for Leonardo only
  if (!nrf24.init())
    Serial.println("init failed");
  // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!nrf24.setChannel(1))
    Serial.println("setChannel failed");
  if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
    Serial.println("setRF failed");    
}


void loop()
{
  Serial.println("Sending to nrf24_server");
  // Send a message to nrf24_server
  uint8_t data[] = "Hello World!";
  nrf24.send(data, sizeof(data));
  
  nrf24.waitPacketSent();
  // Now wait for a reply
  uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
  uint8_t len = sizeof(buf);

  if (nrf24.waitAvailableTimeout(500))
  { 
    // Should be a reply message for us now   
    if (nrf24.recv(buf, &len))
    {
      Serial.print("got reply: ");
      Serial.println((char*)buf);
    }
    else
    {
      Serial.println("recv failed");
    }
  }
  else
  {
    Serial.println("No reply, is nrf24_server running?");
  }
  delay(400);
}

What I would like to focus on is this:

Serial.println(“Sending to nrf24_server”);
// Send a message to nrf24_server
uint8_t data = “Hello World!”; //<---------THIS LINE
nrf24.send(data, sizeof(data));

I want to be able to say, read from the serial port and assign the received data, to what the the radio will send. So that I can choose what is sent to the other radio besides just an explicit line of text, but if I just try to assign something like serial.Read = data the compilers throws:

nrf24_client:41: error: initializer fails to determine size of ‘data’

Im guessing I need to get crafty with a few topics like pointers or perhaps more familiar with data types but I just don’t know what to identify the topics I needs to learn for this as for sure

I don't have hardware that will allow me to duplicate your problem, but the statement:

serial.Read = data[];

is backwards from what is usually done with a read(). Normally, a read() call from a Serial object returns a single byte of data, so the syntax is:

data[index++] = Serial.read();

where index references the position where you want to put the data in the array.

crombwellJewls:
Greetings, All!
My name is nick and I am new to Audrino and C++ in general. I am capable (for the most part) of deciphering how to adapt code to suite my learning needs but seem to have hit a hard place. If anyone can give me an explanation of what I am doing wrong, I would greatly appreciate it. right now if I just put the supplied examples on each nano and upload, they talk back and forth fine. But I want to modify the data sent to the other end.

I have (2) Nanos each connected to a NRF2401L+ transciever. I am using the Radiohead library located HERE

The library comes with two sets of code a Client and a Server. Here is the client.

What I would like to focus on is this:

Serial.println(“Sending to nrf24_server”);
// Send a message to nrf24_server
uint8_t data = “Hello World!”; //<---------THIS LINE
nrf24.send(data, sizeof(data));

I want to be able to say, read from the serial port and assign the received data, to what the the radio will send. So that I can choose what is sent to the other radio besides just an explicit line of text, but if I just try to assign something like serial.Read = data the compilers throws:

nrf24_client:41: error: initializer fails to determine size of ‘data’

Im guessing I need to get crafty with a few topics like pointers or perhaps more familiar with data types but I just don’t know what to identify the topics I needs to learn for this as for sure

Ok, simple C strings:
C style strings are actually a pointer to memory location, this means:

char buff[];
// is the same as
char * buff;

neither one of the declarations define how large of a memory buffer they point to.
both of these definitions of buff use 2 byte of memory to contain the pointer value.

The following code is snipped from your example:

    if (nrf24.recv(buf, &len))
    {
      Serial.print("got reply: ");
      Serial.println((char*)buf);
    }

This code passes a pointer to buf, and a pointer to len to nrf24.recv(), the recv() function probably places the received data packet into the memory location pointed to by buf, then stores the packet length in the memory pointed to by len.
Now that println() operation assumes the data starting at the location pointed to by ‘buf’ has a zero that marks the end. if the packet recv() actually returned is not terminated by a zero character println() will continue sending characters from memory until it finds a zero.

Usually when you have a Cstring and a length of that Cstring you need to insert the end marker yourself buf[len]=0;

here is some code to read from Serial, and Send it out the nrf24:

#define BUFLEN 50; // maximum size of buffer + zero byte end marker
char buf[BUFLEN]; 

//this read routine assume the input line is terminated with a newline \n
uint8_t len=0; // current length of Cstring
bool done=false; // got whole line
while(!done){
  if(Serial.available()){ // characters in Serial Receive Buffer.
   char ch=Serial.read();
   done = ch=='\n';
   if(!done){
     buf[len++]=ch; // store ch into the next space, increment index
     done= (len==(BUFLEN-1)); // Cstring is full, no more room in buffer, except for end marker
     }
   }
buf[len]=0; // set end marker
len++; // before this increment len pointed to the last position used in buf[0..(BUFLEN-1)]
   // I actually need it to be a count of the number of spaces used in buf, so +1

// now we have the line of data in buf, now send it out the nrf24

 nrf24.send(buf,len);
  
// I assume the send() function does not care about the content, it just sends len bytes 
// starting at buf.

Chuck.

Thank you for your reply, Chuck! I'll give it a shot when I get home. I need to start reading about strings and pointers.