readBytesUntil and Null Termination

Is the readBytesUntil function supposed to null terminate the array it stores data in, or do I have to do this myself? I am writing an application and starting off simple by getting my Leonardo to echo back commands sent to it from a VB application. The VB sends commands terminated by a CR. The Leonardo looks for the CR termination character, reads the data to an array and then echoes back the command to the serial port. When I transmit "hello", the Leonardo echoes "hello", however if I follow this by sending "a", the Leonardo echoes "aello" as though a null isn't added to the array and the println is reading past the "a". It's been a while since I programed in C so maybe I'm missing something?

Here's the code

define CR 13

define LF 10

int button_input =12; int led_output =7; int onboard_led =13; char received_command[50];

void setup() { pinMode(onboard_led,OUTPUT); pinMode(button_input,INPUT_PULLUP); pinMode(led_output,OUTPUT);

Serial.begin(9600);

}

void loop() { if(digitalRead(button_input)) // Executes program if input is high { if (Serial.available() > 0) { if(Serial.readBytesUntil(CR, received_command, 25)) // read characters until CR terminator { Serial.println(received_command); // Echo input to serial port digitalWrite(led_output,HIGH); // Pulse LED on and off delay(1000); digitalWrite(led_output,LOW); } } } }

Update - I terminated the array with a null as in the code below

define CR 13

define LF 10

int button_input =12; int led_output =7; int onboard_led =13; char received_command[50]; byte num_chars_received;

void setup() { pinMode(onboard_led,OUTPUT); pinMode(button_input,INPUT_PULLUP); pinMode(led_output,OUTPUT);

Serial.begin(9600);

}

void loop() { if(digitalRead(button_input)) // Executes program if input is high { if (Serial.available() > 0) { num_chars_received = Serial.readBytesUntil(CR, received_command, 25); // CR terminator character isn't counted in number of characters received if(num_chars_received>0) { received_command[num_chars_received]='\0'; // terminate string with NULL Serial.println(received_command); // Echo input to serial port digitalWrite(led_output,HIGH); // Pulse LED on and off delay(1000); digitalWrite(led_output,LOW); } } } }

Pretty sure that function doesn't add anything. It just reads in from the serial buffer into an array. You'll need to add a null terminator yourself unless one was already on the message when it was sent from the sender.