How do I read characters into an array from the serial monitor?

I have read a bunch of forum posts about this topic so I think I have a general grasp of how to do this, but my approach does not seem to be working. What I am doing is creating a command that waits for a user to give input. The function then saves the typed message into a char array which is then re-displayed in the serial monitor. What appears to be happening is that the function reads only a part of the typed message.

// function attached to the "send" command. 
// Takes user input from the serial monitor and sends message.
void send() {
  char message[] = ""; // message to be sent
  bool sent = 0; // break out once message is typed
  Serial.println("type message: "); // prompt for user to type message
  while(!sent) { // loop while waiting for user to type message
    int availableBytes = Serial.available(); // gets number of bytes from serial monitor input
    for(int i=0; i<availableBytes; i++) // loops through all bytes available in the serial monitor
    {
      message[i] = Serial.read(); // reads in each byte and saves to char array message
      Serial.println(message[i]); // DEBUGGING prints each letter of the message
      if(i = availableBytes-1){ // if we reach the end of the message
        sent = 1; // set sent to true
        Serial.println("message read"); // print that the message has been read
      }
    }
    Serial.println("outer loop"); // DEBUGGING checks if we are still in the loop
  }
  uint8_t nBytes = sizeof(message); // number of bytes of the message
  Serial.println(message); // prints message before being sent
}

What I am typing into the serial monitor is
send
this is a test

What is being output
type message:
outer loop
outer loop
.
.
.
t
message read
outer loop
t��

Any advice on how to better approach the problem would be much appreciated.

See the Serial Input Basics tutorial.

This is not valid C/C++, for good reasons. It reserves no memory for the array.

  char message[] = ""; // message to be sent

Is it always best practice to reserve memory even if the number of bytes used is unknown? If so what is the recommended amount of memory I should reserve for this use case?

I can't analyze your code, but here you almost certainly mean ==, not =.

Test for equality, not assignment.

In the IDE preferences, crank up all warnings and verbosity. This will then be brought to your attention as maybe you didn't actually mean what you wrote.

a7

That would be a problem wouldn't it. Sorry about that. Thank you for letting me know about that feature I did not know Arduino IDE's errors could be adjusted.

Yeah, none of us has ever done a dumb mistake like that. :wink:

a7

Yes, always. On an MCU, reserve the maximum amount of memory you expect to encounter (consistent with the amount of dynamic memory available), and always take care not to exceed array bounds.

Writing outside of bounds will lead to unexpected behavior or program crashes.

Finally, remember that array indexing starts at zero, so this reserves 30 bytes, sets them all to zero and writes to byte 31, violating the array boundary:

char line[30]={0};
line[30]='A';  //array boundary violation

You could try it like this..

char buf [90];

void loop ()
{
    if (Serial.available ())  {
        int n = Serial. readBytesUntil ('\n', buf, sizeof(buf)-1);
        buf [n] = 0;      // terminate string

        Serial.println (buf);
    }
}

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

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.