Serial object cannot recognize last character of input serial stream

[edit] While creating this post I figured out that if I set the below to "+1" it works, can someone explain to me why? Do I have to add an extra array slot for the serial stream string termination character?

    *char serialInCharArr[serialInString.length() + 1];*
   *serialInString.toCharArray(serialInCharArr, (serialInString.length() + 1));*

I can't figure out why the last character of my serial stream is always unrecognizable? The IDE displays it as a rectangle. I believe it has something to do with string termination, but I can't figure out why?

Below the console output is repro code. I have it set to 9600 baud and Both NL & CR .

0:43:56.387 ->

10:43:56.387 -> ***** Start *****

10:43:56.387 ->

10:44:01.296 -> New BT data: <sundays=disabled>

10:44:01.441 -> serialInString.length(): 18

10:44:01.442 -> 0 - <

10:44:01.442 -> 1 - s

10:44:01.442 -> 2 - u

10:44:01.442 -> 3 - n

10:44:01.443 -> 4 - d

10:44:01.443 -> 5 - a

10:44:01.443 -> 6 - y

10:44:01.443 -> 7 - s

10:44:01.444 -> 8 - =

10:44:01.444 -> 9 - d

10:44:01.444 -> 10 - i

10:44:01.444 -> 11 - s

10:44:01.444 -> 12 - a

10:44:01.478 -> 13 - b

10:44:01.478 -> 14 - l

10:44:01.478 -> 15 - e

10:44:01.498 -> 16 - d

10:44:01.498 -> 17 - □

10:44:05.948 -> New BT data: <sundays=enabled>

10:44:06.082 -> serialInString.length(): 17

10:44:06.082 -> 0 - <

10:44:06.082 -> 1 - s

10:44:06.083 -> 2 - u

10:44:06.083 -> 3 - n

10:44:06.098 -> 6 - y

10:44:06.098 -> 7 - s

10:44:06.098 -> 8 - =

10:44:06.098 -> 9 - e

10:44:06.098 -> 10 - n

10:44:06.128 -> 11 - a

10:44:06.128 -> 12 - b

10:44:06.128 -> 13 - l

10:44:06.128 -> 14 - e

10:44:06.158 -> 15 - d

10:44:06.158 -> 16 - □


#include <EEPROM.h>
#include <SoftwareSerial.h>

SoftwareSerial BTSerial(12, 13);   // RX , TX

// Serial In/Out Variables
String serialInString;
boolean newData = false;


void setup() 
{
    Serial.begin(9600);
    BTSerial.begin(9600);

    while (!Serial)
    {
      ; // Wait for serial to connect
    }

    while (!BTSerial)
    {
      ; // Wait for BTSerial to connect
    }
   
    delay(2000);

    Serial.println("");
    Serial.println("    ***** Start *****    ");
    Serial.println("");
}

void loop() 
{
        while(BTSerial.available() > 0 && newData == false)
    { 
        serialInString = BTSerial.readString();
        Serial.print("New BT data: "); Serial.println(serialInString);
        Serial.print("serialInString.length(): "); Serial.println(serialInString.length());
        char serialInCharArr[serialInString.length()];
        serialInString.toCharArray(serialInCharArr, serialInString.length());
        //String serialInStringArr[100];
        String bleh = "";
        for (int i = 0; i < serialInString.length(); i++)
        {
            Serial.print(i); Serial.print(" - "); Serial.println(serialInCharArr[i]);
            if (serialInCharArr[i] == '<')
            {
                bool startCharFound = true;
                bleh = "";
            }
            else if (serialInCharArr[i] == '>')
            {
                bool endCharFound = true;
                Serial.print("End char found. String is: "); Serial.println(bleh);
            }
            else
            {
                bleh = bleh + serialInCharArr[i];
            }
        }
        //myString.toCharArray(buf, len)
        newData = true;
        //Serial.println(serialInString.startsWith("alldaystime"));
        delay(15);
    }
  
    delay(5);

    newData = false;

}

Set your Serial Monitor to no line endings and try again. That last character is most likely your CR (0x0C). The String class also has a .trim() function that will remove any/all line endings

plus one that length, to char array null terminates the array..
good luck.. ~q

So the answer is that it needs an extra char at the end to store the string termination character?

thinking that's why your missing the final >..

 char serialInCharArr[serialInString.length()+1];
      serialInString.toCharArray(serialInCharArr, sizeof(serialInCharArr));

good luck.. ~q

1 Like

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