Write to serial immediately after an array receiving

#include "SoftwareSerial.h"

//String rxData[10]={};
char *rxData[]={0};
char count=0;

void setup() {
  Serial.begin(9600);
}

void loop() {

  if(Serial.available()){  
    rxData[count] = Serial.read();
    count++;
    
    if(rxData!=0)
    { 
      Serial.print("Input data : ");
      for(int i=0; i<=count; i++){
        Serial.println(rxData[i]);
      }
    }
  }
  delay(100);
  
  for(int a=0; a<11; a++){
    rxData[a] = NULL;
  }
  count=0;
}

Where was my mistake?

Probably there

Or there

I've tried it.

  • i == count
  • i < count

What happens, and what do you want to happen ?

Writing to memory that is not yours is never a good idea.

1 Like

Put an undetermined value in the array.

...at an undetermined location.

An array with one element

I want to put an undetermined value in an array.

How will you know if you got it right?

That makes no sense to me, good luck.

It must be output from these parts.
Serial.println(rxData[i]);

OK, when you're writing to memory you own, that's when you can rely on being able to read it back successfully.

So, fix the array first.

I see you started with String and gave up.
Try looking at these examples of reading Strings from Serial. Arduino Software Solutions
Using Strings avoids the memory and count issues discussed above.

void setup() {
  Serial.begin(9600);
  Serial.setTimeout(5); // at 9600 get about 1char/1mS so when reading user input from Arduino IDE monitor can set this short timeout
  for (int i = 10; i > 0; i--) {
    Serial.print(' '); Serial.print(i);
    delay(500);
  }
  Serial.println();
  Serial.println(F("SerialReadString.ino"));
}

void processInput() {
  if (Serial.available()) { // skip readString timeout if nothing to read
    String input = Serial.readString();
    input.trim(); // strip off any leading/trailing space and \r \n
    if (input.length() > 0) {  // got some input handle it
      Serial.print(F(" got a some input '")); Serial.print(input); Serial.println("'");
      // parse input here and set globals with results
    }
  }
}

void loop() {
  processInput(); // handle the Serial in this task
  // other tasks here
}

Having said that, you should take the time to understand how to define and access arrays (char or otherwise)

char *rxData[ ]={0};
is mixing a lot of stuff in that statement you are actually defining a array of (char *) of length 1 and setting the pointer to 0. (NULL) What I think you need is an array of (char) of length 10 (or 11)
try
size_t MAX_COUNT = 10;
char rxData[MAX_COUNT]; // allocate memory for 10 chars in an array called rxData

char rxData[MAX_COUNT] = {0}; // allocate memory for 10 chars in an array called rxData and set first location rxData[0] to 0

And

for(int a=0; a<11; a++){
    rxData[a] = NULL;
  }

for a size 10 array the count goes from 0 to 9 so a<11 should be a<10.
However perhaps you where trying to allocate a c-string of size 10 that is 10 chars and a terminating '\0' => array size 11
If that was the case then you need
char rxData[MAX_COUNT+1];
and not forget to add the '\0' after the data has been read.
Finally

rxData[count] = Serial.read();
    count++;

does not limit how big count can get. So after 10 reads you will start writing outside the allocated memory for the array and the sketch will most likely just crash :frowning:

As I said using String avoids all this BUT... you still need to understand arrays.

Good lord guys!

Just change this..

To this.

char rxData[20]; 

Now you have 20 chars that you own to write to.

-jim lee

Yeah, but that's the first glaring one that no one is telling him/her about.

-jim lee

I will try it now. thanks.