I want to read serial input to a char array and keep reading data until it gets to a custom end character. I have no idea how to go about this!
I continued looking and found readBytesUntil. I looked around and couldn't find it. Thanks!
Have a look at the second example in Serial Input Basics - simple reliable non-blocking ways to receive data.
...R
1. Assume that you want to send this string: Arduino Forum from the InputBox of the Serial Monitor. Also, assume that the last character to be sent at the end of the said string is the Newline character which will be automatically implemented if Newline option is choosen in the Line ending tab of Serial Monitor (Fig-1).
Figure-1:
2. You enter/type this string: Arduino Forum in the InputBox of the Serial Monitor and then click on the Send button. As a result, the charcaters of the string are sent one by one; where, A is transmitted first in its ASCII code 0x41 (Fig-2), then r in its ASCII code, ..., and finally Newline character (\n) in its ASCII code 0x0A. Each charcater is sent in 10-bit assembly known as asynchronous frame (Fig-3) which consists of 1-START bit (always LOW), 8-bit character code that includes 7-bit ASCII code, optional 1-Parity bit, 1-STOP bit (always HIGH).
Figure-2:
Figure-3:
3. At the UNO (the receiver) side, you receive one character at a time and save it in the character type array named char myData[20]. The following codes help to receive A and save it at the first location of the array. When a character arrives at the UNO, the UNO is interrupted; it goes to an interrupt sub routine (ISR); it reads the data from the RX-Section and saves into a FIFO (first-in first-out) type BUFFer (Fig-4 as conceptual view).
byte n = Serial.available(); //check if a character has arrived in the FIFO BUFFer
if(n !=0 )
{
myData[0] = Serial.read(); //Data byte is taken out from FIFO BUFFer, and it is saved in local array.
}
Figure-4:
4. To receive all the charcaters of the string, the following codes (no blocking) could be placed at the appropriate place of the sketch.
byte n = Serial.available(); //check if a character has arrived in the FIFO BUFFer
if(n !=0 )
{
char x = Serial.read();
if(x != '\n') //check if Newline character has arrived; if so, stop receiving further charcaters
{
myData[i] = x; //received character is not a Newline character; save it
i++; //adjust array index
}
else
{
myData[i] = '\0'; //insert null character at the end of the received string
Serial.println(myData); //show the string on the OutputBox of Serial Monitor
}
}
5. At the UNO side, you can place the following blocking code (in lieu of the codes of Step-4) at the appropriate place of your sketch to receive all the characters of the string and save in an array named char myData[20].
byte n = Serial.available();
if(n ! =0)
{
byte m = Serial.readBytesUntil('\n', myData, 20);//receive and save all charcaters except '\n'
myData[m] = '\0'; //insert null character in the array at the end of the last character of the string
Serial.print(myData); //show received string
}
6. Please, make a sketch by using the above snippets and post it here.