Sending Char Arrays via Serial Library


I'm sending some commands from my computer to my Arduino Mega and vice-versa. I'm testing with Tera Term right now and running into a weird problem. This problem is with sending data from my Arduino to my computer using Serial.write. In particular, trying to send an array of characters or even an individual character from an array. Neither will show up in Tera Term, even though sending an individual char variable or chars or arrays of chars using "" works just fine. I thought I was following correctly the instructions on the Serial.write descriptor page, but something is off.

I don't have a problem sending individual characters.

char command = 'A';

This works just fine and shows up in tera term without a problem.

char response[3] = "ML";

//my first variant just trying to send a single char from the array

//my second variant after rereading the Serial.write page
Serial2.write(response, 3);

//a final variant

Passing in the array name (my first try) doesn't work, and passing in the first character (my first variant) does not work. Nothing shows up in tera term. My final variant does work.

Does anyone know the proper way to send an array of characters from my Arduino to my computer? Thanks!

char response[3] = "ML";

Did you mean

char response[3] = "ML";


Instead of

char command = 'A';

did you mean

char command = 'A';


Hey AWOL. Maybe I have this wrong. I know that Print and Write work differently. As if I wrote Serial.write(10), it would send the number 10 as binary, versus Serial.print sending each character's ASCII value. I thought if I used Serial.write on an ASCII character, it would simply send the character's binary designation. I didn't have a problem creating a character variable and sending that variable using Serial.write.

Does Serial.write simply not work for character arrays?

And yes jremington - sorry I got that example code wrong. I meant Serial2.write(command).

void SendStatus() {
  Serial.println("SendStatus: Sending Status");
  char status[3];
  if (mystate == LOAD_WAITING_COMM){
    Serial.println("my status is load waiting comm");
    status[3] = "LP";
  if (mystate == UNLOAD_WAITING_COMM)status[3] = "UP";
  if (mystate == MOVING_LOAD_POS)status[3] = "ML";
  if (mystate == MOVING_UNLOAD_POS)status[3] = "MU";
  if (mystate == ERROR_POS)status[3] = "EP";

Here is the function in question. Whether I use Serial2.print or Serial2.write, I can't get my status array to show up in Tera Term. If I change status to status[0] just to see if I can get a single character to show up, still nothing. But if I modify that line to read Serial2.write("ML") it works. Why can I manually send character arrays but for the life of me I can't pass a char array variable in?

  char status[3];
    status[3] = "LP";

Do you see the problem there?

I don't. If you're referring to the length of 3 as opposed to the 2 characters, I gave it a length of 3 to accommodate the null character the reference material said a character array had to have.

I was going off the example of Str5, where the word arduino has 7 characters but they gave the character a length of 8.

How about "In a three element array, the element with the subscript [3] does not exist" ?

Nor should you try to store in the non-existent element (of type char) a pointer to char.

That is extremely true. What is the proper way to assign an array of characters to a char array after its initialization?

status = “ML”;


status = “ML”;

I got errors for both of these.

Use strcpy or strncpy. See: Take care to remember the terminating null and don't write beyond the end of your array.

Fantastic. I just modified my conditional statements to

strncpy(status, "ML", 3)

and now it works. Thank you!

Read carefully the description of strncpy:

No null-character is implicitly appended at the end of destination if source is longer than num. Thus, in this case, destination shall not be considered a null terminated C string (reading it as such would overflow).

As a precaution, I always do:

 strncpy(destination, source, num);
  destination[num-1] = '\0';