Searching and parsing data from serial.read

  1. When assembling a string - what is the purpose of appending it with '\0' ????

  2. When reading a serial input I'm unable to detect the end of the message with '\n' so I get a 1 second pause for the read to timeout (default 1000ms). I've tried '\r' as well and that doesn't work. I can get round this by reducing the default timeout, but would like to know what I'm doing wrong and why I can't detect the end of the message buffer.

regarding #2 my understanding is that you simulate the input by typing in data such as 80 5A 66 BE E0 2E 5C 55 BE E0 2E in the console. is that right? if so there is a little pulldown menu in your console next to the speed settings (where you put 57600 most likely) where you can decide if sending something from the line above will add automatically or not \r and or \n. Double check that one, may be you are not sending them, so no wonder your code does not see them :). Also as you setTimeout to 10 that's pretty short to wait for data...

for 1/ that's because this is how c strings are designed to be recognized by most c functions like calculating the length of the string is looking for the 0 at the end to decide it's the end. 0 is not a valid ASCII characters so that was convenient. printf use the same technique to know when to stop printing, because when you pass a char array, it's just a pointer in memory and in memory all bytes could be text you want to print. so the 0 tells printf where to stop to print.

PS: a neat trick you did not learn yet apparently :slight_smile: is to press ctrl-t (or cmd-t on a mac) to arrange your code in a more readable manner, with proper indentation)